async def _read_pubsub_msg() -> None: writer_closed_task = asyncio.ensure_future(writer.wait_closed()) while True: done, pending = await asyncio.wait( [read_varint_prefixed_bytes(reader), writer_closed_task], return_when=asyncio.FIRST_COMPLETED, ) done_tasks = tuple(done) if writer.is_closing(): return read_task = done_tasks[0] # Sanity check assert read_task._coro.__name__ == "read_varint_prefixed_bytes" msg_bytes = read_task.result() ps_msg = p2pd_pb2.PSMessage() ps_msg.ParseFromString(msg_bytes) # Fill in the message used in py-libp2p msg = rpc_pb2.Message( from_id=ps_msg.from_id, data=ps_msg.data, seqno=ps_msg.seqno, topicIDs=ps_msg.topicIDs, signature=ps_msg.signature, key=ps_msg.key, ) queue.put_nowait(msg)
async def read_message(self) -> Tuple[int, int, bytes]: """ Read a single message off of the secured connection :return: stream_id, flag, message contents """ # FIXME: No timeout is used in Go implementation. try: header = await decode_uvarint_from_stream(self.secured_conn) message = await asyncio.wait_for(read_varint_prefixed_bytes( self.secured_conn), timeout=5) except (ParseError, RawConnError, IncompleteReadError) as error: raise MplexUnavailable( "failed to read messages correctly from the underlying connection" ) from error except asyncio.TimeoutError as error: raise MplexUnavailable( "failed to read more message body within the timeout" ) from error flag = header & 0x07 channel_id = header >> 3 return channel_id, flag, message
async def read_message(self) -> Tuple[int, int, bytes]: """ Read a single message off of the secured connection :return: stream_id, flag, message contents """ # FIXME: No timeout is used in Go implementation. # Timeout is set to a relatively small value to alleviate wait time to exit # loop in handle_incoming try: header = await decode_uvarint_from_stream(self.secured_conn) except ParseError: return None, None, None try: message = await asyncio.wait_for( read_varint_prefixed_bytes(self.secured_conn), timeout=5 ) except (ParseError, IncompleteReadError, asyncio.TimeoutError): # TODO: Investigate what we should do if time is out. return None, None, None flag = header & 0x07 channel_id = header >> 3 return channel_id, flag, message