py-libp2p/libp2p/stream_muxer/mplex/mplex_stream.py

161 lines
5.1 KiB
Python
Raw Normal View History

2018-11-21 10:46:18 +08:00
import asyncio
2019-08-07 15:23:20 +08:00
from io import BytesIO
2018-11-12 07:03:04 +08:00
2019-08-05 11:22:44 +08:00
from libp2p.stream_muxer.abc import IMuxedConn, IMuxedStream
2018-11-01 05:31:00 +08:00
from .constants import HeaderTags
2019-08-02 18:28:04 +08:00
2018-11-21 09:28:41 +08:00
class MplexStream(IMuxedStream):
2018-11-01 06:02:00 +08:00
"""
reference: https://github.com/libp2p/go-mplex/blob/master/stream.go
"""
2019-08-02 18:28:04 +08:00
stream_id: int
initiator: bool
mplex_conn: IMuxedConn
2019-08-05 17:02:18 +08:00
read_deadline: int
write_deadline: int
2019-08-02 18:28:04 +08:00
local_closed: bool
remote_closed: bool
stream_lock: asyncio.Lock
2019-08-07 15:23:20 +08:00
_buf: bytes
def __init__(self, stream_id: int, initiator: bool, mplex_conn: IMuxedConn) -> None:
2018-11-12 07:03:04 +08:00
"""
create new MuxedStream in muxer
:param stream_id: stream stream id
:param initiator: boolean if this is an initiator
2018-11-21 10:46:18 +08:00
:param mplex_conn: muxed connection of this muxed_stream
2018-11-12 07:03:04 +08:00
"""
2018-11-13 02:02:49 +08:00
self.stream_id = stream_id
2018-11-12 07:03:04 +08:00
self.initiator = initiator
2018-11-21 10:46:18 +08:00
self.mplex_conn = mplex_conn
2018-11-13 02:02:49 +08:00
self.read_deadline = None
self.write_deadline = None
2018-11-12 07:03:04 +08:00
self.local_closed = False
self.remote_closed = False
2018-11-21 10:46:18 +08:00
self.stream_lock = asyncio.Lock()
2019-08-07 15:23:20 +08:00
self._buf = None
2018-11-12 07:03:04 +08:00
2019-08-07 15:23:20 +08:00
async def read(self, n: int = -1) -> bytes:
2018-11-12 07:03:04 +08:00
"""
2018-11-12 08:17:38 +08:00
read messages associated with stream from buffer til end of file
2019-08-07 15:23:20 +08:00
:param n: number of bytes to read
2018-11-12 07:03:04 +08:00
:return: bytes of input
"""
2019-08-07 15:23:20 +08:00
if n == -1:
return await self.mplex_conn.read_buffer(self.stream_id)
return await self.read_bytes(n)
async def read_bytes(self, n: int) -> bytes:
if self._buf is None:
self._buf = await self.mplex_conn.read_buffer(self.stream_id)
n_read = 0
bytes_buf = BytesIO()
while self._buf is not None and n_read < n:
n_to_read = min(n - n_read, len(self._buf))
bytes_buf.write(self._buf[:n_to_read])
if n_to_read == n - n_read:
self._buf = self._buf[n_to_read:]
else:
self._buf = None
self._buf = await self.mplex_conn.read_buffer(self.stream_id)
n_read += n_to_read
return bytes_buf.getvalue()
2018-11-01 05:31:00 +08:00
2019-08-02 18:28:04 +08:00
async def write(self, data: bytes) -> int:
2018-11-12 07:03:04 +08:00
"""
write to stream
:return: number of bytes written
"""
flag = (
HeaderTags.MessageInitiator
if self.initiator
else HeaderTags.MessageReceiver
)
2019-08-02 17:14:43 +08:00
return await self.mplex_conn.send_message(flag, data, self.stream_id)
2018-11-01 05:31:00 +08:00
2019-08-02 18:28:04 +08:00
async def close(self) -> bool:
2018-11-12 07:03:04 +08:00
"""
2018-11-21 10:46:18 +08:00
Closing a stream closes it for writing and closes the remote end for reading
but allows writing in the other direction.
2018-11-12 07:03:04 +08:00
:return: true if successful
"""
2018-11-21 13:41:13 +08:00
# TODO error handling with timeout
# TODO understand better how mutexes are used from go repo
2019-08-02 17:14:43 +08:00
flag = HeaderTags.CloseInitiator if self.initiator else HeaderTags.CloseReceiver
await self.mplex_conn.send_message(flag, None, self.stream_id)
2018-11-12 07:03:04 +08:00
2019-08-02 18:28:04 +08:00
remote_lock = False
2018-11-21 10:46:18 +08:00
async with self.stream_lock:
if self.local_closed:
return True
self.local_closed = True
remote_lock = self.remote_closed
2018-11-12 07:03:04 +08:00
2018-11-21 10:46:18 +08:00
if remote_lock:
2019-08-02 18:28:04 +08:00
# FIXME: mplex_conn has no conn_lock!
async with self.mplex_conn.conn_lock: # type: ignore
# FIXME: Don't access to buffers directly
self.mplex_conn.buffers.pop(self.stream_id) # type: ignore
2018-11-12 07:03:04 +08:00
return True
2018-11-01 05:31:00 +08:00
2019-08-02 18:28:04 +08:00
async def reset(self) -> bool:
2018-11-01 05:31:00 +08:00
"""
closes both ends of the stream
tells this remote side to hang up
2018-11-12 07:03:04 +08:00
:return: true if successful
2018-11-01 05:31:00 +08:00
"""
2018-11-21 13:41:13 +08:00
# TODO understand better how mutexes are used here
# TODO understand the difference between close and reset
2018-11-21 10:46:18 +08:00
async with self.stream_lock:
if self.remote_closed and self.local_closed:
return True
if not self.remote_closed:
flag = (
HeaderTags.ResetInitiator
if self.initiator
else HeaderTags.ResetInitiator
)
2019-08-02 17:14:43 +08:00
await self.mplex_conn.send_message(flag, None, self.stream_id)
2018-11-21 10:46:18 +08:00
self.local_closed = True
self.remote_closed = True
2019-08-02 18:28:04 +08:00
# FIXME: mplex_conn has no conn_lock!
async with self.mplex_conn.conn_lock: # type: ignore
# FIXME: Don't access to buffers directly
self.mplex_conn.buffers.pop(self.stream_id, None) # type: ignore
2018-11-21 10:46:18 +08:00
return True
2018-11-01 05:31:00 +08:00
2018-11-12 07:03:04 +08:00
# TODO deadline not in use
2019-08-05 17:02:18 +08:00
def set_deadline(self, ttl: int) -> bool:
2018-11-01 05:31:00 +08:00
"""
set deadline for muxed stream
2018-11-12 07:03:04 +08:00
:return: True if successful
2018-11-01 05:31:00 +08:00
"""
2018-11-12 07:03:04 +08:00
self.read_deadline = ttl
self.write_deadline = ttl
return True
2019-08-05 17:02:18 +08:00
def set_read_deadline(self, ttl: int) -> bool:
2018-11-12 07:03:04 +08:00
"""
set read deadline for muxed stream
:return: True if successful
"""
self.read_deadline = ttl
return True
2019-08-05 17:02:18 +08:00
def set_write_deadline(self, ttl: int) -> bool:
2018-11-12 07:03:04 +08:00
"""
set write deadline for muxed stream
:return: True if successful
"""
self.write_deadline = ttl
return True