Add Python CultMesh RUDP factories
This commit is contained in:
parent
3deabd706d
commit
44c010a867
6 changed files with 189 additions and 8 deletions
|
|
@ -299,7 +299,10 @@ Current progress:
|
|||
- Python's branded `CultMesh` facade now creates schema catalogs, built-in
|
||||
schema catalogs, and shard catalogs by delegating to the existing
|
||||
`cultnet_py` owners, matching the TypeScript/Kotlin/Rust entrypoint pattern
|
||||
without creating a second catalog authority.
|
||||
without creating a second catalog authority. The facade now also parses
|
||||
`rudp://host:port` endpoints and creates RUDP client/server transports from
|
||||
endpoint or peer-card contact hints while leaving packet/session semantics in
|
||||
`cultnet_py`.
|
||||
- Rust now has a poll-driven `CultNetRudpReconnectLoop` for substrate callers:
|
||||
service loops report closure, ask `reconnect_if_due(now_ms)` inside their own
|
||||
reactor, and the shared controller owns retry delay/exhaustion state while the
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ CultMesh.
|
|||
| Runtime | Role | Claimed parity | Not claimed |
|
||||
| --- | --- | --- | --- |
|
||||
| C# | Reference implementation and production game/server substrate | CultCache typed documents and SoA, CultNet schema-v0, LiteNetLib production path with adapter profile, shared transport profiles, shared RUDP socket transport, RUDP reconnect policy/controller and reconnect-loop helper, LiteNetLib client reconnect-controller adoption, schema-client transport port for service request/response clients, CultMesh node/session/catalog/stream/simulation surfaces | No need to abandon LiteNetLib before the CultNet adapter fully owns production service adoption |
|
||||
| Python | Package runtime, local daemon endpoint, raw interop peer, typed state participant | CultCache/CultNet/CultMesh package surface, branded CultMesh node/schema/shard/Verse/peer/authority/stream ergonomics, shard catalogs/logs, schema-transport factory for raw client request/subscription flows, local TCP CultMesh server, session/prediction/simulation fact helpers, RUDP codec/session/socket transport, RUDP reconnect policy/controller and reconnect-loop helper, schema-v0 MessagePack over RUDP evidence, benchmark evidence against C# cache paths | Universal C# throughput, full C# daemon/server stack, and RUDP as the only public service pipe |
|
||||
| Python | Package runtime, local daemon endpoint, raw interop peer, typed state participant | CultCache/CultNet/CultMesh package surface, branded CultMesh node/schema/shard/Verse/peer/authority/stream/RUDP ergonomics, shard catalogs/logs, schema-transport factory for raw client request/subscription flows, local TCP CultMesh server, session/prediction/simulation fact helpers, RUDP codec/session/socket transport, RUDP reconnect policy/controller and reconnect-loop helper, schema-v0 MessagePack over RUDP evidence, benchmark evidence against C# cache paths | Universal C# throughput, full C# daemon/server stack, and RUDP as the only public service pipe |
|
||||
| TypeScript | Node/browser-adjacent projection and interop runtime | CultCache parity tests, CultNet schema-v0 peer, advertised schema/shard catalog wire contracts plus request/sync helpers, transport profiles, TCP-framed transport port and peer helper, shared RUDP socket transport, RUDP reconnect policy/controller, RUDP reconnect-loop helper, branded CultMesh node/schema/shard/Verse/peer/authority/stream/RUDP ergonomics | Full C# game session/server stack |
|
||||
| Rust | Low-level cache/transport/runtime substrate | CultCache/CultNet interop lane, schema and shard catalog helpers, transport profiles, TCP-framed port, shared RUDP codec/session/socket transport, RUDP reconnect policy/controller and reconnect-loop helper, schema-v0 MessagePack over RUDP evidence, small CultMesh facade for schema registry, shard catalog, peer cards, authority leases, RUDP endpoint parsing, and peer-card RUDP client construction | Full CultMesh game-session/server facade, stream catalog, or production game-session server |
|
||||
| Kotlin | JVM/Android client substrate | Typed MessagePack cache, built-in schema catalog advertisement, schema/shard catalogs with shared schema-message transport fetch/sync helpers, authority leases, stream catalog, branded `CultMesh` facade, WebSocket transport profile/connection helper, RUDP client/server sugar, RUDP reconnect policy/controller and reconnect-loop helper, schema-v0 MessagePack over RUDP evidence | C# SoA store, full C# server daemon, or Unity hot-loop memory layout |
|
||||
|
|
|
|||
|
|
@ -129,11 +129,13 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti.
|
|||
Exposes C#-matching client entrypoints for Verse discovery, peer exchange,
|
||||
raw CultNet client construction, local nodes, Verse catalogs, peer catalogs,
|
||||
schema catalogs, built-in schema catalogs, shard catalogs, sessions, streams,
|
||||
authority leases, local TCP node serving, simulation fact commits, and the
|
||||
durable shard-log node option that attaches a file-backed authoritative log
|
||||
store beside the cache file by default. Schema and shard catalog factories
|
||||
delegate to the existing `cultnet_py` owners, so the branded entrypoint does
|
||||
not create a second source of discovery truth. The `cultmesh-py-daemon` /
|
||||
authority leases, local TCP node serving, simulation fact commits, RUDP
|
||||
endpoint parsing, and RUDP client/server construction from endpoint or
|
||||
peer-card contact hints. The durable shard-log node option attaches a
|
||||
file-backed authoritative log store beside the cache file by default. Schema,
|
||||
shard, and RUDP factories delegate to the existing `cultnet_py` owners, so the
|
||||
branded entrypoint does not create a second source of discovery or transport
|
||||
truth. The `cultmesh-py-daemon` /
|
||||
`python -m cultmesh_py.daemon` entrypoint launches that same local TCP server
|
||||
body and emits a bounded readiness document for process supervisors while the
|
||||
served CultNet endpoint remains the capability owner. Readiness now mirrors
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ from .client import (
|
|||
CultMeshVerseDiscoveryClient,
|
||||
)
|
||||
from .daemon import READY_SCHEMA_VERSION, daemon_ready_document
|
||||
from .facade import CultMesh
|
||||
from .facade import CultMesh, CultMeshRudpEndpoint
|
||||
from .node import CultMeshDatabase, CultMeshDatabaseChange, CultMeshNode, CultMeshNodeOptions, create_node
|
||||
from .server import CultMeshLocalServer
|
||||
from .session import (
|
||||
|
|
@ -62,6 +62,7 @@ __all__ = [
|
|||
"CultMeshPeerCard",
|
||||
"CultMeshPeerCatalog",
|
||||
"CultMeshPrediction",
|
||||
"CultMeshRudpEndpoint",
|
||||
"CultMeshSessionChange",
|
||||
"CultMeshSimulationFact",
|
||||
"CultMeshSimulationFactCommit",
|
||||
|
|
|
|||
|
|
@ -1,9 +1,17 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import socket as socket_module
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from cultnet_py import (
|
||||
CultNetRawClient,
|
||||
CultNetReconnectPolicy,
|
||||
CultNetRudpSocketMode,
|
||||
CultNetRudpSocketTransportConnection,
|
||||
CultNetRudpSocketTransportOptions,
|
||||
CultNetSchemaCatalog,
|
||||
CultNetShardCatalog,
|
||||
CultNetSimulationObservationHub,
|
||||
|
|
@ -18,12 +26,20 @@ from .simulation import CultMeshSimulationFactCommitter
|
|||
from .wire import (
|
||||
AuthorityLeaseVerifier,
|
||||
CultMeshAuthorityLeaseCatalog,
|
||||
CultMeshPeerCard,
|
||||
CultMeshPeerCatalog,
|
||||
CultMeshStreamCatalog,
|
||||
CultMeshVerseCatalog,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CultMeshRudpEndpoint:
|
||||
host: str
|
||||
port: int
|
||||
uri: str
|
||||
|
||||
|
||||
class CultMesh:
|
||||
@staticmethod
|
||||
def create_node(
|
||||
|
|
@ -103,6 +119,113 @@ class CultMesh:
|
|||
def create_shard_catalog() -> CultNetShardCatalog:
|
||||
return CultNetShardCatalog()
|
||||
|
||||
@staticmethod
|
||||
def parse_rudp_endpoint(endpoint: str) -> CultMeshRudpEndpoint:
|
||||
if not endpoint:
|
||||
raise ValueError("RUDP endpoint must be non-empty")
|
||||
parsed = urlparse(endpoint)
|
||||
if parsed.scheme.lower() != "rudp":
|
||||
raise ValueError("RUDP endpoint must use the rudp:// scheme")
|
||||
if not parsed.hostname or parsed.port is None:
|
||||
raise ValueError("RUDP endpoint must include a host and port")
|
||||
if parsed.port <= 0 or parsed.port > 65535:
|
||||
raise ValueError("RUDP endpoint port must be between 1 and 65535")
|
||||
host = parsed.hostname
|
||||
uri_host = f"[{host}]" if ":" in host and not host.startswith("[") else host
|
||||
return CultMeshRudpEndpoint(
|
||||
host=host,
|
||||
port=parsed.port,
|
||||
uri=f"rudp://{uri_host}:{parsed.port}",
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def create_rudp_server(
|
||||
runtime_id: str,
|
||||
connection_id: int,
|
||||
*,
|
||||
bind_host: str = "127.0.0.1",
|
||||
bind_port: int = 0,
|
||||
socket: socket_module.socket | None = None,
|
||||
initial_sequence: int = 1,
|
||||
resend_delay_ms: int = 250,
|
||||
transport_id: str = "rudp",
|
||||
max_payload_bytes: int | None = None,
|
||||
max_fragment_bytes: int | None = None,
|
||||
max_pending_reliable_packets: int | None = None,
|
||||
reconnect_policy: CultNetReconnectPolicy | dict[str, Any] | None = None,
|
||||
) -> CultNetRudpSocketTransportConnection:
|
||||
transport_socket = socket or _bind_rudp_socket(bind_host, bind_port)
|
||||
return CultNetRudpSocketTransportConnection(
|
||||
CultNetRudpSocketTransportOptions(
|
||||
runtime_id=runtime_id,
|
||||
socket=transport_socket,
|
||||
mode=CultNetRudpSocketMode.SERVER,
|
||||
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,
|
||||
reconnect_policy=reconnect_policy,
|
||||
)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def create_rudp_client(
|
||||
runtime_id: str,
|
||||
connection_id: int,
|
||||
endpoint: str | CultMeshRudpEndpoint,
|
||||
*,
|
||||
bind_host: str = "127.0.0.1",
|
||||
bind_port: int = 0,
|
||||
socket: socket_module.socket | None = None,
|
||||
initial_sequence: int = 1,
|
||||
resend_delay_ms: int = 250,
|
||||
transport_id: str = "rudp",
|
||||
max_payload_bytes: int | None = None,
|
||||
max_fragment_bytes: int | None = None,
|
||||
max_pending_reliable_packets: int | None = None,
|
||||
reconnect_policy: CultNetReconnectPolicy | dict[str, Any] | None = None,
|
||||
) -> CultNetRudpSocketTransportConnection:
|
||||
parsed_endpoint = (
|
||||
CultMesh.parse_rudp_endpoint(endpoint)
|
||||
if isinstance(endpoint, str)
|
||||
else endpoint
|
||||
)
|
||||
transport_socket = socket or _bind_rudp_socket(bind_host, bind_port)
|
||||
return CultNetRudpSocketTransportConnection(
|
||||
CultNetRudpSocketTransportOptions(
|
||||
runtime_id=runtime_id,
|
||||
socket=transport_socket,
|
||||
mode=CultNetRudpSocketMode.CLIENT,
|
||||
remote_addr=(parsed_endpoint.host, parsed_endpoint.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,
|
||||
reconnect_policy=reconnect_policy,
|
||||
)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def create_rudp_client_for_peer(
|
||||
runtime_id: str,
|
||||
connection_id: int,
|
||||
peer: CultMeshPeerCard,
|
||||
**options: Any,
|
||||
) -> CultNetRudpSocketTransportConnection:
|
||||
endpoint = next(
|
||||
(value for value in peer.endpoints if value.lower().startswith("rudp://")),
|
||||
None,
|
||||
)
|
||||
if endpoint is None:
|
||||
raise ValueError(f"Peer {peer.peer_id!r} does not advertise a RUDP endpoint")
|
||||
return CultMesh.create_rudp_client(runtime_id, connection_id, endpoint, **options)
|
||||
|
||||
@staticmethod
|
||||
def create_verse_discovery_client(
|
||||
host: str = "localhost",
|
||||
|
|
@ -174,3 +297,10 @@ class CultMesh:
|
|||
timeout_seconds: float = 4.0,
|
||||
) -> CultNetRawClient:
|
||||
return CultNetRawClient(host, port, timeout_seconds)
|
||||
|
||||
|
||||
def _bind_rudp_socket(bind_host: str, bind_port: int) -> socket_module.socket:
|
||||
transport_socket = socket_module.socket(socket_module.AF_INET, socket_module.SOCK_DGRAM)
|
||||
transport_socket.bind((bind_host, bind_port))
|
||||
transport_socket.settimeout(0.02)
|
||||
return transport_socket
|
||||
|
|
|
|||
|
|
@ -1140,6 +1140,51 @@ class CultCacheTests(unittest.TestCase):
|
|||
client.close()
|
||||
server.close()
|
||||
|
||||
def test_cultmesh_facade_creates_rudp_client_from_peer_endpoint(self) -> None:
|
||||
connection_id = 0x10203042
|
||||
server = CultMesh.create_rudp_server(
|
||||
"python-cultmesh-rudp-server",
|
||||
connection_id,
|
||||
initial_sequence=100,
|
||||
resend_delay_ms=25,
|
||||
max_fragment_bytes=1024,
|
||||
max_pending_reliable_packets=16,
|
||||
)
|
||||
server_host, server_port = server.socket.getsockname()[:2]
|
||||
endpoint = CultMesh.parse_rudp_endpoint(f"rudp://{server_host}:{server_port}")
|
||||
self.assertEqual(endpoint.host, server_host)
|
||||
self.assertEqual(endpoint.port, server_port)
|
||||
peer = CultMeshPeerCard(
|
||||
peer_id="python-cultmesh-rudp-server",
|
||||
verse_id="local",
|
||||
endpoints=(endpoint.uri,),
|
||||
roles=("schema",),
|
||||
)
|
||||
client = CultMesh.create_rudp_client_for_peer(
|
||||
"python-cultmesh-rudp-client",
|
||||
connection_id,
|
||||
peer,
|
||||
resend_delay_ms=25,
|
||||
max_fragment_bytes=1024,
|
||||
max_pending_reliable_packets=16,
|
||||
)
|
||||
|
||||
try:
|
||||
client.connect(b"join")
|
||||
pump_rudp_handshake(client, server)
|
||||
self.assertTrue(client.connected)
|
||||
self.assertTrue(server.connected)
|
||||
|
||||
client.send("schema", b"client-state")
|
||||
server_frame = receive_rudp_frame(server)
|
||||
self.assertEqual(server_frame.channel_id, "schema")
|
||||
self.assertEqual(server_frame.payload, b"client-state")
|
||||
self.assertEqual(server.profile["transports"][0]["protocol"], "rudp")
|
||||
self.assertEqual(client.profile["transports"][0]["protocol"], "rudp")
|
||||
finally:
|
||||
client.close()
|
||||
server.close()
|
||||
|
||||
def test_cultnet_database_subscription_helpers_match_schema_v0_shape(self) -> None:
|
||||
subscribe = database_subscribe(
|
||||
message_id="sub-message",
|
||||
|
|
|
|||
Loading…
Reference in a new issue