Example #1
0
    async def dial(self, maddr, self_id, options=None):
        """
        dial a transport to peer listening on multiaddr
        :param maddr: multiaddr of peer
        :param self_id: peer_id of the dialer (to send to receiver)
        :param options: optional object
        :return: True if successful
        """
        host = maddr.value_for_protocol('ip4')
        port = int(maddr.value_for_protocol('tcp'))

        reader, writer = await asyncio.open_connection(host, port)

        # First: send our peer ID so receiver knows it
        writer.write(id_b58_encode(self_id).encode())
        await writer.drain()

        # Await ack for peer id
        expected_ack_str = "received peer id"
        ack = (await reader.read(len(expected_ack_str))).decode()

        if ack != expected_ack_str:
            raise Exception("Receiver did not receive peer id")

        return RawConnection(host, port, reader, writer, True)
Example #2
0
    async def dial(self, maddr: Multiaddr) -> IRawConnection:
        """
        dial a transport to peer listening on multiaddr
        :param maddr: multiaddr of peer
        :return: `RawConnection` if successful
        """
        self.host = maddr.value_for_protocol("ip4")
        self.port = int(maddr.value_for_protocol("tcp"))

        reader, writer = await asyncio.open_connection(self.host, self.port)

        return RawConnection(reader, writer, True)
Example #3
0
    async def dial(self, maddr: Multiaddr) -> IRawConnection:
        """
        dial a transport to peer listening on multiaddr
        :param maddr: multiaddr of peer
        :return: `RawConnection` if successful
        :raise OpenConnectionError: raised when failed to open connection
        """
        self.host = maddr.value_for_protocol("ip4")
        self.port = int(maddr.value_for_protocol("tcp"))

        try:
            reader, writer = await asyncio.open_connection(
                self.host, self.port)
        except (ConnectionAbortedError, ConnectionRefusedError) as error:
            raise OpenConnectionError(error)

        return RawConnection(reader, writer, True)
Example #4
0
    async def dial(self, maddr: Multiaddr) -> IRawConnection:
        """
        dial a transport to peer listening on multiaddr.

        :param maddr: multiaddr of peer
        :return: `RawConnection` if successful
        :raise OpenConnectionError: raised when failed to open connection
        """
        self.host = maddr.value_for_protocol("ip4")
        self.port = int(maddr.value_for_protocol("tcp"))

        try:
            stream = await trio.open_tcp_stream(self.host, self.port)
        except OSError as error:
            raise OpenConnectionError from error
        read_write_closer = TrioTCPStream(stream)

        return RawConnection(read_write_closer, True)
Example #5
0
 async def tcp_stream_handler(stream: ReadWriteCloser) -> None:
     nonlocal conn_1
     conn_1 = RawConnection(stream, initiator=False)
     event.set()
     await trio.sleep_forever()
Example #6
0
 async def handler(tcp_stream):
     nonlocal raw_conn_other_side
     raw_conn_other_side = RawConnection(tcp_stream, False)
     event.set()
     await trio.sleep_forever()