Ejemplo n.º 1
0
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            //Interlocked.Increment(ref m_numConnectedSockets);
            //Console.WriteLine("Client connection accepted. There are {0} clients connected to the server",
            //    m_numConnectedSockets);

            // Get the socket for the accepted client connection and put it into the
            //ReadEventArg object user token
            try
            {
                SocketAsyncEventArgs readEventArgs = m_readWritePool.Pop();
                ((T)readEventArgs.UserToken).Socket = e.AcceptSocket;

                // As soon as the client is connected, post a receive to the connection
                readEventArgs.SetBuffer(0, m_maxSize);
                bool willRaiseEvent = e.AcceptSocket.ReceiveAsync(readEventArgs);
                if (!willRaiseEvent)
                {
                    ProcessReceive(readEventArgs);
                }
                // Accept the next connection request
                StartAccept(e);
            }
            catch (Exception ex)
            {
                LogTool.Log(ex.Message + ex.StackTrace);
                CloseClientSocket(e);
            }
        }
        private void StartReceive()
        {
            m_maxNumberAcceptedClients.WaitOne();
            SocketAsyncEventArgs e = m_readWritePool.Pop();
            bool willRaiseEvent    = mainServiceSocket.ReceiveFromAsync(e);

            if (!willRaiseEvent)
            {
                ProcessReceive(e);
            }
            StartReceive();
        }