def encode_by_route(self, msg_data): stream = Stream() self.encode_flag(stream, False) if self.has_id(): self.encode_id(stream) if self.has_route(): route_len = self.route_len() stream.write(struct.pack('B', route_len)) stream.write(self.route) stream.write(msg_data) return stream.getvalue()
def encode_by_code(self, route_code, msg_data): stream = Stream() self.encode_flag(stream) if self.has_id(): self.encode_id(stream) if self.has_route(): stream.write(struct.pack(">H", route_code)) stream.write(msg_data) return stream.getvalue()
def decode(cls, code_to_route, global_protos, data, msgid_to_route=None): flag = 0xF & struct.unpack("B", data[0])[0] message_type = flag >> 1 is_route = flag & 0x01 msg_id = None route = None stream = Stream(data[1:]) if message_type == Message.MSG_TYPE_REQUEST: msg_id = cls.decode_id(stream) route = cls.decode_route(stream, code_to_route, is_route) elif message_type == Message.MSG_TYPE_NOTIFY: route = cls.decode_route(stream, code_to_route, is_route) elif message_type == Message.MSG_TYPE_PUSH: route = cls.decode_route(stream, code_to_route, is_route) elif message_type == Message.MSG_TYPE_RESPONSE: msg_id = cls.decode_id(stream) if isinstance(msgid_to_route, dict): route = msgid_to_route.get(msg_id) return cls.decode_message(stream, message_type, global_protos, msg_id, route)
def protobuf_decode_submessage(stream, global_protos, protos, result): size = protobuf_decode_varint(stream) substream = Stream(stream.read(size)) return protobuf_decode(substream, global_protos, protos, result)