async def _handle(self, raw_message, *, writer, sender_port_holder, receiver_port_holder, ordinal_to_writer): sender_ordinal = port_holder_to_node_ord(sender_port_holder) receiver_ordinal = port_holder_to_node_ord(receiver_port_holder) try: message = BinarySerializer(schema).deserialize( raw_message, PeerMessage) assert BinarySerializer(schema).serialize(message) == raw_message if message.enum == 'Handshake': message.Handshake.listen_port += 100 if sender_port_holder[0] is None: sender_port_holder[0] = message.Handshake.listen_port other_ordinal = self.other(sender_ordinal, receiver_ordinal) if other_ordinal is not None and not other_ordinal in ordinal_to_writer: ordinal_to_writer[other_ordinal] = writer decision = await self.handle(message, sender_ordinal, receiver_ordinal) if decision is True and message.enum == 'Handshake': decision = message if not isinstance(decision, bool): decision = BinarySerializer(schema).serialize(decision) return decision except: # TODO: Remove this if raw_message[0] == 13: # raw_message[0] == 13 is RoutedMessage. Skip leading fields to get to the RoutedMessageBody ser = BinarySerializer(schema) ser.array = bytearray(raw_message) ser.offset = 1 ser.deserialize_field(PeerIdOrHash) ser.deserialize_field(PublicKey) ser.deserialize_field(Signature) ser.deserialize_field('u8') # The next byte is the variant ordinal of the `RoutedMessageBody`. # Skip if it's the ordinal of a variant for which the schema is not ported yet if raw_message[ser.offset] in [3, 4, 5, 7]: # Allow the handler determine if the message should be passed even when it couldn't be deserialized return await self.handle(None, sender_ordinal, receiver_ordinal) is not False print("ERROR 13", int(raw_message[ser.offset])) else: print("ERROR", int(raw_message[0])) raise return True
def call_handler(raw_msg, handler, sender_port_holder, receiver_port_holder): try: obj = BinarySerializer(schema).deserialize(raw_msg, PeerMessage) assert BinarySerializer(schema).serialize(obj) == raw_msg if obj.enum == 'Handshake': obj.Handshake.listen_port += 100 if sender_port_holder[0] is None: sender_port_holder[0] = obj.Handshake.listen_port decision = handler(obj, port_holder_to_node_ord(sender_port_holder), port_holder_to_node_ord(receiver_port_holder)) if decision == True and obj.enum == 'Handshake': decision = obj if type(decision) != bool: decision = BinarySerializer(schema).serialize(decision) return decision except: if raw_msg[0] == 13: # raw_msg[0] == 13 is RoutedMessage. Skip leading fields to get to the RoutedMessageBody ser = BinarySerializer(schema) ser.array = bytearray(raw_msg) ser.offset = 1 ser.deserialize_field(PeerIdOrHash) ser.deserialize_field(PublicKey) ser.deserialize_field(Signature) ser.deserialize_field('u8') # The next byte is the variant ordinal of the `RoutedMessageBody`. # Skip if it's the ordinal of a variant for which the schema is not ported yet if raw_msg[ser.offset] in [3, 4, 5, 7, 10]: return True print("ERROR 13", int(raw_msg[ser.offset])) else: print("ERROR", int(raw_msg[0])) raise return True