async def _open(self): rx = StreamReader() transport, proto = await get_running_loop().connect_write_pipe( lambda: StreamReaderProtocol(rx), os.fdopen(self._fd, "wb")) tx = StreamWriter(transport, proto, rx, get_running_loop()) return transport, tx
async def read_pipe(file, size): loop = get_event_loop() reader = StreamReader() protocol = StreamReaderProtocol(reader) transport, _ = await loop.connect_read_pipe( lambda: protocol, fdopen(os_open(file, O_RDONLY | O_NONBLOCK))) data = await reader.read(size) transport.close() return data
async def _connect(cls, address, security, loop): """ Attempt to establish a TCP connection to the address provided. :param address: :param security: :param loop: :return: a 3-tuple of reader, writer and security settings for the new connection :raise BoltConnectionError: if a connection could not be established """ assert isinstance(address, Address) if loop is None: loop = get_event_loop() connection_args = { "host": address.host, "port": address.port, "family": address.family, # TODO: other args } if security is True: security = Security.default() if isinstance(security, Security): ssl_context = security.to_ssl_context() connection_args["ssl"] = ssl_context connection_args["server_hostname"] = address.host elif security: raise TypeError( "Unsupported security configuration {!r}".format(security)) else: security = None log.debug("[#0000] C: <DIAL> %s", address) try: reader = BoltStreamReader(loop=loop) protocol = StreamReaderProtocol(reader, loop=loop) transport, _ = await loop.create_connection( lambda: protocol, **connection_args) writer = BoltStreamWriter(transport, protocol, reader, loop) except SSLError as err: log.debug("[#%04X] S: <REJECT> %s (%d %s)", 0, address, err.errno, strerror(err.errno)) raise BoltSecurityError("Failed to establish a secure connection", address) from err except OSError as err: log.debug("[#%04X] S: <REJECT> %s (%d %s)", 0, address, err.errno, strerror(err.errno)) raise BoltConnectionError("Failed to establish a connection", address) from err else: local_address = Address(transport.get_extra_info("sockname")) remote_address = Address(transport.get_extra_info("peername")) log.debug("[#%04X] S: <ACCEPT> %s -> %s", local_address.port_number, local_address, remote_address) return reader, writer, security
async def main(): arg_parser = ArgumentParser(prog="bloXroute CLI", epilog=get_description(), formatter_class=RawDescriptionHelpFormatter) cli_parser = ArgumentParser(prog="bloXroute CLI", epilog=get_description(), formatter_class=RawDescriptionHelpFormatter) add_base_arguments(arg_parser) add_base_arguments(cli_parser) add_run_arguments(cli_parser) args = sys.argv[1:] if "-h" in args or "--help" in args: cli_parser.print_help() arg_parser.exit() opts, params = arg_parser.parse_known_args() stdin_reader = StreamReader() cli_loop = asyncio.get_event_loop() await cli_loop.connect_read_pipe( lambda: StreamReaderProtocol(stdin_reader), sys.stdin) transport, protocol = await cli_loop.connect_write_pipe( lambda: FlowControlMixin(), os.fdopen(sys.stdout.fileno(), "wb")) stdout_writer = StreamWriter(transport, protocol, None, cli_loop) if not validate_args(opts, stdout_writer): exit(1) try: if opts.interactive_shell or len(params) == 0: await run_cli(opts.rpc_host, opts.rpc_port, opts.rpc_user, opts.rpc_password, opts.account_id, opts.secret_hash, opts.cloud_api_url, opts.cloud_api, cli_parser, stdin_reader, stdout_writer) else: if "help" in sys.argv: cli_parser.print_help(file=ArgParserFile(stdout_writer)) async with RpcClient(opts.rpc_host, opts.rpc_port, opts.rpc_user, opts.rpc_password, opts.account_id, opts.secret_hash, opts.cloud_api_url, opts.cloud_api) as client: await parse_and_handle_command(cli_parser, stdout_writer, client) except (ClientConnectorError, ClientConnectionError, TimeoutError, CancelledError) as e: stdout_writer.write( f"Connection to RPC server is broken: {e}, exiting!\n".encode( "utf-8")) await stdout_writer.drain() if opts.debug: raise finally: stdout_writer.close()
async def read_pipe(file, size): loop = get_event_loop() reader = StreamReader() protocol = StreamReaderProtocol(reader) transport, _ = await loop.connect_read_pipe( lambda: protocol, fdopen(os_open(file, O_RDONLY | O_NONBLOCK))) chunks = list() while size > 0: chunk = await reader.read(size) if not chunk: break chunks.append(chunk) size -= len(chunk) transport.close() return b''.join(chunks)
async def _connect(cls, address, loop, config): """ Attempt to establish a TCP connection to the address provided. :param address: :param loop: :param config: :return: a 3-tuple of reader, writer and security settings for the new connection :raise BoltConnectionError: if a connection could not be established """ assert isinstance(address, Address) assert loop is not None assert isinstance(config, PoolConfig) connection_args = { "host": address.host, "port": address.port, "family": address.family, # TODO: other args } ssl_context = config.get_ssl_context() if ssl_context: connection_args["ssl"] = ssl_context connection_args["server_hostname"] = address.host log.debug("[#0000] C: <DIAL> %s", address) try: reader = BoltStreamReader(loop=loop) protocol = StreamReaderProtocol(reader, loop=loop) transport, _ = await loop.create_connection( lambda: protocol, **connection_args) writer = BoltStreamWriter(transport, protocol, reader, loop) except SSLError as err: log.debug("[#%04X] S: <REJECT> %s (%d %s)", 0, address, err.errno, strerror(err.errno)) raise BoltSecurityError("Failed to establish a secure connection", address) from err except OSError as err: log.debug("[#%04X] S: <REJECT> %s (%d %s)", 0, address, err.errno, strerror(err.errno)) raise BoltConnectionError("Failed to establish a connection", address) from err else: local_address = Address(transport.get_extra_info("sockname")) remote_address = Address(transport.get_extra_info("peername")) log.debug("[#%04X] S: <ACCEPT> %s -> %s", local_address.port_number, local_address, remote_address) return reader, writer
async def open(self, loop=None): """ Open the transmit end on the given event loop, returning an instance of [`asyncio.StreamWriter`](https://docs.python.org/3/library/asyncio-stream.html#streamwriter). If no event loop is given, the default one will be used. """ if loop is None: loop = asyncio.get_event_loop() txTransport, txProto = await loop.connect_write_pipe( lambda: StreamReaderProtocol(StreamReader(loop=loop), loop=loop), os.fdopen(self._fd, "w")) tx = StreamWriter(txTransport, txProto, None, loop) return tx
async def open(self, loop=None): """ Open the receive end on the given event loop, returning an instance of [`asyncio.StreamReader`](https://docs.python.org/3/library/asyncio-stream.html#streamreader). If no event loop is given, the default one will be used. """ if loop is None: loop = asyncio.get_event_loop() rx = StreamReader(loop=loop) _, _ = await loop.connect_read_pipe( lambda: StreamReaderProtocol(rx, loop=loop), os.fdopen(self._fd)) return rx
async def _open(self): rx = StreamReader() transport, _ = await get_running_loop().connect_read_pipe( lambda: StreamReaderProtocol(rx), os.fdopen(self._fd, 'rb')) return transport, rx