This commit is contained in:
zixuanzh
2018-11-12 13:02:49 -05:00
parent b4272918d9
commit c5c9d3e5c9
17 changed files with 66 additions and 84 deletions

View File

@ -3,6 +3,7 @@ from .utils import encode_uvarint, decode_uvarint
from .muxed_connection_interface import IMuxedConn
from .muxed_stream import MuxedStream
class MuxedConn(IMuxedConn):
"""
reference: https://github.com/libp2p/go-mplex/blob/master/multiplex.go
@ -47,6 +48,10 @@ class MuxedConn(IMuxedConn):
def open_stream(self, protocol_id, stream_id, peer_id, multi_addr):
"""
creates a new muxed_stream
:param protocol_id: protocol_id of stream
:param stream_id: stream_id of stream
:param peer_id: peer_id that stream connects to
:param multi_addr: multi_addr that stream connects to
:return: a new stream
"""
stream = MuxedStream(stream_id, multi_addr, self)

View File

@ -23,11 +23,13 @@ class IMuxedConn(ABC):
pass
@abstractmethod
def open_stream(self, protocol_id, stream_name):
def open_stream(self, protocol_id, stream_id, peer_id, multi_addr):
"""
creates a new muxed_stream
:param protocol_id: id to be associated with stream
:param stream_name: name as part of identifier
:param protocol_id: protocol_id of stream
:param stream_id: stream_id of stream
:param peer_id: peer_id that stream connects to
:param multi_addr: multi_addr that stream connects to
:return: a new stream
"""
pass

View File

@ -1,4 +1,3 @@
import asyncio
from .muxed_stream_interface import IMuxedStream
from .constants import HEADER_TAGS
@ -15,12 +14,12 @@ class MuxedStream(IMuxedStream):
:param initiator: boolean if this is an initiator
:param muxed_conn: muxed connection of this muxed_stream
"""
self.id = stream_id
self.stream_id = stream_id
self.initiator = initiator
self.muxed_conn = muxed_conn
# self.read_deadline = None
# self.write_deadline = None
self.read_deadline = None
self.write_deadline = None
self.local_closed = False
self.remote_closed = False
@ -33,22 +32,22 @@ class MuxedStream(IMuxedStream):
"""
if self.initiator:
return HEADER_TAGS[action]
else:
return HEADER_TAGS[action] - 1
return HEADER_TAGS[action] - 1
async def read(self):
"""
read messages associated with stream from buffer til end of file
:return: bytes of input
"""
return await self.muxed_conn.read_buffer(self.id)
return await self.muxed_conn.read_buffer(self.stream_id)
async def write(self, data):
"""
write to stream
:return: number of bytes written
"""
return await self.muxed_conn.send_message(self.get_flag("MESSAGE"), data, self.id)
return await self.muxed_conn.send_message(self.get_flag("MESSAGE"), data, self.stream_id)
def close(self):
"""
@ -59,8 +58,8 @@ class MuxedStream(IMuxedStream):
if self.local_closed and self.remote_closed:
return True
self.muxed_conn.send_message(self.get_flag("CLOSE"), None, self.id)
self.muxed_conn.streams.pop(self.id)
self.muxed_conn.send_message(self.get_flag("CLOSE"), None, self.stream_id)
self.muxed_conn.streams.pop(self.stream_id)
self.local_closed = True
self.remote_closed = True

View File

@ -1,8 +1,9 @@
from .muxed_stream import MuxedStream
from .muxed_connection import MuxedConn
class Multiplex(object):
"""
muxing logic currently lives in MuxedConn
reference: https://github.com/whyrusleeping/go-smux-multiplex/blob/master/multiplex.go
"""
def __init__(self, conn, initiator):