private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs) { Program.Logger.InfoFormat("Client connection accepted. Local Address: {0}, Remote Address: {1}", acceptEventArgs.AcceptSocket.LocalEndPoint, acceptEventArgs.AcceptSocket.RemoteEndPoint); AsyncSocketUserToken userToken = m_asyncSocketUserTokenPool.Pop(); m_asyncSocketUserTokenList.Add(userToken); //添加到正在连接列表 userToken.ClientSocket = acceptEventArgs.AcceptSocket; userToken.ConnectDateTime = DateTime.Now; try { bool willRaiseEvent = userToken.ClientSocket.ReceiveAsync(userToken.ReceiveEventArgs); //投递接收请求 if (!willRaiseEvent) { lock (userToken) { ProcessReceive(userToken.ReceiveEventArgs); } } } catch (Exception E) { Program.Logger.ErrorFormat("Accept client {0} error, message: {1}", userToken.ClientSocket, E.Message); Program.Logger.Error(E.StackTrace); } StartAccept(acceptEventArgs); //把当前异步事件释放,等待下次连接 }
/// <summary> /// 接收链接后的处理过程 /// </summary> /// <param name="e"></param> private void ProcessAccept(SocketAsyncEventArgs e) { //Program.Logger.InfoFormat("Client connection accepted. Local Address: {0}, Remote Address: {1}", acceptEventArgs.AcceptSocket.LocalEndPoint, acceptEventArgs.AcceptSocket.RemoteEndPoint); //从连接池中分配一个连接赋给当前连接 AsyncSocketUserToken userToken = m_asyncSocketUserTokenPool.Pop(); AsyncSocketUserTokenList.Add(userToken); //添加到正在连接列表 userToken.ConnectSocket = e.AcceptSocket; userToken.ConnectDateTime = DateTime.Now; try { // As soon as the client is connected, post a receive to the connection bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs); if (!willRaiseEvent) { lock (userToken) { ProcessReceive(userToken.ReceiveEventArgs); } } } catch (Exception E) { Program.Logger.ErrorFormat("Accept client {0} error, message: {1}, trace:{2}", userToken.ConnectSocket, E.Message, E.StackTrace); //Program.Logger.Error(E.StackTrace); } // Accept the next connection request StartAccept(e); }
void ProcessAccept(SocketAsyncEventArgs asyncEventArgs) { Program.Logger.InfoFormat("Client connection accepted, LocalEndPoint:{0}, RemoteEndPoint:{1}", asyncEventArgs.AcceptSocket.LocalEndPoint.ToString(), asyncEventArgs.AcceptSocket.RemoteEndPoint.ToString()); AsyncSocketUserToken userToken = m_AsyncSocketUserTokenPool.Pop(); m_AsyncSocketUserTokenList.Add(userToken); userToken.ConnectSocket = asyncEventArgs.AcceptSocket; userToken.ConnectDateTime = DateTime.Now; try { bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs); if (!willRaiseEvent) { lock (userToken) { ProcessReceive(userToken.ReceiveEventArgs); } } } catch (Exception ex) { Program.Logger.ErrorFormat("Accept client {0}, error message {1}", userToken.ConnectSocket, ex.Message); Program.Logger.Error(ex.StackTrace); } //把当前异步释放,等待下次连接 StartAccept(asyncEventArgs); }
private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs) { Debug.WriteLog.Log(eLogLevel.ell_Debug, "Client connection accepted. Local Address: " + acceptEventArgs.AcceptSocket.LocalEndPoint + ", Remote Address: " + acceptEventArgs.AcceptSocket.RemoteEndPoint); AsyncSocketUserToken userToken = m_asyncSocketUserTokenPool.Pop(); m_asyncSocketUserTokenList.Add(userToken); //添加到正在连接列表 userToken.ConnectSocket = acceptEventArgs.AcceptSocket; userToken.ConnectDateTime = DateTime.Now; try { bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs); //投递接收请求 if (!willRaiseEvent) { lock (userToken) { ProcessReceive(userToken.ReceiveEventArgs); } } } catch (Exception E) { Debug.WriteLog.Log(eLogLevel.ell_Error, E.StackTrace); } StartAccept(acceptEventArgs); //把当前异步事件释放,等待下次连接 }