summaryrefslogtreecommitdiff
path: root/net/dispatcher.py
blob: 115212ce4c7afae2d87ff45e44e31c25dd3032af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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))