Beispiel #1
0
        private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs)
        {
            Debug.WriteLog.Log(eLogLevel.ell_Debug,
                               "Client connection accepted. Local Address: " + acceptEventArgs.AcceptSocket.LocalEndPoint + ", Remote Address: " + acceptEventArgs.AcceptSocket.RemoteEndPoint);
            AsyncSocketUserToken userToken = m_asyncSocketUserTokenPool.Pop();

            m_asyncSocketUserTokenList.Add(userToken); //添加到正在连接列表
            userToken.ConnectSocket   = acceptEventArgs.AcceptSocket;
            userToken.ConnectDateTime = DateTime.Now;

            try
            {
                bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs); //投递接收请求
                if (!willRaiseEvent)
                {
                    lock (userToken)
                    {
                        ProcessReceive(userToken.ReceiveEventArgs);
                    }
                }
            }
            catch (Exception E)
            {
                Debug.WriteLog.Log(eLogLevel.ell_Error, E.StackTrace);
            }

            StartAccept(acceptEventArgs); //把当前异步事件释放,等待下次连接
        }
        /// <summary>
        /// 接收链接后的处理过程
        /// </summary>
        /// <param name="e"></param>
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            //从连接池中分配一个连接赋给当前连接
            AsyncSocketUserToken userToken = m_asyncSocketUserTokenPool.Pop();

            AsyncSocketUserTokenList.Add(userToken); //添加到正在连接列表
            userToken.ConnectSocket   = e.AcceptSocket;
            userToken.ConnectDateTime = DateTime.Now;

            try
            {
                // As soon as the client is connected, post a receive to the connection
                bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs);
                if (!willRaiseEvent)
                {
                    lock (userToken)
                    {
                        ProcessReceive(userToken.ReceiveEventArgs);
                    }
                }
            }
            catch (Exception E)
            {
                Program.Logger.ErrorFormat("Accept client {0} error, message: {1}, trace:{2}", userToken.ConnectSocket, E.Message, E.StackTrace);
                //Program.Logger.Error(E.StackTrace);
            }

            // Accept the next connection request
            StartAccept(e);
        }
        /// <summary>
        /// 监听Socket接受处理
        /// </summary>
        /// <param name="e">SocketAsyncEventArg associated with the completed accept operation.</param>
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            if (!IsRunning)
            {
                return;
            }

            if (e.SocketError == SocketError.Success)
            {
                Socket s = e.AcceptSocket;//和客户端关联的socket
                if (s.Connected)
                {
                    try
                    {
                        _stopWaitSem.WaitOne();     // 服务器是否正在停止
                        if (!IsRunning)
                        {
                            _stopWaitSem.Release();
                            return;
                        }
                        Interlocked.Increment(ref _clientCount);//原子操作加1
                        Log4Debug(String.Format("客户端 {0} 连入, 当前 {1} 个连接。", s.RemoteEndPoint.ToString(), _clientCount));

                        AsyncSocketUserToken userToken = _userTokenPool.Pop();
                        userToken.ConnectSocket  = s;
                        userToken.RemoteEndPoint = userToken.ConnectSocket.RemoteEndPoint;
                        userToken.ConnectTime    = DateTime.Now;
                        ConnectedClients.Add(userToken);

                        _stopWaitSem.Release();

                        if (userToken.ConnectSocket != null)
                        {
                            if (!userToken.ConnectSocket.ReceiveAsync(userToken.RecvEventArgs))//投递接收请求
                            {
                                ProcessReceive(userToken.RecvEventArgs);
                            }
                        }
                    }
                    catch (SocketException ex)
                    {
                        Log4Debug(DateTime.Now.ToString("yyyyMMdd hhMMss.fff") + " ProcessAccept Error:" + ex.Message + "\r\n" + ex.StackTrace);
                        Error2File("ProcessAccept Error:" + ex.Message + "\r\n" + ex.StackTrace);
                        //TODO 异常处理
                    }

                    //投递下一个接受请求
                    StartAccept(e);
                }
            }
        }