Exemplo n.º 1
0
        void process_accept(SocketAsyncEventArgs e)
        {
            try
            {
                SocketAsyncEventArgs readEventArg = m_receiveEventArgsPool.pop();
                Log.ASSERT("There are no more available sockets to allocate.", readEventArg != null);
                Log.ASSERT("Client form {0} has disconnected!", e.AcceptSocket.Connected, e.AcceptSocket.RemoteEndPoint.ToString());

                Interlocked.Increment(ref m_numConnectedSocket);
                ((AsyncUserToken)readEventArg.UserToken).Socket = e.AcceptSocket;

                //产生Robot,并返回socketid 到client
                m_robotSystem.generate_robot(e.AcceptSocket);

                Log.INFO("Client from {0} connection accepted.There are {1} clients connected to the server", e.AcceptSocket.RemoteEndPoint.ToString(), m_numConnectedSocket);
                if (!e.AcceptSocket.ReceiveAsync(readEventArg))
                {
                    process_receive(readEventArg);
                }
            }
            catch (SocketException ex)
            {
                Log.INFO("Error when processing data received from {0}:\r\n{1}", e.AcceptSocket.RemoteEndPoint, ex.ToString());
            }
            catch (Exception ex)
            {
                Log.INFO(ex.ToString());
            }
            start_accept(e);
        }