| 1 |
"""Low-level protocol stuff.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
from typing import NamedTuple |
| 5 |
from zlib import crc32 |
| 6 |
|
| 7 |
|
| 8 |
__all__ = [ |
| 9 |
"HEADER_SIZE", |
| 10 |
"RESPONSE_TYPES", |
| 11 |
"Header", |
| 12 |
"LoginRequest", |
| 13 |
"LoginResponse", |
| 14 |
"CommandRequest", |
| 15 |
"CommandResponse", |
| 16 |
"ServerMessage", |
| 17 |
"ServerMessageAck", |
| 18 |
"Request", |
| 19 |
"Response", |
| 20 |
] |
| 21 |
|
| 22 |
|
| 23 |
HEADER_SIZE = 8 |
| 24 |
PREFIX = "BE" |
| 25 |
INFIX = 0xFF |
| 26 |
|
| 27 |
|
| 28 |
class Header(NamedTuple): |
| 29 |
"""Packet header.""" |
| 30 |
|
| 31 |
crc32: int |
| 32 |
type: int |
| 33 |
|
| 34 |
def __bytes__(self): |
| 35 |
return b"".join( |
| 36 |
( |
| 37 |
PREFIX.encode("ascii"), |
| 38 |
self.crc32.to_bytes(4, "little"), |
| 39 |
INFIX.to_bytes(1, "little"), |
| 40 |
self.type.to_bytes(1, "little"), |
| 41 |
) |
| 42 |
) |
| 43 |
|
| 44 |
@classmethod |
| 45 |
def create(cls, typ: int, payload: bytes) -> Header: |
| 46 |
"""Create a header for the given payload.""" |
| 47 |
return cls( |
| 48 |
crc32( |
| 49 |
b"".join( |
| 50 |
(INFIX.to_bytes(1, "little"), typ.to_bytes(1, "little"), payload) |
| 51 |
) |
| 52 |
), |
| 53 |
typ, |
| 54 |
) |
| 55 |
|
| 56 |
@classmethod |
| 57 |
def from_bytes(cls, payload: bytes) -> Header: |
| 58 |
"""Create a header from the given bytes.""" |
| 59 |
if (size := len(payload)) != HEADER_SIZE: |
| 60 |
raise ValueError("Invalid payload size", size) |
| 61 |
|
| 62 |
if (prefix := payload[:2].decode("ascii")) != PREFIX: |
| 63 |
raise ValueError("Invalid prefix", prefix) |
| 64 |
|
| 65 |
if (infix := int.from_bytes(payload[6:7], "little")) != INFIX: |
| 66 |
raise ValueError("Invalid infix", infix) |
| 67 |
|
| 68 |
return cls( |
| 69 |
int.from_bytes(payload[2:6], "little"), |
| 70 |
int.from_bytes(payload[7:8], "little"), |
| 71 |
) |
| 72 |
|
| 73 |
|
| 74 |
class LoginRequest(str): |
| 75 |
"""Login request packet.""" |
| 76 |
|
| 77 |
def __bytes__(self): |
| 78 |
return bytes(self.header) + self.payload |
| 79 |
|
| 80 |
@property |
| 81 |
def payload(self) -> bytes: |
| 82 |
"""Return the payload.""" |
| 83 |
return self.encode("ascii") |
| 84 |
|
| 85 |
@property |
| 86 |
def header(self) -> Header: |
| 87 |
"""Return the appropriate header.""" |
| 88 |
return Header.create(0x00, self.payload) |
| 89 |
|
| 90 |
|
| 91 |
class LoginResponse(NamedTuple): |
| 92 |
"""A login response.""" |
| 93 |
|
| 94 |
header: Header |
| 95 |
success: bool |
| 96 |
|
| 97 |
@classmethod |
| 98 |
def from_bytes(cls, header: Header, payload: bytes) -> LoginResponse: |
| 99 |
"""Create a login response from the given bytes.""" |
| 100 |
return cls(header, bool(int.from_bytes(payload[:1], "little"))) |
| 101 |
|
| 102 |
|
| 103 |
class CommandRequest(NamedTuple): |
| 104 |
"""Command packet.""" |
| 105 |
|
| 106 |
seq: int |
| 107 |
command: str |
| 108 |
|
| 109 |
def __bytes__(self): |
| 110 |
return bytes(self.header) + self.payload |
| 111 |
|
| 112 |
@property |
| 113 |
def payload(self) -> bytes: |
| 114 |
"""Return the payload.""" |
| 115 |
return b"".join((self.seq.to_bytes(1, "little"), self.command.encode("ascii"))) |
| 116 |
|
| 117 |
@property |
| 118 |
def header(self) -> Header: |
| 119 |
"""Return the appropriate header.""" |
| 120 |
return Header.create(0x01, self.payload) |
| 121 |
|
| 122 |
@classmethod |
| 123 |
def from_string(cls, command: str) -> CommandRequest: |
| 124 |
"""Create a command packet from the given string.""" |
| 125 |
return cls(0x00, command) |
| 126 |
|
| 127 |
@classmethod |
| 128 |
def from_command(cls, command: str, *args: str) -> CommandRequest: |
| 129 |
"""Create a command packet from the command and arguments.""" |
| 130 |
return cls.from_string(" ".join([command, *args])) |
| 131 |
|
| 132 |
|
| 133 |
class CommandResponse(NamedTuple): |
| 134 |
"""A command response.""" |
| 135 |
|
| 136 |
header: Header |
| 137 |
seq: int |
| 138 |
payload: bytes |
| 139 |
|
| 140 |
@classmethod |
| 141 |
def from_bytes(cls, header: Header, payload: bytes) -> CommandResponse: |
| 142 |
"""Create a command response from the given bytes.""" |
| 143 |
return cls(header, int.from_bytes(payload[:1], "little"), payload[1:]) |
| 144 |
|
| 145 |
@property |
| 146 |
def message(self) -> str: |
| 147 |
"""Return the text message.""" |
| 148 |
return self.payload.decode("ascii") |
| 149 |
|
| 150 |
|
| 151 |
class ServerMessage(NamedTuple): |
| 152 |
"""A message from the server.""" |
| 153 |
|
| 154 |
header: Header |
| 155 |
seq: int |
| 156 |
payload: bytes |
| 157 |
|
| 158 |
@classmethod |
| 159 |
def from_bytes(cls, header: Header, payload: bytes) -> ServerMessage: |
| 160 |
"""Create a server message from the given bytes.""" |
| 161 |
return cls(header, int.from_bytes(payload[:1], "little"), payload[1:]) |
| 162 |
|
| 163 |
@property |
| 164 |
def message(self) -> str: |
| 165 |
"""Return the text message.""" |
| 166 |
return self.payload.decode("ascii") |
| 167 |
|
| 168 |
|
| 169 |
class ServerMessageAck(NamedTuple): |
| 170 |
"""An acknowledgement of a message from the server.""" |
| 171 |
|
| 172 |
seq: int |
| 173 |
|
| 174 |
def __bytes__(self): |
| 175 |
return bytes(self.header) + self.payload |
| 176 |
|
| 177 |
@property |
| 178 |
def header(self) -> Header: |
| 179 |
"""Return the appropriate header.""" |
| 180 |
return Header.create(0x02, self.payload) |
| 181 |
|
| 182 |
@property |
| 183 |
def payload(self) -> bytes: |
| 184 |
"""Return the payload.""" |
| 185 |
return self.seq.to_bytes(1, "little") |
| 186 |
|
| 187 |
|
| 188 |
Request = LoginRequest | CommandRequest | ServerMessageAck |
| 189 |
Response = LoginResponse | CommandResponse | ServerMessage |
| 190 |
|
| 191 |
RESPONSE_TYPES = {0x00: LoginResponse, 0x01: CommandResponse, 0x02: ServerMessage} |