async def _decode_uvarint_from_stream(stream: INetStream, timeout: float) -> None: try: return await asyncio.wait_for(decode_uvarint_from_stream(stream), timeout) except asyncio.TimeoutError: raise ReadMessageFailure("Timeout") except StreamEOF as error: await stream.close() raise ReadMessageFailure() from error except StreamReset as error: raise ReadMessageFailure() from error
async def _read_varint_prefixed_bytes( stream: INetStream, timeout: float = None, ) -> bytes: try: len_payload = await asyncio.wait_for( decode_uvarint_from_stream(stream), timeout) # TODO: Catch more errors? except asyncio.TimeoutError as error: raise ReadMessageFailure( "failed to read the length of the payload") from error if len_payload > REQ_RESP_MAX_SIZE: raise ReadMessageFailure( f"size_of_payload={len_payload} is larger than maximum={REQ_RESP_MAX_SIZE}" ) try: payload = await asyncio.wait_for(stream.read(len_payload), timeout) # TODO: Catch more errors? except asyncio.TimeoutError as error: raise ReadMessageFailure("failed to read the payload") from error if len(payload) != len_payload: raise ReadMessageFailure( f"expected {len_payload} bytes, but only read {len(payload)}") return payload