Implemented packet compression when sending packets and added code to make the client actually spawn

This commit is contained in:
Ammar Askar 2015-03-22 18:16:47 +05:00
parent ba70a169e3
commit 463b48414e
2 changed files with 64 additions and 2 deletions

View File

@ -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

View File

@ -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
}