示例#1
0
async def PacketDecoder(
    manager: ManagerAPI,
    incoming_datagram_receive_channel: ReceiveChannel[IncomingDatagram],
    incoming_packet_send_channel: SendChannel[IncomingPacket],
) -> None:
    """Decodes incoming datagrams to packet objects."""
    logger = logging.getLogger('p2p.discv5.channel_services.PacketDecoder')

    async with incoming_datagram_receive_channel, incoming_packet_send_channel:
        async for datagram, endpoint in incoming_datagram_receive_channel:
            try:
                packet = decode_packet(datagram)
                logger.debug(
                    f"Successfully decoded {packet.__class__.__name__} from {endpoint}"
                )
            except ValidationError:
                logger.warn(f"Failed to decode a packet from {endpoint}",
                            exc_info=True)
            else:
                await incoming_packet_send_channel.send(
                    IncomingPacket(packet, endpoint))
示例#2
0
def test_packet_decoding(packet):
    encoded_packet = packet.to_wire_bytes()
    decoded_packet = decode_packet(encoded_packet)
    assert decoded_packet == packet