Plaintext: use existing msgio reader

This commit is contained in:
mhchia 2020-02-17 23:43:00 +08:00
parent 3c2e835725
commit f0df2d189f
No known key found for this signature in database
GPG Key ID: 389EFBEA1362589A
4 changed files with 8 additions and 38 deletions

View File

@ -5,7 +5,7 @@ from that repo: "a simple package to r/w length-delimited slices."
NOTE: currently missing the capability to indicate lengths by "varint" method. NOTE: currently missing the capability to indicate lengths by "varint" method.
""" """
# TODO unify w/ https://github.com/libp2p/py-libp2p/blob/1aed52856f56a4b791696bbcbac31b5f9c2e88c9/libp2p/utils.py#L85-L99 # noqa: E501
from typing import Optional from typing import Optional
from libp2p.io.abc import MsgReadWriteCloser, Reader, ReadWriteCloser from libp2p.io.abc import MsgReadWriteCloser, Reader, ReadWriteCloser

View File

@ -1,19 +0,0 @@
from libp2p.io.abc import MsgReadWriteCloser, ReadWriteCloser
from libp2p.utils import encode_fixedint_prefixed, read_fixedint_prefixed
class PlaintextHandshakeReadWriter(MsgReadWriteCloser):
conn: ReadWriteCloser
def __init__(self, conn: ReadWriteCloser) -> None:
self.conn = conn
async def read_msg(self) -> bytes:
return await read_fixedint_prefixed(self.conn)
async def write_msg(self, msg: bytes) -> None:
encoded_msg_bytes = encode_fixedint_prefixed(msg)
await self.conn.write(encoded_msg_bytes)
async def close(self) -> None:
await self.conn.close()

View File

@ -5,6 +5,7 @@ from libp2p.crypto.keys import PrivateKey, PublicKey
from libp2p.crypto.pb import crypto_pb2 from libp2p.crypto.pb import crypto_pb2
from libp2p.crypto.serialization import deserialize_public_key from libp2p.crypto.serialization import deserialize_public_key
from libp2p.io.abc import ReadWriteCloser from libp2p.io.abc import ReadWriteCloser
from libp2p.io.msgio import BaseMsgReadWriter
from libp2p.network.connection.exceptions import RawConnError from libp2p.network.connection.exceptions import RawConnError
from libp2p.network.connection.raw_connection_interface import IRawConnection from libp2p.network.connection.raw_connection_interface import IRawConnection
from libp2p.peer.id import ID from libp2p.peer.id import ID
@ -14,7 +15,6 @@ from libp2p.security.exceptions import HandshakeFailure
from libp2p.security.secure_conn_interface import ISecureConn from libp2p.security.secure_conn_interface import ISecureConn
from libp2p.typing import TProtocol from libp2p.typing import TProtocol
from .io import PlaintextHandshakeReadWriter
from .pb import plaintext_pb2 from .pb import plaintext_pb2
# Reference: https://github.com/libp2p/go-libp2p-core/blob/master/sec/insecure/insecure.go # Reference: https://github.com/libp2p/go-libp2p-core/blob/master/sec/insecure/insecure.go
@ -22,6 +22,12 @@ from .pb import plaintext_pb2
PLAINTEXT_PROTOCOL_ID = TProtocol("/plaintext/2.0.0") PLAINTEXT_PROTOCOL_ID = TProtocol("/plaintext/2.0.0")
SIZE_PLAINTEXT_LEN_BYTES = 4
class PlaintextHandshakeReadWriter(BaseMsgReadWriter):
size_len_bytes = SIZE_PLAINTEXT_LEN_BYTES
class InsecureSession(BaseSession): class InsecureSession(BaseSession):
def __init__( def __init__(

View File

@ -78,20 +78,3 @@ async def read_delim(reader: Reader) -> bytes:
f'`msg_bytes` is not delimited by b"\\n": `msg_bytes`={msg_bytes!r}' f'`msg_bytes` is not delimited by b"\\n": `msg_bytes`={msg_bytes!r}'
) )
return msg_bytes[:-1] return msg_bytes[:-1]
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: Reader) -> bytes:
len_bytes = await reader.read(SIZE_LEN_BYTES)
len_int = int.from_bytes(len_bytes, "big")
return await reader.read(len_int)