Use RawConnection.read

Instead of accessing its reader and writer directly.

TODO: considering add `ReaderWriterCloser` interface and let connection
and stream inherit from it.
This commit is contained in:
mhchia
2019-08-20 18:09:36 +08:00
parent 0b466ddc86
commit ef476e555b
9 changed files with 22 additions and 31 deletions

View File

@ -23,8 +23,6 @@ class BaseSession(ISecureConn):
self.remote_permanent_pubkey = None
self.initiator = self.conn.initiator
self.writer = self.conn.writer
self.reader = self.conn.reader
# TODO clean up how this is passed around?
def next_stream_id(self) -> int:
@ -33,8 +31,8 @@ class BaseSession(ISecureConn):
async def write(self, data: bytes) -> None:
await self.conn.write(data)
async def read(self) -> bytes:
return await self.conn.read()
async def read(self, n: int = -1) -> bytes:
return await self.conn.read(n)
def close(self) -> None:
self.conn.close()

View File

@ -23,7 +23,7 @@ class InsecureSession(BaseSession):
encoded_msg_bytes = encode_fixedint_prefixed(msg_bytes)
await self.write(encoded_msg_bytes)
msg_bytes_other_side = await read_fixedint_prefixed(self.reader)
msg_bytes_other_side = await read_fixedint_prefixed(self.conn)
msg_other_side = plaintext_pb2.Exchange()
msg_other_side.ParseFromString(msg_bytes_other_side)

View File

@ -87,10 +87,6 @@ class SecurityMultistream(ABC):
:param initiator: true if we are the initiator, false otherwise
:return: selected secure transport
"""
# TODO: Is conn acceptable to multiselect/multiselect_client
# instead of stream? In go repo, they pass in a raw conn
# (https://raw.githubusercontent.com/libp2p/go-conn-security-multistream/master/ssms.go)
protocol: TProtocol
communicator = RawConnectionCommunicator(conn)
if initiator:

View File

@ -7,6 +7,7 @@ from libp2p.security.base_transport import BaseSecureTransport
from libp2p.security.insecure.transport import InsecureSession
from libp2p.security.secure_conn_interface import ISecureConn
from libp2p.transport.exceptions import SecurityUpgradeFailure
from libp2p.utils import encode_fixedint_prefixed, read_fixedint_prefixed
class SimpleSecurityTransport(BaseSecureTransport):
@ -22,8 +23,8 @@ class SimpleSecurityTransport(BaseSecureTransport):
for an inbound connection (i.e. we are not the initiator)
:return: secure connection object (that implements secure_conn_interface)
"""
await conn.write(self.key_phrase.encode())
incoming = (await conn.read()).decode()
await conn.write(encode_fixedint_prefixed(self.key_phrase.encode()))
incoming = (await read_fixedint_prefixed(conn)).decode()
if incoming != self.key_phrase:
raise SecurityUpgradeFailure(
@ -48,8 +49,8 @@ class SimpleSecurityTransport(BaseSecureTransport):
for an inbound connection (i.e. we are the initiator)
:return: secure connection object (that implements secure_conn_interface)
"""
await conn.write(self.key_phrase.encode())
incoming = (await conn.read()).decode()
await conn.write(encode_fixedint_prefixed(self.key_phrase.encode()))
incoming = (await read_fixedint_prefixed(conn)).decode()
# Force context switch, as this security transport is built for testing locally
# in a single event loop