Пример #1
0
        /// <summary>
        /// 接入连接具体操作
        /// </summary>
        /// <param name="saea"></param>
        private void ProcessAccept(SocketAsyncEventArgs saea)
        {
            try
            {
                Interlocked.Increment(ref _CurConn);//改当前连接数
                string        guid    = Guid.NewGuid().ToString("N");
                SocketSession session = new SocketSession {
                    Client = saea.AcceptSocket, SessionId = guid
                };
                _dic.TryAdd(guid, session);                                                            //添加SokcetSession
                MySocketAsyncEventArgs msaea = Buffer.GetSocketEvent();                                //从池中获取一个SocketAsyncEventArgs
                msaea.UserToken = session;
                msaea.SetBuffer(Buffer.Buffer, msaea.CurrentIndex * this.BufferSize, this.BufferSize); //设置连接的缓冲区
                msaea.Completed -= IO_Completed;
                msaea.Completed += IO_Completed;
                _argsDic.TryAdd(guid, msaea);//添加SocketAsyncEventArgs

                NewSessionEvent?.Invoke(null, new SessionEventArgs {
                    Type = SessionType.NEW, Endpoint = saea.AcceptSocket.RemoteEndPoint.ToString(), Session = session
                });
                bool resultFlag = saea.AcceptSocket.ReceiveAsync(msaea);
                if (!resultFlag)
                {
                    ProcessReceive(msaea);
                }
            }catch (Exception e) { Log.WriteLog(e.TargetSite + "->" + e.Message, LogType.INFO); }

            StartAccept(saea);
        }