/*接收新的连接*/ private void OnAcceptNewConnection(object sender, SocketAsyncEventArgs e) { /*新连接的socket对象*/ var socket = e.AcceptSocket; /*继续接收*/ startAccept(e); /*连接数+1*/ Interlocked.Increment(ref connectionCount); /*尝试取出一个异步完成套接字*/ if (!receiveAsyncEventQueue.TryDequeue(out var socketReceiveAsync)) { socketReceiveAsync = new SocketAsyncEventArgs(); socketReceiveAsync.Completed += SocketAsync_Receive_Completed; socketReceiveAsync.SetBuffer(new byte[1024], 0, 1024); } /*创建一个客户端*/ var session = new TSession { Socket = socket, ReceiveSAE = socketReceiveAsync, APPServer = this, }; socketReceiveAsync.UserToken = session; NewSession?.Invoke(this, session); OnNewSession(session); StartReceive(session); }
/// <summary>收到新连接时处理</summary> /// <param name="client"></param> protected virtual void OnAccept(Socket client) { var session = CreateSession(client); // 设置心跳时间 client.SetTcpKeepAlive(true); if (_Sessions.Add(session)) { //session.ID = g_ID++; // 会话改为原子操作,避免多线程冲突 session.ID = Interlocked.Increment(ref g_ID); //WriteLog("{0}新会话 {1}", this, client.Client.RemoteEndPoint); session.WriteLog("New {0}", session.Remote.EndPoint); if (StatSession != null) { StatSession.Increment(1); } NewSession?.Invoke(this, new SessionEventArgs { Session = session }); //// 自动开始异步接收处理 //if (AutoReceiveAsync) session.ReceiveAsync(); } }
public async void ListenAsync(string ip, int port) { if (Listening) { throw new Exception("Listening"); } Listening = true; server = new System.Net.Sockets.TcpListener(IPAddress.Parse(ip), port); server.Start(); ListeningStarted(this, server); Env.Print($"listening: {server.LocalEndpoint.ToString()}"); try { while (true) { Env.Print("waiting for a connection"); var client = await server.AcceptTcpClientAsync(); Env.Print("one incoming tcp connection"); var session = new TcpClientSession(client); NewSession.Invoke(this, session); TcpClientAccepted?.Invoke(this, session); session.Receive(); } } catch (Exception ex) { Env.Print(ex.Message); } }
/// <summary>接受连接时,对于Udp是收到数据时(同时触发OnReceived)。</summary> /// <param name="sender"></param> /// <param name="e"></param> void Server_NewSession(Object sender, SessionEventArgs e) { var session = e.Session; var ns = OnNewSession(session); NewSession?.Invoke(sender, new NetSessionEventArgs { Session = ns }); }
internal static void OnSessionCreated(NativeInterfaces.IAudioSessionControl newSession) { if (newSession == null) { return; } var args = new AudioSessionCreatedEventArgs(new AudioSession((NativeInterfaces.IAudioSessionControl2)newSession)); NewSession?.Invoke(args); if (args.Release) { args.Session.Dispose(); } }
/// <summary> /// 接收新的连接 /// </summary> private void ChannelActive(IChannelHandlerContext context) { /*连接数+1*/ Interlocked.Increment(ref connectionCount); var channelId = context.Channel.Id.AsShortText(); var session = new TSession() { SessionId = channelId, Channel = context.Channel }; if (allSession.TryAdd(channelId, session)) { NewSession?.Invoke(this, session); OnNewSession(session); } }
internal void OnNewSession(IEntitySession session) { NewSession?.Invoke(session, new EntitySessionEventArgs(session)); }
/// <summary> /// Raise the <see cref="NewSession"/> event /// </summary> /// <param name="eventArgs"></param> private void NewSessionEvent(EventArgs eventArgs) { NewSession?.Invoke(sender: this, e: eventArgs); }
/// <summary>创建会话</summary> /// <param name="remoteEP"></param> /// <returns></returns> public virtual ISocketSession CreateSession(IPEndPoint remoteEP) { if (Disposed) { throw new ObjectDisposedException(GetType().Name); } var sessions = _Sessions; if (sessions == null) { return(null); } // 平均执行耗时260.80ns,其中55%花在sessions.Get上面,Get里面有加锁操作 if (!Active) { // 根据目标地址适配本地IPv4/IPv6 Local.Address = Local.Address.GetRightAny(remoteEP.AddressFamily); if (!Open()) { return(null); } } // 需要查找已有会话,已有会话不存在时才创建新会话 var session = sessions.Get(remoteEP + ""); if (session != null) { return(session); } // 相同远程地址可能同时发来多个数据包,而底层采取多线程方式同时调度,导致创建多个会话 lock (sessions) { // 需要查找已有会话,已有会话不存在时才创建新会话 session = sessions.Get(remoteEP + ""); if (session != null) { return(session); } var us = new UdpSession(this, remoteEP); us.Log = Log; us.LogSend = LogSend; us.LogReceive = LogReceive; // UDP不好分会话统计 //us.StatSend.Parent = StatSend; //us.StatReceive.Parent = StatReceive; us.Packet = SessionPacket?.Create(); session = us; if (sessions.Add(session)) { //us.ID = g_ID++; // 会话改为原子操作,避免多线程冲突 us.ID = Interlocked.Increment(ref g_ID); us.Start(); if (StatSession != null) { StatSession.Increment(1); } // 触发新会话事件 NewSession?.Invoke(this, new SessionEventArgs { Session = session }); } } return(session); }
public void SetSession(Session obj) { Current = obj; NewSession?.Invoke(obj, EventArgs.Empty); }