Added a way to listen for certain packets

This commit is contained in:
Ammar Askar committed 2015-03-22 19:05:55 +05:00
1 parent 20c4c87510
commit 2cc1d64b6d
3 files changed
+65 -8

No files matched your search

+21 -8
View File
@@ -31,6 +31,11 @@ class Connection(object):
_write_lock = Lock()
networking_thread = None
options = ConnectionOptions()
packet_listeners = []
# The reactor handles all the default responses to packets,
# it should be changed per networking state
reactor = None
#: Indicates if this connection is spawned in the Minecraft game world
spawned = False
@@ -71,6 +76,16 @@ class Connection(object):
else:
self._outgoing_packet_queue.append(packet)
def register_packet_listener(self, method, *args):
"""
Registers a listener method which will be notified when a packet of
a selected type is received
:param method: The method which will be called back with the packet
:param args: The packets to listen for
"""
self.packet_listeners.append(PacketListener(method, *args))
def _pop_packet(self):
# Pops the topmost packet off the outgoing queue and writes it out through the socket
#
@@ -157,15 +172,20 @@ class NetworkingThread(threading.Thread):
num_packets += 1
self.connection.reactor.react(packet)
for listener in self.connection.packet_listeners:
listener.call_packet(packet)
if num_packets >= 50:
break
packet = self.connection.reactor.read_packet(self.connection.file_object)
time.sleep(0.05)
class PacketReactor(object):
"""
Reads and reacts to packets
"""
state_name = None
clientbound_packets = None
@@ -174,11 +194,6 @@ class PacketReactor(object):
def __init__(self, connection):
self.connection = connection
# Design objectives:
#
# Avoid buffering data as much as possible
# Wherever possible, read directly from the network stream
# Minimize any overheads, reading packets should be simple
def read_packet(self, stream):
ready_to_read, _, __ = select.select([self.connection.socket], [], [], self.TIME_OUT)
@@ -203,8 +218,6 @@ class PacketReactor(object):
packet_id = VarInt.read(packet_data)
print "Reading packet: " + str(packet_id) + " / " + hex(packet_id) + " (Size: " + str(length) + ") (Actual Size: " + str(len(packet_data.get_writable())) + ")"
# If we know the structure of the packet, attempt to parse it
# otherwise just skip it
if packet_id in self.clientbound_packets:
+33
View File
@@ -29,6 +29,22 @@ class PacketBuffer(object):
return self.b.getvalue()
class PacketListener(object):
packets_to_listen = []
def __init__(self, callback, *args):
self.callback = callback
for arg in args:
if issubclass(arg, Packet):
self.packets_to_listen.append(arg)
def call_packet(self, packet):
for packet_type in self.packets_to_listen:
if isinstance(packet, packet_type):
self.callback(packet)
class Packet(object):
packet_name = "base"
@@ -216,6 +232,14 @@ class JoinGamePacket(Packet):
{'reduced_debug_info': Boolean}]
class ChatMessagePacket(Packet):
id = 0x02
packet_name = "chat message"
definition = [
{'json_data': String},
{'position': Byte}]
class PlayerPositionAndLookPacket(Packet):
id = 0x08
packet_name = "player position and look"
@@ -246,12 +270,20 @@ class SetCompressionPacketPlayState(Packet):
state_playing_clientbound = {
0x00: KeepAlivePacket,
0x01: JoinGamePacket,
0x02: ChatMessagePacket,
0x08: PlayerPositionAndLookPacket,
0x40: DisconnectPacketPlayState,
0x46: SetCompressionPacketPlayState
}
class ChatPacket(Packet):
id = 0x01
packet_name = "chat"
definition = [
{'message': String}]
class PositionAndLookPacket(Packet):
id = 0x06
packet_name = "position and look"
@@ -265,5 +297,6 @@ class PositionAndLookPacket(Packet):
state_playing_serverbound = {
0x00: KeepAlivePacket,
0x01: ChatPacket,
0x06: PositionAndLookPacket
}
+11
View File
@@ -52,9 +52,20 @@ def main():
connection = Connection(address, port, login_response)
connection.connect()
def print_chat(chat_packet):
print "Position: " + str(chat_packet.position)
print "Data: " + chat_packet.json_data
from network.packets import ChatMessagePacket
connection.register_packet_listener(print_chat, ChatMessagePacket)
from network.packets import ChatPacket
while True:
try:
text = raw_input()
packet = ChatPacket()
packet.message = text
connection.write_packet(packet)
except KeyboardInterrupt:
print "Bye!"
sys.exit()