simple-message-channels/simple_message_channels/smc.py
Luke Murphy d05ac50741
Some checks failed
continuous-integration/drone/push Build is failing
Spec out basics on send/send_batch once again
2020-08-01 18:53:28 +02:00

41 lines
1.2 KiB
Python

"""Sans I/O wire protocol for Hypercore"""
from typing import List, Tuple
import attr
from pyvarint import encode, encoding_length
__all__ = ["SimpleMessageChannel"]
@attr.s(auto_attribs=True)
class SimpleMessageChannel:
"""A simple message channel."""
async def send(self, channel: int, type: int, message: bytes) -> bytes:
"""Encode a channel, type and message data to be sent.
:param channel: the message channel identifier
:param type: the type of message
:param message: the message data
"""
header = channel << 4 or type
length = len(message) + encoding_length(header)
return encode(length) + encode(header) + message
async def send_batch(self, messages: List[Tuple[int, int, bytes]]) -> bytes:
"""Encodes a series of messages into a single payload of bytes.
:param messages: Several data messages
"""
payload = b""
for (channel, type, message) in messages:
payload += await self.send(channel, type, message)
return payload
async def recv(self, data: bytes) -> Tuple[int, int, bytes]:
"""Encode a channel, type, message to be sent.
:param data: the message data
"""
pass