private void ProcessSend(SocketAsyncEventArgs e) { TCPSession session = e.UserToken as TCPSession; if (e.SocketError != SocketError.Success) { ClientClose(session, e.SocketError.ToString()); } else { session?.Send(); } }
private void ProcessAccept(SocketAsyncEventArgs e) { ISession session = null; uint sessionid = SessionIDGenerator.GetNextSessionID(); session = new TCPSession(sessionid, sessionListener, ClientClose, ProcessSend); ((TCPSession)session).receiveSAEA.Completed += IOComleted; ((TCPSession)session).sendSAEA.Completed += IOComleted; sessions.Add(session.id, session); session.SetSocket(e.AcceptSocket); sessionListener.OnClientAccept(session, (IPEndPoint)e.AcceptSocket.RemoteEndPoint); StartReceive(session); StartAccept(e); }
private void ProcessReceive(SocketAsyncEventArgs e) { TCPSession session = e.UserToken as TCPSession; if (session != null && (session.receiveSAEA.BytesTransferred > 0 && session.receiveSAEA.SocketError == SocketError.Success)) { byte[] message = new byte[session.receiveSAEA.BytesTransferred]; Buffer.BlockCopy(session.receiveSAEA.Buffer, 0, message, 0, session.receiveSAEA.BytesTransferred); session.Active((IPEndPoint)e.AcceptSocket.RemoteEndPoint); session.DoReceiveInGateway(message, message.Length); StartReceive(session); } else { if (session != null && session.receiveSAEA.SocketError != SocketError.Success) { ClientClose(session, session.receiveSAEA.SocketError.ToString()); } else { ClientClose(session, "客户端主动断开连接"); } } }