Add Python RUDP schema client transport

This commit is contained in:
Meta Vens 2026-06-16 23:37:59 +01:00
commit ce9d709dae
8 changed files with 202 additions and 8 deletions

View file

@ -16,6 +16,7 @@ from cultnet_py import (
CultNetSchemaCatalog,
CultNetShardCatalog,
CultNetSimulationObservationHub,
create_rudp_schema_transport,
wire_message_schema_catalog,
)
@ -310,8 +311,35 @@ class CultMesh:
port: int = 3075,
*,
timeout_seconds: float = 4.0,
endpoint: str | CultMeshRudpEndpoint | None = None,
connection_id: int = 0x43554C54,
runtime_id: str = "cultmesh-python-rudp-client",
bind_host: str = "127.0.0.1",
bind_port: int = 0,
) -> CultNetRawClient:
return CultNetRawClient(host, port, timeout_seconds)
if endpoint is None and host.lower().startswith("rudp://"):
endpoint = host
if endpoint is None:
return CultNetRawClient(host, port, timeout_seconds)
parsed_endpoint = (
CultMesh.parse_rudp_endpoint(endpoint)
if isinstance(endpoint, str)
else endpoint
)
return CultNetRawClient(
parsed_endpoint.host,
parsed_endpoint.port,
timeout_seconds,
create_transport=lambda: create_rudp_schema_transport(
host=parsed_endpoint.host,
port=parsed_endpoint.port,
connection_id=connection_id,
timeout_seconds=timeout_seconds,
runtime_id=runtime_id,
bind_host=bind_host,
bind_port=bind_port,
),
)
@staticmethod
def connect_client(
@ -319,8 +347,22 @@ class CultMesh:
port: int = 3075,
*,
timeout_seconds: float = 4.0,
endpoint: str | CultMeshRudpEndpoint | None = None,
connection_id: int = 0x43554C54,
runtime_id: str = "cultmesh-python-rudp-client",
bind_host: str = "127.0.0.1",
bind_port: int = 0,
) -> CultNetRawClient:
return CultNetRawClient(host, port, timeout_seconds)
return CultMesh.create_client(
host,
port,
timeout_seconds=timeout_seconds,
endpoint=endpoint,
connection_id=connection_id,
runtime_id=runtime_id,
bind_host=bind_host,
bind_port=bind_port,
)
def _bind_rudp_socket(bind_host: str, bind_port: int) -> socket_module.socket:

View file

@ -4,6 +4,7 @@ from .client import (
CultNetRawClient,
CultNetSchemaTransport,
CultNetSchemaTransportFactory,
create_rudp_schema_transport,
create_tcp_framed_schema_transport,
)
from .framing import decode_frame, encode_frame, read_frame, write_frame
@ -170,6 +171,7 @@ __all__ = [
"compute_simulation_claim_hash",
"compute_reconnect_delay_ms",
"create_reconnect_policy",
"create_rudp_schema_transport",
"create_tcp_framed_schema_transport",
"create_rudp_transport_profile",
"create_tcp_framed_transport_profile",

View file

@ -1,6 +1,7 @@
from __future__ import annotations
import socket
import time
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any, Protocol
@ -21,7 +22,13 @@ from .shard_catalog import CultNetShardCatalog, CultNetShardDescriptor
from .shard_log import CultNetShardLogResponse
from .snapshot import CultNetRawSnapshotResponse
from .subscription import CultNetDatabaseChange
from .transport import TcpFramedTransportConnection, create_tcp_framed_transport_profile
from .transport import (
CultNetRudpSocketMode,
CultNetRudpSocketTransportConnection,
CultNetRudpSocketTransportOptions,
TcpFramedTransportConnection,
create_tcp_framed_transport_profile,
)
class CultNetSchemaTransport(Protocol):
@ -377,6 +384,55 @@ def create_tcp_framed_schema_transport(
)
def create_rudp_schema_transport(
*,
host: str,
port: int,
connection_id: int,
timeout_seconds: float = 4.0,
runtime_id: str = "cultnet-python-rudp-client",
bind_host: str = "127.0.0.1",
bind_port: int = 0,
initial_sequence: int = 1,
resend_delay_ms: int = 25,
transport_id: str = "rudp",
max_payload_bytes: int | None = None,
max_fragment_bytes: int | None = None,
max_pending_reliable_packets: int | None = None,
) -> CultNetRudpSocketTransportConnection:
transport_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
transport_socket.bind((bind_host, bind_port))
transport_socket.settimeout(min(timeout_seconds, 0.02))
transport = CultNetRudpSocketTransportConnection(
CultNetRudpSocketTransportOptions(
runtime_id=runtime_id,
socket=transport_socket,
mode=CultNetRudpSocketMode.CLIENT,
remote_addr=(host, port),
connection_id=connection_id,
initial_sequence=initial_sequence,
resend_delay_ms=resend_delay_ms,
transport_id=transport_id,
max_payload_bytes=max_payload_bytes,
max_fragment_bytes=max_fragment_bytes,
max_pending_reliable_packets=max_pending_reliable_packets,
)
)
try:
transport.connect(b"cultnet-schema")
deadline = time.monotonic() + timeout_seconds
while not transport.connected:
transport.receive_once()
transport.poll_resends()
if time.monotonic() >= deadline:
raise TimeoutError("Timed out waiting for RUDP schema transport handshake")
return transport
except Exception:
transport.close()
raise
class _OwnedTcpFramedTransportConnection(TcpFramedTransportConnection):
def __init__(self, stream: Any, connection: socket.socket, *, profile: dict[str, Any]) -> None:
super().__init__(stream, profile=profile)

View file

@ -819,6 +819,12 @@ class CultNetRudpSocketTransportConnection:
self.disconnect_reason: bytes | None = None
self.pong_payloads: deque[bytes] = deque()
def __enter__(self) -> "CultNetRudpSocketTransportConnection":
return self
def __exit__(self, exc_type: Any, exc: Any, traceback: Any) -> None:
self.close()
@property
def connected(self) -> bool:
return self.session.connected
@ -890,6 +896,16 @@ class CultNetRudpSocketTransportConnection:
self._send_packet(self.session.create_ack())
return frame
def receive(self, timeout_seconds: float | None = None) -> CultNetTransportFrame:
deadline = None if timeout_seconds is None else time.monotonic() + timeout_seconds
while True:
frame = self.receive_once()
if frame is not None:
return frame
self.poll_resends()
if deadline is not None and time.monotonic() >= deadline:
raise TimeoutError("Timed out waiting for RUDP schema frame")
def disconnect(self, reason: bytes = b"") -> None:
self._send_packet(self.session.create_disconnect(reason))