Пример #1
0
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            Interlocked.Increment(ref m_numConnectedSockets);
            try
            {
                if (e.AcceptSocket.RemoteEndPoint != null)
                {
                    Log(string.Format("Connection {1} accepted. {0} connected to the server",
                                      m_numConnectedSockets, e.AcceptSocket.RemoteEndPoint.ToString()));
                }
                else
                {
                    Log(string.Format("Connection accepted. {0} connected to the server",
                                      m_numConnectedSockets));
                }
            }
            catch (Exception exception)
            {
                Log(exception.Message);
            }
            var userToken = m_asyncSocketUserTokenPool.Pop();

            userToken.connId = ++connId;
            userToken.Socket = e.AcceptSocket;
            lock (dicUserToken)
            {
                dicUserToken.Add(userToken.connId, userToken);
            }
            try
            {
                InitRequest initRequest = new InitRequest()
                {
                    ConnectionId = userToken.connId,
                };

                userToken.Peer = application.CreatePeer(initRequest);
                userToken.Peer.OnConnected(userToken);
            }
            catch (Exception exception)
            {
                Log(exception.Message);
            }
            try
            {
                bool willRaiseEvent = e.AcceptSocket.ReceiveAsync(userToken.receiveEventArgs);
                if (!willRaiseEvent)
                {
                    lock (userToken)
                    {
                        ProcessReceive(userToken.receiveEventArgs);
                    }
                }

                StartAccept(e);
            }
            catch (Exception ex)
            {
                lock (userToken)
                {
                    CloseClientSocket(userToken.receiveEventArgs);
                }
                Log(ex.ToString());
                StartAccept(e);
                return;
            }
        }
Пример #2
0
 //有新连接
 public abstract PeerBase CreatePeer(InitRequest initRequest);