Harden per-client session state and socket writes

This commit is contained in:
Nomads_Reach
2026-08-15 21:01:55 -04:00
parent 7995f71b4b
commit 1ea8ece884
+17
View File
@@ -17,7 +17,10 @@ class ClientSession:
packets_received: int = 0 packets_received: int = 0
packets_sent: int = 0 packets_sent: int = 0
packets_broadcast: int = 0 packets_broadcast: int = 0
gameplay_active: bool = False
protocol_version: int = 0
_lock: Any = field(repr=False, compare=False, default_factory=threading.RLock) _lock: Any = field(repr=False, compare=False, default_factory=threading.RLock)
_send_lock: Any = field(repr=False, compare=False, default_factory=threading.Lock)
@property @property
def label(self) -> str: def label(self) -> str:
@@ -39,6 +42,18 @@ class ClientSession:
if broadcast: if broadcast:
self.packets_broadcast += 1 self.packets_broadcast += 1
def mark_gameplay_active(self, protocol_version: int) -> bool:
with self._lock:
if self.gameplay_active:
return False
self.gameplay_active = True
self.protocol_version = int(protocol_version)
return True
def send_bytes(self, payload: bytes) -> None:
with self._send_lock:
self.connection.sendall(payload)
def to_snapshot(self) -> dict[str, Any]: def to_snapshot(self) -> dict[str, Any]:
with self._lock: with self._lock:
return { return {
@@ -51,4 +66,6 @@ class ClientSession:
"packetsReceived": self.packets_received, "packetsReceived": self.packets_received,
"packetsSent": self.packets_sent, "packetsSent": self.packets_sent,
"packetsBroadcast": self.packets_broadcast, "packetsBroadcast": self.packets_broadcast,
"gameplayActive": self.gameplay_active,
"protocolVersion": self.protocol_version,
} }