Do not abstract away versions

This library should not try to detect versions
because the user of the library might not want to do that
and it has performance implications

A user of this library should either know which protocol
version they expect to send and receive or they should just
try the 0.6 parser and then the 0.7 parser of this library
and decide them selfs which version to detect
This commit is contained in:
ChillerDragon 2023-03-18 09:10:56 +01:00
parent 6f2a9eda51
commit 9c3cbbc105

View file

@ -57,7 +57,7 @@ class TwPacket(PrettyPrint):
self.header: PacketHeader = PacketHeader() self.header: PacketHeader = PacketHeader()
self.messages: list[Union[CtrlMessage, GameMessage, SysMessage]] = [] self.messages: list[Union[CtrlMessage, GameMessage, SysMessage]] = []
class PacketHeaderParser(): class PacketHeaderParser7():
def parse_flags7(self, data: bytes) -> PacketFlags7: def parse_flags7(self, data: bytes) -> PacketFlags7:
# FFFF FFaa # FFFF FFaa
flag_bits = (data[0] & 0xfc) >> 2 flag_bits = (data[0] & 0xfc) >> 2
@ -96,10 +96,10 @@ class PacketParser():
pck = TwPacket() pck = TwPacket()
pck.version = '0.7' pck.version = '0.7'
# TODO: what is the most performant way in python to do this? # TODO: what is the most performant way in python to do this?
# heap allocating a PacketHeaderParser just to bundle a bunch of # heap allocating a PacketHeaderParser7 just to bundle a bunch of
# methods that do not share state seems like a waste of performance # methods that do not share state seems like a waste of performance
# would this be nicer with class methods? # would this be nicer with class methods?
pck.header = PacketHeaderParser().parse_header(data) pck.header = PacketHeaderParser7().parse_header(data)
if pck.header.flags.control: if pck.header.flags.control:
if data[7] == 0x04: # close if data[7] == 0x04: # close
msg_dc = CtrlMessage('close') msg_dc = CtrlMessage('close')