|
1 |
| -from enum import IntEnum |
2 |
| -from typing import Dict |
3 |
| - |
4 |
| - |
5 |
| -class FragFlag(IntEnum): |
6 |
| - NOT = 0 |
7 |
| - START = 1 |
8 |
| - CONTINUE = 2 |
9 |
| - END = 3 |
10 |
| - |
11 |
| - @staticmethod |
12 |
| - def check(value: int): |
13 |
| - type_dict = { |
14 |
| - 0: FragFlag.NOT, |
15 |
| - 1: FragFlag.START, |
16 |
| - 2: FragFlag.CONTINUE, |
17 |
| - 3: FragFlag.END |
18 |
| - } |
19 |
| - if value in type_dict: |
20 |
| - return type_dict[value] |
21 |
| - else: |
22 |
| - raise ValueError("Invalid fragment flag value.") |
23 |
| - |
24 |
| - |
25 |
| -class Frame: |
26 |
| - BYTE_ORDER = 'big' |
27 |
| - HEADER_COST = 1 + 2 + 2 + 1 |
28 |
| - MAX_PAYLOAD_LEN = 2048 - HEADER_COST |
29 |
| - |
30 |
| - def __init__(self, frame_type: int, payload_len: int, seq: int, frag_flag: FragFlag, data: bytes): |
31 |
| - assert frame_type == 0xff |
32 |
| - assert 2 <= payload_len <= Frame.MAX_PAYLOAD_LEN |
33 |
| - assert 0 <= seq < 2 ** 16 |
34 |
| - assert frag_flag in FragFlag |
35 |
| - assert 0 <= len(data) <= Frame.MAX_PAYLOAD_LEN |
36 |
| - |
37 |
| - self.frame_type = frame_type # 0xff |
38 |
| - self.payload_len = payload_len |
39 |
| - self.seq = seq |
40 |
| - self.frag_flag = frag_flag |
41 |
| - self.data = data |
42 |
| - |
43 |
| - @staticmethod |
44 |
| - def from_dict(data: Dict): |
45 |
| - return Frame(**data) |
46 |
| - |
47 |
| - @staticmethod |
48 |
| - def from_bytes(data: bytes): |
49 |
| - frame_type = int(data[0]) |
50 |
| - payload_len = int(data[1:1+2].hex(), base=16) |
51 |
| - if len(data) < Frame.HEADER_COST + payload_len: |
52 |
| - raise ValueError("Invalid length of payload") |
53 |
| - seq = int(data[3:3+2].hex(), base=16) |
54 |
| - frag_flag = FragFlag.check(data[5]) |
55 |
| - data = data[6:6+payload_len] |
56 |
| - return Frame(frame_type, payload_len, seq, frag_flag, data) |
57 |
| - |
58 |
| - def to_bytes(self): |
59 |
| - result = self.frame_type.to_bytes(1, self.BYTE_ORDER) \ |
60 |
| - + self.payload_len.to_bytes(2, self.BYTE_ORDER) \ |
61 |
| - + self.seq.to_bytes(2, self.BYTE_ORDER) \ |
62 |
| - + self.frag_flag.value.to_bytes(1, self.BYTE_ORDER) \ |
63 |
| - + self.data |
64 |
| - |
65 |
| - return result |
66 |
| - |
67 |
| - def to_dict(self): |
68 |
| - return { |
69 |
| - "frame_type": self.frame_type, |
70 |
| - "payload_len": self.payload_len, |
71 |
| - "seq": self.seq, |
72 |
| - "frag_flag": self.frag_flag, |
73 |
| - "data": self.data |
74 |
| - } |
75 |
| - |
76 |
| - |
77 |
| -if __name__ == "__main__": |
78 |
| - f1 = Frame.from_bytes(b"\xff\x00\x03\x00\x00\x00\x01\x02\x03") |
79 |
| - print(f1.to_dict()) |
80 |
| - print(f1.to_bytes()) |
81 |
| - |
82 |
| - f2 = Frame.from_dict(f1.to_dict()) |
83 |
| - print(f2.to_dict()) |
84 |
| - print(f2.to_bytes()) |
| 1 | +""" |
| 2 | +简易的帧解析和生成,仅支持线性帧结构 |
| 3 | +""" |
| 4 | + |
| 5 | +import struct |
| 6 | +from typing import Any |
| 7 | + |
| 8 | + |
| 9 | +class LinearFrame: |
| 10 | + """ |
| 11 | + basic use format string in struct, expect bare 's' means greedy bytes. |
| 12 | + """ |
| 13 | + def __init__(self, spec: dict[str, str]): |
| 14 | + self.spec = spec |
| 15 | + |
| 16 | + def serialize(self, *args) -> bytes: |
| 17 | + if len(args) != len(self.spec): |
| 18 | + raise Exception |
| 19 | + |
| 20 | + frame = b"" |
| 21 | + # for each section |
| 22 | + for i, fc in enumerate(self.spec.values()): |
| 23 | + arg = args[i] |
| 24 | + if fc == "s": |
| 25 | + fmt = str(len(arg)) + "s" |
| 26 | + else: |
| 27 | + fmt = fc |
| 28 | + section = struct.pack(fmt, arg) |
| 29 | + frame += section |
| 30 | + return frame |
| 31 | + |
| 32 | + def deserialize(self, frame: bytes) -> dict[Any]: |
| 33 | + ret = {} |
| 34 | + ptr = 0 |
| 35 | + # for each section |
| 36 | + for i, item in enumerate(self.spec.items()): |
| 37 | + label, fc = item[0], item[1] |
| 38 | + if fc == "s": |
| 39 | + fmt = str(len(frame) - ptr) + "s" |
| 40 | + else: |
| 41 | + fmt = fc |
| 42 | + size = struct.calcsize(fmt) |
| 43 | + value = struct.unpack(fmt, frame[ptr:ptr+size])[0] |
| 44 | + ptr += size |
| 45 | + ret[label] = value |
| 46 | + return ret |
0 commit comments