Ejemplo n.º 1
0
        /// <summary>
        /// 处理新客户端连接
        /// </summary>
        private void ProcessAccept(System.Net.Sockets.SocketAsyncEventArgs e)
        {
            var socket = Socket.FillAsClient(e.AcceptSocket);

            if (socket != null)
            {
                Interlocked.Increment(ref numConnectedSockets);
                // log.Info("ProcessAccept", "Client connection accepted. There are {0} clients connected to the server", numConnectedSockets);

                // Get the socket for the accepted client connection and put it into the
                //ReadEventArg object user token
                if (readWritePool.TryDequeue(out System.Net.Sockets.SocketAsyncEventArgs readEventArgs))
                {
                    RemoteConnection client = (RemoteConnection)readEventArgs.UserToken;

                    // 初始化
                    client.Init(socket, IO_Completed);
                    // 加入已连接列表
                    lock (remoteTCPClients) remoteTCPClients.Add(client);
                    // 新连接回调
                    if (socketStatusListener != null)
                    {
                        socketStatusListener.OnConnect(client);
                    }
                    // 加入监控中
                    if (monitorSocketStatusTask != null)
                    {
                        monitorSocketStatusTask.PushCheck(client);
                    }

                    // As soon as the client is connected, post a receive to the connection
                    bool willRaiseEvent = client.Socket.ReceiveAsync(readEventArgs);
                    if (!willRaiseEvent)
                    {
                        ProcessReceive(readEventArgs);
                    }
                }
            }
            // Accept the next connection request
            StartAccept(e);
        }