private void UpdateClient() { while (Client.DataAvailable) { var packet = ClientStream.ReadPacket(Craft.Net.PacketDirection.Serverbound); Log.LogPacket(packet, true); if (packet is EncryptionKeyResponsePacket) { if (!FinializeClientEncryption((EncryptionKeyResponsePacket)packet)) { if (ConnectionClosed != null) { ConnectionClosed(this, null); } Worker.Abort(); return; } } else { var eventArgs = new IncomingPacketEventArgs(packet, true); if (IncomingPacket != null) { IncomingPacket(this, eventArgs); } lock (Server) { if (!eventArgs.Handled) { ServerStream.WritePacket(packet, Craft.Net.PacketDirection.Serverbound); } // We use a BufferedStream to make sure packets get sent in one piece, rather than // a field at a time. Flushing it here sends the assembled packet. Server.Flush(); } if (packet is DisconnectPacket) { Console.WriteLine("Client disconnected: " + ((DisconnectPacket)packet).Reason); if (ConnectionClosed != null) { ConnectionClosed(this, null); } Worker.Abort(); } } } }
private void UpdateServer() { while (Server.DataAvailable) { var packet = ServerStream.ReadPacket(Craft.Net.PacketDirection.Clientbound); Log.LogPacket(packet, false); if (packet is EncryptionKeyRequestPacket) { InitializeEncryption((EncryptionKeyRequestPacket)packet); } else { var eventArgs = new IncomingPacketEventArgs(packet, false); if (IncomingPacket != null) { IncomingPacket(this, eventArgs); } lock (Client) { if (!eventArgs.Handled) { ClientStream.WritePacket(packet, Craft.Net.PacketDirection.Clientbound); } Client.Flush(); } if (packet is DisconnectPacket) { Console.WriteLine("Server disconnected: " + ((DisconnectPacket)packet).Reason); if (ConnectionClosed != null) { ConnectionClosed(this, null); } Worker.Abort(); } } } }