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(); } } } }