private void AcceptCallback(IAsyncResult ar) { if (mode >= ServerMode.WaitClose) { return; } var socket = sysSocket.EndAccept(ar); if (acceptController.CanAccept) { acceptController.OnConnect(); var connection = new TcpConnection(this, new TcpSocket(socket)); acceptQueue.Enqueue(connection); } else { //防止多余的连接进入TIME_WAIT状态,占用服务器资源 socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new LingerOption(true, 0)); socket.Close(); } sysSocket.BeginAccept(AcceptCallback, null); }
internal void OnConnectionReceive(TcpConnection conn, byte[] data) { receiveQueue.Enqueue(Tuple.Create(conn, data)); }
internal void OnConnectionDisconnected(TcpConnection conn, ConnectionCloseType type) { closeQueue.Enqueue(Tuple.Create(conn, type)); acceptController.OnDisconnect(); }