class Msg(object): proto = False body = '!!! NO BODY !!!' def __init__(self, msg, data=None, extended=False): self.extended = extended self.header = ExtendedMsgHdr(data) if extended else MsgHdr(data) self.msg = msg if data: data = data[self.header._size:] deserializer = get_struct(msg) if deserializer: self.body = deserializer(data) @property def msg(self): return self.header.msg @msg.setter def msg(self, value): self.header.msg = EMsg(value) def serialize(self): return self.header.serialize() + self.body.serialize() @property def steamID(self): return (self.header.steamID if isinstance(self.header, ExtendedMsgHdr) else None ) @steamID.setter def steamID(self, value): if isinstance(self.header, ExtendedMsgHdr): self.header.steamID = value @property def sessionID(self): return (self.header.sessionID if isinstance(self.header, ExtendedMsgHdr) else None ) @sessionID.setter def sessionID(self, value): if isinstance(self.header, ExtendedMsgHdr): self.header.sessionID = value def __repr__(self): return "<Msg %s>" % repr(self.msg) def __str__(self): rows = ["Msg"] header = str(self.header) rows.append("-------------- header --") rows.append(header if header else "(empty)") body = str(self.body) rows.append("---------------- body --") rows.append(body if body else "(empty)") return '\n'.join(rows)
class Msg(object): proto = False body = None #: message instance payload = None #: Will contain body payload, if we fail to find correct message class def __init__(self, msg, data=None, extended=False, parse=True): self.extended = extended self.header = ExtendedMsgHdr(data) if extended else MsgHdr(data) self.msg = msg if data: self.payload = data[self.header._size:] if parse: self.parse() def parse(self): """Parses :attr:`payload` into :attr:`body` instance""" if self.body is None: deserializer = get_struct(self.msg) if deserializer: self.body = deserializer(self.payload) self.payload = None else: self.body = '!!! Failed to resolve message !!!' @property def msg(self): return self.header.msg @msg.setter def msg(self, value): self.header.msg = EMsg(value) def serialize(self): return self.header.serialize() + self.body.serialize() @property def steamID(self): return (self.header.steamID if isinstance(self.header, ExtendedMsgHdr) else None) @steamID.setter def steamID(self, value): if isinstance(self.header, ExtendedMsgHdr): self.header.steamID = value @property def sessionID(self): return (self.header.sessionID if isinstance(self.header, ExtendedMsgHdr) else None) @sessionID.setter def sessionID(self, value): if isinstance(self.header, ExtendedMsgHdr): self.header.sessionID = value def __repr__(self): if isinstance(self.body, _StructMessage): suffix = self.body.__class__.__name__ elif self.payload: suffix = 'not parsed' else: suffix = 'n/a' return "<Msg(%r | %s)>" % (self.msg, suffix) def __str__(self): rows = [repr(self)] header = str(self.header) rows.append("-------------- header --") rows.append(header if header else "(empty)") body = str(self.body) rows.append("---------------- body --") rows.append(body if body else "(empty)") if self.payload: rows.append("------------- payload --") rows.append(repr(self.payload)) return '\n'.join(rows)