py-libp2p/muxer/mplex/smux_multiplex.py

45 lines
1.2 KiB
Python
Raw Normal View History

2018-11-01 05:31:00 +08:00
from .muxed_stream import MuxedStream
from .muxed_connection import MuxedConn
class Multiplex(object):
"""
reference: https://github.com/whyrusleeping/go-smux-multiplex/blob/master/multiplex.go
"""
def __init__(self, conn, initiator):
"""
:param conn: an instance of raw connection
2018-11-12 08:31:22 +08:00
:param initiator: boolean to prevent multiplex with self
2018-11-01 05:31:00 +08:00
"""
self.muxed_conn = MuxedConn(conn, initiator)
def close(self):
"""
close the stream muxer and underlying raw connection
"""
return self.muxed_conn.close()
def is_closed(self):
"""
check connection is fully closed
:return: true if successful
"""
return self.muxed_conn.is_closed()
2018-11-01 06:02:00 +08:00
def open_stream(self, protocol_id, stream_name):
2018-11-01 05:31:00 +08:00
"""
creates a new muxed_stream
:return: a new stream
"""
2018-11-01 06:02:00 +08:00
return self.muxed_conn.open_stream(protocol_id, stream_name)
2018-11-01 05:31:00 +08:00
def accept_stream(self, _muxed_stream):
2018-11-01 06:02:00 +08:00
"""
accepts a muxed stream opened by the other end
:param _muxed_stream: stream to be accepted
2018-11-01 05:31:00 +08:00
:return: the accepted stream
2018-11-01 06:02:00 +08:00
"""
2018-11-01 05:31:00 +08:00
pass
# def new_conn(raw_conn, is_server):
# pass