Keep messages a union of ctl and net (closed #1)
The messages array in the TwPacket class is now planned to stay a Union of CtrlMessage and NetMessage. Which both are empty duck typed classes. That abstract away the 5 specific CtrlMessage classes and the many game and system message classes. ```python class CtrlMessage(Protocol): message_name: str def unpack(self, data: bytes) -> bool: ... def pack(self) -> bytes: ... class NetMessage(Protocol): message_name: str system_message: bool header: ChunkHeader def unpack(self, data: bytes) -> bool: ... def pack(self) -> bytes: ... class TwPacket(PrettyPrint): def __init__(self) -> None: self.messages: list[Union[CtrlMessage, NetMessage]] = [] ```
This commit is contained in:
parent
37730deaa2
commit
7ba6e18ced
8
twnet_parser/ctrl_message.py
Normal file
8
twnet_parser/ctrl_message.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
from typing import Protocol
|
||||
|
||||
class CtrlMessage(Protocol):
|
||||
message_name: str
|
||||
def unpack(self, data: bytes) -> bool:
|
||||
...
|
||||
def pack(self) -> bytes:
|
||||
...
|
|
@ -3,7 +3,6 @@ from typing import Optional
|
|||
from twnet_parser.pretty_print import PrettyPrint
|
||||
from twnet_parser.packer import Unpacker
|
||||
from twnet_parser.packer import pack_str
|
||||
from twnet_parser.chunk_header import ChunkHeader
|
||||
|
||||
class CtrlClose(PrettyPrint):
|
||||
def __init__(
|
||||
|
@ -11,20 +10,6 @@ class CtrlClose(PrettyPrint):
|
|||
reason: Optional[str] = None
|
||||
) -> None:
|
||||
self.message_name = 'close'
|
||||
self.system_message = False
|
||||
self.control_message = True
|
||||
# TODO: do something about ChunkHeader
|
||||
# control messages do not really have a chunk header
|
||||
# but we need it here so we can duck type it into a
|
||||
# NetMessage
|
||||
# maybe it should be an optional field?
|
||||
# or there should be a union again instead of just
|
||||
# NetMessage
|
||||
#
|
||||
# related issue
|
||||
# https://gitlab.com/teeworlds-network/twnet_parser/-/issues/1
|
||||
self.header: ChunkHeader
|
||||
|
||||
self.reason: Optional[str] = reason
|
||||
|
||||
# first byte of data
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
from typing import Optional
|
||||
|
||||
import twnet_parser.msg7
|
||||
from twnet_parser.net_message import NetMessage
|
||||
from twnet_parser.ctrl_message import CtrlMessage
|
||||
|
||||
import twnet_parser.messages7.control.close as close7
|
||||
|
||||
def match_control7(msg_id: int, data: bytes) -> NetMessage:
|
||||
msg: Optional[NetMessage] = None
|
||||
def match_control7(msg_id: int, data: bytes) -> CtrlMessage:
|
||||
msg: Optional[CtrlMessage] = None
|
||||
|
||||
if msg_id == twnet_parser.msg7.CTRL_CLOSE:
|
||||
msg = close7.CtrlClose()
|
||||
|
|
|
@ -7,6 +7,7 @@ from twnet_parser import packer
|
|||
from twnet_parser.pretty_print import PrettyPrint
|
||||
from twnet_parser.message_parser import MessageParser
|
||||
from twnet_parser.net_message import NetMessage
|
||||
from twnet_parser.ctrl_message import CtrlMessage
|
||||
from twnet_parser.chunk_header import ChunkHeader, ChunkFlags
|
||||
from twnet_parser.msg_matcher.control7 import match_control7
|
||||
|
||||
|
@ -24,10 +25,6 @@ CHUNKFLAG7_RESEND = 2
|
|||
|
||||
PACKET_HEADER7_SIZE = 7
|
||||
|
||||
class CtrlMessage(PrettyPrint):
|
||||
def __init__(self, name: str) -> None:
|
||||
self.message_name: str = name
|
||||
|
||||
class PacketFlags7(PrettyPrint):
|
||||
def __init__(self):
|
||||
self.control = False
|
||||
|
@ -160,7 +157,7 @@ class PacketParser():
|
|||
pck.payload_raw = data[PACKET_HEADER7_SIZE:]
|
||||
pck.payload_decompressed = pck.payload_raw
|
||||
if pck.header.flags.control:
|
||||
ctrl_msg: NetMessage = match_control7(data[7], data[8:])
|
||||
ctrl_msg: CtrlMessage = match_control7(data[7], data[8:])
|
||||
pck.messages.append(ctrl_msg)
|
||||
return pck
|
||||
if pck.header.flags.compression:
|
||||
|
|
Loading…
Reference in a new issue