py-libp2p/libp2p/network/stream/net_stream.py

45 lines
1.1 KiB
Python
Raw Normal View History

2018-11-12 05:04:57 +08:00
from .net_stream_interface import INetStream
2018-11-13 02:02:49 +08:00
2018-11-12 05:04:57 +08:00
class NetStream(INetStream):
def __init__(self, muxed_stream):
self.muxed_stream = muxed_stream
2019-03-03 20:34:49 +08:00
self.mplex_conn = muxed_stream.mplex_conn
2018-11-13 02:02:49 +08:00
self.protocol_id = None
2018-11-12 05:04:57 +08:00
def get_protocol(self):
"""
:return: protocol id that stream runs on
"""
return self.protocol_id
def set_protocol(self, protocol_id):
"""
:param protocol_id: protocol id that stream runs on
:return: true if successful
"""
self.protocol_id = protocol_id
2018-11-13 00:00:43 +08:00
async def read(self):
2018-11-12 05:04:57 +08:00
"""
read from stream
:return: bytes of input until EOF
"""
2018-11-13 00:00:43 +08:00
return await self.muxed_stream.read()
2018-11-12 05:04:57 +08:00
2018-11-13 02:02:49 +08:00
async def write(self, data):
2018-11-12 05:04:57 +08:00
"""
write to stream
:return: number of bytes written
"""
2018-11-13 02:02:49 +08:00
return await self.muxed_stream.write(data)
2018-11-12 05:04:57 +08:00
2018-11-19 00:22:17 +08:00
async def close(self):
2018-11-12 05:04:57 +08:00
"""
close stream
:return: true if successful
"""
2018-11-19 00:22:17 +08:00
await self.muxed_stream.close()
2018-11-12 05:04:57 +08:00
return True