py-libp2p/libp2p/network/stream/net_stream_interface.py

47 lines
1.0 KiB
Python
Raw Normal View History

2018-11-12 05:04:57 +08:00
from abc import ABC, abstractmethod
2019-07-28 14:06:29 +08:00
from typing import (
Any,
Coroutine,
)
2018-11-12 05:04:57 +08:00
2019-07-28 14:30:15 +08:00
from libp2p.stream_muxer.mplex.mplex import Mplex
2019-01-10 02:38:56 +08:00
2018-11-13 02:02:49 +08:00
class INetStream(ABC):
2018-11-12 05:04:57 +08:00
2019-07-28 14:30:15 +08:00
mplex_conn: Mplex
2018-11-12 05:04:57 +08:00
@abstractmethod
2019-07-28 14:06:29 +08:00
def get_protocol(self) -> str:
2018-11-12 05:04:57 +08:00
"""
:return: protocol id that stream runs on
"""
@abstractmethod
2019-07-28 14:06:29 +08:00
def set_protocol(self, protocol_id: str) -> bool:
2018-11-12 05:04:57 +08:00
"""
:param protocol_id: protocol id that stream runs on
:return: true if successful
"""
@abstractmethod
2019-07-28 14:06:29 +08:00
def read(self) -> Coroutine[Any, Any, bytes]:
2018-11-12 05:04:57 +08:00
"""
reads from the underlying muxed_stream
2018-11-12 05:04:57 +08:00
:return: bytes of input
"""
@abstractmethod
2019-07-28 14:06:29 +08:00
def write(self, data: bytes) -> Coroutine[Any, Any, int]:
2018-11-12 05:04:57 +08:00
"""
write to the underlying muxed_stream
2018-11-12 05:04:57 +08:00
:return: number of bytes written
"""
@abstractmethod
2019-07-28 14:06:29 +08:00
def close(self) -> Coroutine[Any, Any, bool]:
2018-11-12 05:04:57 +08:00
"""
close the underlying muxed stream
2018-11-12 05:04:57 +08:00
:return: true if successful
"""