2018-11-21 10:46:18 +08:00
|
|
|
import asyncio
|
2019-01-10 02:38:56 +08:00
|
|
|
|
2019-07-30 15:31:02 +08:00
|
|
|
from libp2p.stream_muxer.muxed_stream_interface import IMuxedStream
|
|
|
|
|
2019-08-02 17:14:43 +08:00
|
|
|
from .constants import HeaderTags
|
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
|
|
|
|
"""
|
|
|
|
|
2019-08-02 17:53:51 +08:00
|
|
|
def __init__(self, stream_id, initiator: bool, mplex_conn):
|
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()
|
2018-11-12 07:03:04 +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-21 10:46:18 +08:00
|
|
|
return await self.mplex_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
|
|
|
|
"""
|
2019-08-02 17:14:43 +08:00
|
|
|
flag = (
|
|
|
|
HeaderTags.MessageInitiator
|
|
|
|
if self.initiator
|
|
|
|
else HeaderTags.MessageReceiver
|
2019-08-01 06:00:12 +08:00
|
|
|
)
|
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
|
|
|
|
2018-11-19 00:22:17 +08:00
|
|
|
async def close(self):
|
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
|
|
|
|
2018-11-21 10:46:18 +08:00
|
|
|
remote_lock = ""
|
|
|
|
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:
|
|
|
|
async with self.mplex_conn.conn_lock:
|
2018-11-26 04:27:54 +08:00
|
|
|
self.mplex_conn.buffers.pop(self.stream_id)
|
2018-11-12 07:03:04 +08:00
|
|
|
|
|
|
|
return True
|
2018-11-01 05:31:00 +08:00
|
|
|
|
2018-11-21 10:46:18 +08:00
|
|
|
async def reset(self):
|
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:
|
2019-08-02 17:14:43 +08:00
|
|
|
flag = (
|
|
|
|
HeaderTags.ResetInitiator
|
|
|
|
if self.initiator
|
|
|
|
else HeaderTags.ResetInitiator
|
2019-08-01 06:00:12 +08:00
|
|
|
)
|
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
|
|
|
|
|
|
|
|
async with self.mplex_conn.conn_lock:
|
2018-11-26 04:27:54 +08:00
|
|
|
self.mplex_conn.buffers.pop(self.stream_id, None)
|
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
|
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
|