mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-10 23:20:55 +00:00
Change async def write
To return `None` instead of `int. `Writer.write` *does* write all data in all use case.
This commit is contained in:
@ -14,7 +14,7 @@ class Reader(ABC):
|
|||||||
|
|
||||||
class Writer(ABC):
|
class Writer(ABC):
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def write(self, data: bytes) -> int:
|
async def write(self, data: bytes) -> None:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -31,9 +31,8 @@ class MsgIOWriter(WriteCloser):
|
|||||||
def __init__(self, write_closer: WriteCloser) -> None:
|
def __init__(self, write_closer: WriteCloser) -> None:
|
||||||
self.write_closer = write_closer
|
self.write_closer = write_closer
|
||||||
|
|
||||||
async def write(self, data: bytes) -> int:
|
async def write(self, data: bytes) -> None:
|
||||||
await self.write_msg(data)
|
await self.write_msg(data)
|
||||||
return len(data)
|
|
||||||
|
|
||||||
async def write_msg(self, msg: bytes) -> None:
|
async def write_msg(self, msg: bytes) -> None:
|
||||||
data = encode_msg_with_length(msg)
|
data = encode_msg_with_length(msg)
|
||||||
|
|||||||
@ -51,14 +51,14 @@ class NetStream(INetStream):
|
|||||||
except MuxedStreamReset as error:
|
except MuxedStreamReset as error:
|
||||||
raise StreamReset() from error
|
raise StreamReset() from error
|
||||||
|
|
||||||
async def write(self, data: bytes) -> int:
|
async def write(self, data: bytes) -> None:
|
||||||
"""
|
"""
|
||||||
write to stream.
|
write to stream.
|
||||||
|
|
||||||
:return: number of bytes written
|
:return: number of bytes written
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return await self.muxed_stream.write(data)
|
await self.muxed_stream.write(data)
|
||||||
except MuxedStreamClosed as error:
|
except MuxedStreamClosed as error:
|
||||||
raise StreamClosed() from error
|
raise StreamClosed() from error
|
||||||
|
|
||||||
|
|||||||
@ -27,8 +27,8 @@ class NoiseConnection(BaseSession):
|
|||||||
async def read(self, n: int = None) -> bytes:
|
async def read(self, n: int = None) -> bytes:
|
||||||
return await self.conn.read(n)
|
return await self.conn.read(n)
|
||||||
|
|
||||||
async def write(self, data: bytes) -> int:
|
async def write(self, data: bytes) -> None:
|
||||||
return await self.conn.write(data)
|
await self.conn.write(data)
|
||||||
|
|
||||||
async def close(self) -> None:
|
async def close(self) -> None:
|
||||||
await self.conn.close()
|
await self.conn.close()
|
||||||
|
|||||||
@ -135,9 +135,8 @@ class SecureSession(BaseSession):
|
|||||||
raise DecryptionFailedException() from e
|
raise DecryptionFailedException() from e
|
||||||
return decrypted_msg
|
return decrypted_msg
|
||||||
|
|
||||||
async def write(self, data: bytes) -> int:
|
async def write(self, data: bytes) -> None:
|
||||||
await self.write_msg(data)
|
await self.write_msg(data)
|
||||||
return len(data)
|
|
||||||
|
|
||||||
async def write_msg(self, msg: bytes) -> None:
|
async def write_msg(self, msg: bytes) -> None:
|
||||||
encrypted_data = self.local_encrypter.encrypt(msg)
|
encrypted_data = self.local_encrypter.encrypt(msg)
|
||||||
|
|||||||
@ -160,7 +160,7 @@ class Mplex(IMuxedConn):
|
|||||||
|
|
||||||
return await self.write_to_stream(_bytes)
|
return await self.write_to_stream(_bytes)
|
||||||
|
|
||||||
async def write_to_stream(self, _bytes: bytes) -> int:
|
async def write_to_stream(self, _bytes: bytes) -> None:
|
||||||
"""
|
"""
|
||||||
writes a byte array to a secured connection.
|
writes a byte array to a secured connection.
|
||||||
|
|
||||||
@ -174,8 +174,6 @@ class Mplex(IMuxedConn):
|
|||||||
"failed to write message to the underlying connection"
|
"failed to write message to the underlying connection"
|
||||||
) from e
|
) from e
|
||||||
|
|
||||||
return len(_bytes)
|
|
||||||
|
|
||||||
async def handle_incoming(self) -> None:
|
async def handle_incoming(self) -> None:
|
||||||
"""Read a message off of the secured connection and add it to the
|
"""Read a message off of the secured connection and add it to the
|
||||||
corresponding message buffer."""
|
corresponding message buffer."""
|
||||||
|
|||||||
@ -134,7 +134,7 @@ class MplexStream(IMuxedStream):
|
|||||||
self._buf = self._buf[len(payload) :]
|
self._buf = self._buf[len(payload) :]
|
||||||
return bytes(payload)
|
return bytes(payload)
|
||||||
|
|
||||||
async def write(self, data: bytes) -> int:
|
async def write(self, data: bytes) -> None:
|
||||||
"""
|
"""
|
||||||
write to stream.
|
write to stream.
|
||||||
|
|
||||||
@ -147,7 +147,7 @@ class MplexStream(IMuxedStream):
|
|||||||
if self.is_initiator
|
if self.is_initiator
|
||||||
else HeaderTags.MessageReceiver
|
else HeaderTags.MessageReceiver
|
||||||
)
|
)
|
||||||
return await self.muxed_conn.send_message(flag, data, self.stream_id)
|
await self.muxed_conn.send_message(flag, data, self.stream_id)
|
||||||
|
|
||||||
async def close(self) -> None:
|
async def close(self) -> None:
|
||||||
"""Closing a stream closes it for writing and closes the remote end for
|
"""Closing a stream closes it for writing and closes the remote end for
|
||||||
|
|||||||
Reference in New Issue
Block a user