Sync from GitHub main #1

Open
nomad wants to merge 145 commits from sync/from-github into main
Showing only changes of commit 240c3e9a86 - Show all commits
+18 -1
View File
@@ -5,10 +5,13 @@ import threading
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Any from typing import Any
from packet_codec import EncodedPacket
from tcp_transport import send_message
@dataclass @dataclass
class ClientSession: class ClientSession:
connection: socket.socket connection: Any
address: tuple[str, int] address: tuple[str, int]
player_id: int player_id: int
connected_at: float connected_at: float
@@ -61,6 +64,20 @@ class ClientSession:
with self._send_lock: with self._send_lock:
self.connection.sendall(payload) self.connection.sendall(payload)
def send_packet(self, encoded: EncodedPacket) -> None:
"""Send one already-serialized protocol message through the active transport.
TCP compatibility adds its newline delimiter here. Message-oriented
transports can expose ``send_encoded`` and receive the raw JSON payload
plus its delivery policy without TCP framing leaking into the protocol.
"""
with self._send_lock:
send_encoded = getattr(self.connection, "send_encoded", None)
if callable(send_encoded):
send_encoded(encoded)
return
send_message(self.connection, encoded.payload)
def to_snapshot(self) -> dict[str, Any]: def to_snapshot(self) -> dict[str, Any]:
with self._lock: with self._lock:
return { return {