2019-09-04 12:42:45 +08:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
|
|
|
|
|
|
class Closer(ABC):
|
2020-02-17 23:33:45 +08:00
|
|
|
@abstractmethod
|
2019-09-04 12:42:45 +08:00
|
|
|
async def close(self) -> None:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
class Reader(ABC):
|
|
|
|
@abstractmethod
|
2020-01-26 23:03:38 +08:00
|
|
|
async def read(self, n: int = None) -> bytes:
|
2019-09-04 12:42:45 +08:00
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
class Writer(ABC):
|
|
|
|
@abstractmethod
|
2020-02-07 18:33:15 +08:00
|
|
|
async def write(self, data: bytes) -> None:
|
2019-09-04 12:42:45 +08:00
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
class WriteCloser(Writer, Closer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ReadCloser(Reader, Closer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ReadWriter(Reader, Writer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ReadWriteCloser(Reader, Writer, Closer):
|
|
|
|
pass
|
2020-02-17 17:30:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
class MsgReader(ABC):
|
|
|
|
@abstractmethod
|
|
|
|
async def read_msg(self) -> bytes:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
class MsgWriter(ABC):
|
|
|
|
@abstractmethod
|
|
|
|
async def write_msg(self, msg: bytes) -> None:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
2020-02-17 23:33:45 +08:00
|
|
|
class MsgReadWriteCloser(MsgReader, MsgWriter, Closer):
|
2020-02-17 17:30:29 +08:00
|
|
|
pass
|
2020-02-17 19:02:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Encrypter(ABC):
|
|
|
|
@abstractmethod
|
|
|
|
def encrypt(self, data: bytes) -> bytes:
|
|
|
|
...
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def decrypt(self, data: bytes) -> bytes:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
2020-02-17 23:33:45 +08:00
|
|
|
class EncryptedMsgReadWriter(MsgReadWriteCloser, Encrypter):
|
|
|
|
"""Read/write message with encryption/decryption."""
|