From 463b48414e2477385dd7f82d8e37a81226db8b53 Mon Sep 17 00:00:00 2001 From: Ammar Askar Date: Sun, 22 Mar 2015 18:16:47 +0500 Subject: [PATCH] Implemented packet compression when sending packets and added code to make the client actually spawn --- network/connection.py | 14 ++++++++++++ network/packets.py | 52 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/network/connection.py b/network/connection.py index 92ae5eb..6065e2a 100644 --- a/network/connection.py +++ b/network/connection.py @@ -281,6 +281,20 @@ class PlayingReactor(PacketReactor): keep_alive_packet.keep_alive_id = packet.keep_alive_id self.connection.write_packet(keep_alive_packet) + if packet.packet_name == "player position and look": + position_response = PositionAndLookPacket() + position_response.x = packet.x + position_response.feet_y = packet.y + position_response.z = packet.z + position_response.yaw = packet.yaw + position_response.pitch = packet.pitch + position_response.on_ground = True + + self.connection.write_packet(position_response) + + if packet.packet_name == "disconnect": + print(packet.json_data) # TODO: handle propagating this back + class StatusReactor(PacketReactor): clientbound_packets = state_status_clientbound diff --git a/network/packets.py b/network/packets.py index 9ce024f..22ee80a 100644 --- a/network/packets.py +++ b/network/packets.py @@ -55,7 +55,20 @@ class Packet(object): data = getattr(self, var_name) data_type.send(data, packet_buffer) - # TODO: implement compression + # compression_threshold of -1 means compression is disabled + if compression_threshold != -1: + if len(packet_buffer.get_writable()) > compression_threshold: + # compress the current payload + compressed_data = compress(packet_buffer.get_writable()) + packet_buffer.reset() + # write out the length of the compressed payload + VarInt.send(len(compressed_data), packet_buffer) + # write the compressed payload itself + packet_buffer.send(compressed_data) + else: + # write out a 0 to indicate uncompressed data + packet_buffer.reset() + VarInt.send(0, packet_buffer) VarInt.send(len(packet_buffer.get_writable()), socket) # Packet Size socket.send(packet_buffer.get_writable()) # Packet Payload @@ -203,6 +216,26 @@ class JoinGamePacket(Packet): {'reduced_debug_info': Boolean}] +class PlayerPositionAndLookPacket(Packet): + id = 0x08 + packet_name = "player position and look" + definition = [ + {'x': Double}, + {'y': Double}, + {'z': Double}, + {'yaw': Float}, + {'pitch': Float}, + {'flags': Byte}] + + +class DisconnectPacketPlayState(Packet): + id = 0x40 + packet_name = "disconnect" + + definition = [ + {'json_data': String}] + + class SetCompressionPacketPlayState(Packet): id = 0x46 packet_name = "set compression" @@ -213,9 +246,24 @@ class SetCompressionPacketPlayState(Packet): state_playing_clientbound = { 0x00: KeepAlivePacket, 0x01: JoinGamePacket, + 0x08: PlayerPositionAndLookPacket, + 0x40: DisconnectPacketPlayState, 0x46: SetCompressionPacketPlayState } -state_playing_serverbound = { +class PositionAndLookPacket(Packet): + id = 0x06 + packet_name = "position and look" + definition = [ + {'x': Double}, + {'feet_y': Double}, + {'z': Double}, + {'yaw': Float}, + {'pitch': Float}, + {'on_ground': Boolean}] + +state_playing_serverbound = { + 0x00: KeepAlivePacket, + 0x06: PositionAndLookPacket } \ No newline at end of file