/// <summary>
        /// 监听Socket接受处理
        /// </summary>
        /// <param name="e">SocketAsyncEventArg associated with the completed accept operation.</param>
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                Socket sock = e.AcceptSocket;//和客户端关联的socket
                if (sock.Connected)
                {
                    try
                    {
                        Interlocked.Increment(ref _clientCount);//原子操作加1
                        AsyncUserToken userToken = _userTokenPool.Pop();
                        userToken.ConnectSocket = sock;

                        Log4Debug(String.Format("客户 {0} 连入, 共有 {1} 个连接。", sock.RemoteEndPoint.ToString(), _clientCount));

                        if (!sock.ReceiveAsync(userToken.ReceiveEventArgs))//投递接收请求
                        {
                            ProcessReceive(userToken.ReceiveEventArgs);
                        }
                    }
                    catch (SocketException ex)
                    {
                        Log4Debug(String.Format("接收客户 {0} 数据出错, 异常信息: {1} 。", sock.RemoteEndPoint, ex.ToString()));
                        //TODO 异常处理
                    }
                    //投递下一个接受请求
                    StartAccept(e);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 监听Socket接受处理
        /// </summary>
        /// <param name="e">SocketAsyncEventArg associated with the completed accept operation.</param>
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                Socket sock = e.AcceptSocket;//和客户端关联的socket
                if (sock.Connected)
                {
                    AsyncUserToken userToken = null;
                    try
                    {
                        //确保和_clientCount逻辑一致
                        _maxAcceptedClients.WaitOne();
                        Interlocked.Increment(ref _clientCount);//原子操作加1

                        userToken = _userTokenPool.Pop();
                        if (userToken != null)
                        {
                            userToken.ConnectSocket = sock;

                            _logger.Debug("Client {0} connected, Totoal {1} Connections", sock.RemoteEndPoint.ToString(), _clientCount);

                            if (!sock.ReceiveAsync(userToken.ReceiveEventArgs))//投递接收请求
                            {
                                ProcessReceive(userToken.ReceiveEventArgs);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ex, "IOCP ProcessAccept {0} Data Error.", sock.RemoteEndPoint);
                        if (userToken != null)
                        {
                            CloseClientSocket(userToken);
                        }
                        else
                        {
                            sock.Shutdown(SocketShutdown.Both);
                        }
                    }
                }
            }
            //投递下一个接受请求
            StartAccept(e);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 监听Socket接受处理
        /// </summary>
        /// <param name="e">SocketAsyncEventArg associated with the completed accept operation.</param>
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            Socket sock = e.AcceptSocket;//和客户端关联的socket

            if (e.SocketError == SocketError.Success)
            {
                if (sock.Connected)
                {
                    Log4Debug("连接用户的Handle:" + sock.Handle);
                    try
                    {
                        Interlocked.Increment(ref _clientCount);//原子操作加1
                        AsyncUserToken userToken = _userTokenPool.Pop();
                        userToken.Init();
                        userToken.ConnectSocket = sock;

                        //创建处理数据线程
                        //Thread handle = new Thread(new ParameterizedThreadStart(Handle));
                        //handle.Start(userToken);

                        //心跳时间
                        userToken.userInfo = new RoomActor(DateTime.Now);

                        Log4Debug(String.Format("客户 {0} 连入, 共有 {1} 个连接。", sock.RemoteEndPoint.ToString(), _clientCount));

                        ProcessReceive(userToken);//投递接收请求
                    }
                    catch (SocketException ex)
                    {
                        Log4Debug(String.Format("接收客户 {0} 数据出错, 异常信息: {1} 。", sock.RemoteEndPoint, ex.ToString()));
                        //TODO 异常处理
                    }
                    //投递下一个接受请求
                    StartAccept(e);
                }
            }
        }