mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Fix msg encoding
- Change varint-prefix encode to fixedint-prefix(4 bytes) encode.
This commit is contained in:
@ -4,7 +4,7 @@ from typing import Tuple
|
||||
|
||||
from libp2p.typing import StreamReader
|
||||
|
||||
TIMEOUT = 1
|
||||
TIMEOUT = 10
|
||||
|
||||
|
||||
def encode_uvarint(number: int) -> bytes:
|
||||
@ -64,7 +64,8 @@ async def read_varint_prefixed_bytes(
|
||||
return await reader.read(len_msg)
|
||||
|
||||
|
||||
# Delimited read/write
|
||||
# Delimited read/write, used by multistream-select.
|
||||
# Reference: https://github.com/gogo/protobuf/blob/07eab6a8298cf32fac45cceaac59424f98421bbc/io/varint.go#L109-L126 # noqa: E501
|
||||
|
||||
|
||||
def encode_delim(msg_str: str) -> bytes:
|
||||
@ -75,3 +76,20 @@ def encode_delim(msg_str: str) -> bytes:
|
||||
async def read_delim(reader: StreamReader, timeout: int = TIMEOUT) -> str:
|
||||
msg_bytes = await read_varint_prefixed_bytes(reader, timeout)
|
||||
return msg_bytes.decode().rstrip()
|
||||
|
||||
|
||||
SIZE_LEN_BYTES = 4
|
||||
|
||||
# Fixed-prefixed read/write, used by "/plaintext/2.0.0".
|
||||
# Reference: https://github.com/libp2p/go-msgio/blob/d5bbf59d3c4240266b1d2e5df9dc993454c42011/num.go#L11-L33 # noqa: E501 # noqa: E501
|
||||
|
||||
|
||||
def encode_fixedint_prefixed(msg_bytes: bytes) -> bytes:
|
||||
len_prefix = len(msg_bytes).to_bytes(SIZE_LEN_BYTES, "big")
|
||||
return len_prefix + msg_bytes
|
||||
|
||||
|
||||
async def read_fixedint_prefixed(reader: StreamReader) -> bytes:
|
||||
len_bytes = await reader.read(SIZE_LEN_BYTES)
|
||||
len_int = int.from_bytes(len_bytes, "big")
|
||||
return await reader.read(len_int)
|
||||
|
||||
Reference in New Issue
Block a user