py-libp2p/stream_muxer/mplex/mplex_stream.py

113 lines
3.0 KiB
Python
Raw Normal View History

2018-11-12 07:03:04 +08:00
from .constants import HEADER_TAGS
2018-11-21 09:28:41 +08:00
from ..muxed_stream_interface import IMuxedStream
2018-11-12 07:03:04 +08:00
2018-11-01 05:31:00 +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
"""
2018-11-12 07:03:04 +08:00
def __init__(self, stream_id, initiator, muxed_conn):
"""
create new MuxedStream in muxer
:param stream_id: stream stream id
:param initiator: boolean if this is an initiator
:param muxed_conn: muxed connection of this muxed_stream
"""
2018-11-13 02:02:49 +08:00
self.stream_id = stream_id
2018-11-12 07:03:04 +08:00
self.initiator = initiator
self.muxed_conn = muxed_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
def get_flag(self, action):
"""
get header flag based on action for mplex
:param action: action type in str
:return: int flag
"""
if self.initiator:
return HEADER_TAGS[action]
2018-11-13 02:02:49 +08:00
return HEADER_TAGS[action] - 1
2018-11-01 05:31:00 +08:00
2018-11-13 00:00:43 +08:00
async def read(self):
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
2018-11-12 07:03:04 +08:00
:return: bytes of input
"""
2018-11-13 02:02:49 +08:00
return await self.muxed_conn.read_buffer(self.stream_id)
2018-11-01 05:31:00 +08:00
2018-11-13 00:00:43 +08:00
async def write(self, data):
2018-11-12 07:03:04 +08:00
"""
write to stream
:return: number of bytes written
"""
2018-11-13 02:02:49 +08:00
return await self.muxed_conn.send_message(self.get_flag("MESSAGE"), data, self.stream_id)
2018-11-01 05:31:00 +08:00
2018-11-19 00:22:17 +08:00
async def close(self):
2018-11-12 07:03:04 +08:00
"""
close stream
:return: true if successful
"""
if self.local_closed and self.remote_closed:
return True
2018-11-19 00:22:17 +08:00
await self.muxed_conn.send_message(self.get_flag("CLOSE"), None, self.stream_id)
2018-11-13 02:02:49 +08:00
self.muxed_conn.streams.pop(self.stream_id)
2018-11-12 07:03:04 +08:00
self.local_closed = True
self.remote_closed = True
return True
2018-11-01 05:31:00 +08:00
def reset(self):
"""
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-12 07:03:04 +08:00
# TODO behavior not fully understood
2018-11-01 05:31:00 +08:00
pass
2018-11-12 07:03:04 +08:00
# if self.local_closed and self.remote_closed:
# return True
#
# self.muxed_conn.send_message(self.get_flag("RESET"), None, self.id)
# self.muxed_conn.streams.pop(self.id, None)
#
# self.local_closed = True
# self.remote_closed = True
#
# return True
2018-11-01 05:31:00 +08:00
2018-11-12 07:03:04 +08:00
# TODO deadline not in use
2018-11-01 05:31:00 +08:00
def set_deadline(self, ttl):
"""
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
def set_read_deadline(self, ttl):
"""
set read deadline for muxed stream
:return: True if successful
"""
self.read_deadline = ttl
return True
def set_write_deadline(self, ttl):
"""
set write deadline for muxed stream
:return: True if successful
"""
self.write_deadline = ttl
return True