private void OnTcpPacketAssembled(Socket sender, byte[] buffer, int offset, int size) { lock (this) { if (_closed) { return; } DoubleServerClient client = _tcpClients[sender]; _receiveBuffer.SetContents(buffer, offset, size); if (client.State == ClientState.TcpAuthenticating) { if (_handler.TcpAuthenticateClient(client, _receiveBuffer, out byte[] encryptionKey, out byte errorCode))
/// <summary> /// Sends the specified payload over TCP to the specified client. /// </summary> /// <param name="recipient">The client in question.</param> /// <param name="payloadWriter">The action which writes the payload to a buffer.</param> public void SendTcp(IDoubleServerClient recipient, Action <BitBuffer> payloadWriter) { lock (this) { if (_closed) { return; } DoubleServerClient client = (DoubleServerClient)recipient; SendEncryptedLengthPrefixOnlyTcp(client.TcpSocket, client.EncryptionKey, buffer => { buffer.Write(client.NextSendSequenceId()); payloadWriter(buffer); }); } }
/// <summary> /// Sends the specified payload over UDP to the specified client. /// </summary> /// <param name="recipient">The client in question.</param> /// <param name="payloadWriter">The action which writes the payload to a buffer.</param> public void SendUdp(IDoubleServerClient recipient, Action <BitBuffer> payloadWriter) { lock (this) { if (_closed) { return; } DoubleServerClient client = (DoubleServerClient)recipient; using (_sendBuffer) { UdpHelper.WritePrefix(_sendBuffer, client.ConnectionStartTimestamp, payloadWriter); byte[] encrypted = _crypto.Encrypt(client.EncryptionKey, _sendBuffer.Array, 0, _sendBuffer.Size); _udp.Send(client.UdpEndPoint, encrypted, 0, encrypted.Length); } } }
private void OnTcpConnected(Socket socket) { lock (this) { if (_closed) { return; } DoubleServerClient client = new DoubleServerClient(socket); _tcpClients.Add(socket, client); Task.Delay(TcpAuthenticationTimeout).ContinueWith(task => { lock (this) { if (client.State == ClientState.TcpAuthenticating) { Disconnect(client); } } }); } }
/// <summary> /// Kicks a specific client. /// </summary> /// <param name="client">The client in question.</param> public void Disconnect(IDoubleServerClient client) { lock (this) { if (_closed) { return; } DoubleServerClient impl = (DoubleServerClient)client; impl.Disconnected(); _tcpClients.Remove(impl.TcpSocket); _tcp.Disconnect(impl.TcpSocket); if (impl.UdpEndPoint != null) { _udpClients.Remove(impl.UdpEndPoint); } if (impl.State != ClientState.TcpAuthenticating && _authenticatedCount-- == _maxAuthenticatedCount) { _tcp.StartAccepting(); } } }