Esempio n. 1
0
        private void UpdateClient()
        {
            while (Client.DataAvailable)
            {
                var packet = PacketReader.ReadPacket(ClientStream);
                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)
                            packet.WritePacket(ServerStream);
                        // 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.
                        ServerStream.Flush();
                    }
                    if (packet is DisconnectPacket)
                    {
                        Console.WriteLine("Client disconnected: " + ((DisconnectPacket)packet).Reason);
                        if (ConnectionClosed != null)
                            ConnectionClosed(this, null);
                        Worker.Abort();
                    }
                    if (packet is HandshakePacket)
                    {
                        var handshake = (HandshakePacket)packet;
                        PlayerName = handshake.Username;
                        if (handshake.ProtocolVersion != PacketReader.ProtocolVersion)
                        {
                            Console.WriteLine("Warning! Specified protocol version does not match SMProxy supported version!");
                            Log.Write("Warning! Specified protocol version does not match SMProxy supported version!");
                        }
                    }

                }
            }
        }
Esempio n. 2
0
        private void UpdateServer()
        {
            while (Server.DataAvailable)
            {
                var packet = PacketReader.ReadPacket(ServerStream);
                Log.LogPacket(packet, false);

                if (packet is EncryptionKeyRequestPacket)
                    InitializeEncryption((EncryptionKeyRequestPacket)packet);
                else if (packet is EncryptionKeyResponsePacket)
                    FinializeServerEncryption((EncryptionKeyResponsePacket)packet);
                else
                {
                    var eventArgs = new IncomingPacketEventArgs(packet, false);
                    if (IncomingPacket != null)
                        IncomingPacket(this, eventArgs);
                    lock (Client)
                    {
                        if (!eventArgs.Handled)
                            packet.WritePacket(ClientStream);
                        ClientStream.Flush();
                    }
                    if (packet is DisconnectPacket)
                    {
                        Console.WriteLine("Server disconnected: " + ((DisconnectPacket)packet).Reason);
                        if (ConnectionClosed != null)
                            ConnectionClosed(this, null);
                        Worker.Abort();
                    }
                }
            }
        }
Esempio n. 3
0
        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();
                    }

                }
            }
        }