py-libp2p/network/stream/net_stream.py

43 lines
1002 B
Python
Raw Normal View History

2018-11-12 05:04:57 +08:00
import asyncio
from .net_stream_interface import INetStream
class NetStream(INetStream):
def __init__(self, muxed_stream):
self.muxed_stream = muxed_stream
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 00:00:43 +08:00
async def write(self, bytes):
2018-11-12 05:04:57 +08:00
"""
write to stream
:return: number of bytes written
"""
2018-11-13 00:00:43 +08:00
return await self.muxed_stream.write(bytes)
2018-11-12 05:04:57 +08:00
def close(self):
"""
close stream
:return: true if successful
"""
self.muxed_stream.close()
return True