summaryrefslogtreecommitdiff
path: root/net/dispatcher.py
diff options
context:
space:
mode:
authorLivio Recchia <recchialivio@libero.it>2020-02-10 23:06:34 +0100
committerLivio Recchia <recchialivio@libero.it>2020-02-10 23:06:34 +0100
commit9a13903a2f7d3a65fdf15a65fb59cccd622e2066 (patch)
tree9403b7dff39eb5e5d7fa0f79efb69b496add4c4b /net/dispatcher.py
parent11cc316b74d5f3f283413a33e7693b314741aa4a (diff)
downloadmanachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.gz
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.bz2
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.xz
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.zip
Initial commit
Diffstat (limited to 'net/dispatcher.py')
-rw-r--r--net/dispatcher.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/net/dispatcher.py b/net/dispatcher.py
new file mode 100644
index 0000000..115212c
--- /dev/null
+++ b/net/dispatcher.py
@@ -0,0 +1,27 @@
+from construct import Struct, ULInt16, MetaField
+from loggers import netlog
+from packetlen import packet_lengths
+
+
+def dispatch(stream, protodef):
+ opcode = ULInt16("opcode").parse_stream(stream)
+
+ if opcode in protodef:
+ func, macro = protodef[opcode]
+ data = macro.parse_stream(stream)
+ func(data)
+ else:
+ data = ''
+ pktlen = packet_lengths.get(opcode, -1)
+
+ if pktlen > 0:
+ data = stream.read(pktlen - 2)
+ elif pktlen == -1:
+ datadef = Struct("data",
+ ULInt16("length"),
+ MetaField("ignore",
+ lambda ctx: ctx["length"] - 4))
+ data = datadef.parse_stream(stream)
+
+ netlog.warning('UNIMPLEMENTED opcode={:04x} data={}'.format(
+ opcode, data))