/// /// 引发接入事件 /// /// 数据 protected virtual void OnAcceptServerEvent(AcceptServerEventArgs e) { if (AcceptServerEvent != null) { AcceptServerEvent(this, e); } }
private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs) { // Program.Logger.InfoFormat("Client connection accepted. Local Address: {0}, Remote Address: {1}", // acceptEventArgs.AcceptSocket.LocalEndPoint, acceptEventArgs.AcceptSocket.RemoteEndPoint); ClientContext client = m_ClientContextPool.Pop(); try { client.ClientSocket = acceptEventArgs.AcceptSocket; } catch (System.Exception ex) { CloseClientSocket(client); OnErrorServerEvent(new ErrorServerEventArgs(ex, client, TCPErrorType.Unkown, "acceptinit:")); StartAccept(acceptEventArgs); //把当前异步事件释放,等待下次连接 return; } lock (client) { AcceptServerEventArgs ae = new AcceptServerEventArgs(client); OnAcceptServerEvent(ae); if (ae.isCancel) { CloseClientSocket(client); } else { try { m_ClientContextList.Add(client); //添加到正在连接列表 bool willRaiseEvent = client.ClientSocket.ReceiveAsync(client.ReceiveEventArgs); //投递接收请求 if (!willRaiseEvent) { ProcessReceive(client.ReceiveEventArgs); } } catch (Exception e) { CloseClientSocket(client); //Exception ex = new Exception("accept:" + e.Message, e); OnErrorServerEvent(new ErrorServerEventArgs(e, client, TCPErrorType.Unkown, "accept:")); // Program.Logger.ErrorFormat("Accept client {0} error, message: {1}", userToken.ConnectSocket, E.Message); // Program.Logger.Error(E.StackTrace); } } } StartAccept(acceptEventArgs); //把当前异步事件释放,等待下次连接 }