2018-11-12 06:15:55 +08:00
|
|
|
import asyncio
|
2018-11-12 06:38:11 +08:00
|
|
|
from .utils import encode_uvarint, decode_uvarint
|
2018-11-01 06:02:00 +08:00
|
|
|
from .muxed_connection_interface import IMuxedConn
|
2018-11-12 00:52:26 +08:00
|
|
|
from transport.stream.Stream import Stream
|
2018-11-01 05:31:00 +08:00
|
|
|
|
2018-11-01 06:02:00 +08:00
|
|
|
class MuxedConn(IMuxedConn):
|
2018-11-01 05:31:00 +08:00
|
|
|
"""
|
|
|
|
reference: https://github.com/libp2p/go-mplex/blob/master/multiplex.go
|
|
|
|
"""
|
|
|
|
def __init__(self, conn, initiator):
|
|
|
|
"""
|
|
|
|
create a new muxed connection
|
|
|
|
:param conn: an instance of raw connection
|
|
|
|
:param initiator: boolean to prevent multiplex with self
|
|
|
|
"""
|
|
|
|
self.raw_conn = conn
|
|
|
|
self.initiator = initiator
|
2018-11-12 06:15:55 +08:00
|
|
|
self.buffers = {}
|
|
|
|
self.streams = {}
|
|
|
|
|
|
|
|
self.add_incoming_task()
|
2018-11-01 05:31:00 +08:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""
|
|
|
|
close the stream muxer and underlying raw connection
|
|
|
|
"""
|
2018-11-12 06:15:55 +08:00
|
|
|
self.raw_conn.close()
|
2018-11-01 05:31:00 +08:00
|
|
|
|
|
|
|
def is_closed(self):
|
|
|
|
"""
|
|
|
|
check connection is fully closed
|
|
|
|
:return: true if successful
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2018-11-12 06:15:55 +08:00
|
|
|
def open_stream(self, protocol_id, stream_id, peer_id, multi_addr):
|
2018-11-01 05:31:00 +08:00
|
|
|
"""
|
|
|
|
creates a new muxed_stream
|
|
|
|
:return: a new stream
|
|
|
|
"""
|
2018-11-12 06:15:55 +08:00
|
|
|
stream = Stream(peer_id, multi_addr, self)
|
|
|
|
self.streams[stream_id] = stream
|
|
|
|
self.buffers[stream_id] = bytearray()
|
|
|
|
return stream
|
2018-11-12 00:52:26 +08:00
|
|
|
|
2018-11-01 05:31:00 +08:00
|
|
|
|
|
|
|
def accept_stream(self):
|
|
|
|
"""
|
|
|
|
accepts a muxed stream opened by the other end
|
|
|
|
:return: the accepted stream
|
|
|
|
"""
|
2018-11-01 06:02:00 +08:00
|
|
|
pass
|
2018-11-12 06:15:55 +08:00
|
|
|
|
2018-11-12 06:38:11 +08:00
|
|
|
def send_message(self, flag, data, stream_id):
|
2018-11-12 06:15:55 +08:00
|
|
|
"""
|
|
|
|
sends a message over the connection
|
|
|
|
:param header: header to use
|
|
|
|
:param data: data to send in the message
|
2018-11-12 06:38:11 +08:00
|
|
|
:param stream_id: stream the message is in
|
2018-11-12 06:15:55 +08:00
|
|
|
:return: True if success
|
|
|
|
"""
|
2018-11-12 06:38:11 +08:00
|
|
|
# << by 3, then or with flag
|
|
|
|
header = (stream_id << 3) | flag
|
|
|
|
header = encode_uvarint(header)
|
|
|
|
data_length = encode_uvarint(len(data))
|
|
|
|
_bytes = header + data_length + data
|
|
|
|
return self.write_to_stream(_bytes)
|
|
|
|
|
|
|
|
async def write_to_stream(self, _bytes):
|
|
|
|
to_return = self.raw_conn.writer.write(_bytes)
|
|
|
|
await self.raw_conn.writer.drain()
|
|
|
|
return to_return
|
2018-11-12 06:15:55 +08:00
|
|
|
|
|
|
|
async def handle_incoming(self):
|
|
|
|
data = bytearray()
|
|
|
|
while True:
|
|
|
|
chunk = self.raw_conn.reader.read(100)
|
|
|
|
if not chunk:
|
|
|
|
break
|
|
|
|
data += chunk
|
|
|
|
|
|
|
|
# Read header
|
|
|
|
# Read message length
|
|
|
|
# Read message into corresponding buffer
|
|
|
|
|
|
|
|
|
|
|
|
def add_incoming_task(self):
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
handle_incoming_task = loop.create_task(self.handle_incoming())
|
|
|
|
handle_incoming_task.add_done_callback(self.add_incoming_task)
|