Document CultLib substrate and schema server port

This commit is contained in:
Meta Vens 2026-06-21 16:31:50 +01:00
commit d85f3fd32a
8 changed files with 161 additions and 26 deletions

View file

@ -52,7 +52,7 @@ namespace GameCult.Networking
/// <summary>
/// Peer context for a schema-v0 RUDP server.
/// </summary>
public sealed class RudpCultNetSchemaServerPeer
public sealed class RudpCultNetSchemaServerPeer : ICultNetSchemaServerPeer
{
private readonly RudpCultNetSchemaServer _server;
@ -86,11 +86,12 @@ namespace GameCult.Networking
/// <summary>
/// Multi-peer schema-v0 host over the native CultNet RUDP transport.
/// </summary>
public sealed class RudpCultNetSchemaServer : IDisposable
public sealed class RudpCultNetSchemaServer : ICultNetSchemaServer, IDisposable
{
private readonly Socket _socket;
private readonly CultNetRudpSocketTransportServer _transport;
private readonly ConcurrentDictionary<Type, Delegate> _handlers = new();
private readonly ConcurrentDictionary<Delegate, Delegate> _schemaPeerAdapters = new();
private bool _disposed;
/// <summary>
@ -167,6 +168,41 @@ namespace GameCult.Networking
});
}
/// <inheritdoc />
public void OnCultNet<TMessage>(Func<TMessage, ICultNetSchemaServerPeer, Task> callback)
where TMessage : ICultNetSchemaMessage
{
if (callback == null) throw new ArgumentNullException(nameof(callback));
Func<TMessage, RudpCultNetSchemaServerPeer, Task> adapter = (message, peer) => callback(message, peer);
_schemaPeerAdapters[callback] = adapter;
OnCultNet(adapter);
}
/// <inheritdoc />
public void RemoveCultNetMessageListener<TMessage>(Delegate callback)
where TMessage : ICultNetSchemaMessage
{
if (_schemaPeerAdapters.TryRemove(callback, out var adapter))
{
callback = adapter;
}
if (!_handlers.TryGetValue(typeof(TMessage), out var current))
{
return;
}
var next = Delegate.Remove(current, callback);
if (next == null)
{
_handlers.TryRemove(typeof(TMessage), out _);
}
else
{
_handlers[typeof(TMessage)] = next;
}
}
/// <summary>
/// Sends a schema-v0 message to a transport peer.
/// </summary>

View file

@ -0,0 +1,35 @@
using System;
using System.Threading.Tasks;
namespace GameCult.Networking
{
/// <summary>
/// Transport-neutral schema-v0 peer surface for service bridges.
/// </summary>
public interface ICultNetSchemaServerPeer
{
/// <summary>
/// Sends a schema-v0 message to this peer.
/// </summary>
void SendCultNet<TMessage>(TMessage message)
where TMessage : ICultNetSchemaMessage;
}
/// <summary>
/// Transport-neutral schema-v0 server surface for service bridges.
/// </summary>
public interface ICultNetSchemaServer
{
/// <summary>
/// Registers a schema-v0 handler.
/// </summary>
void OnCultNet<TMessage>(Func<TMessage, ICultNetSchemaServerPeer, Task> callback)
where TMessage : ICultNetSchemaMessage;
/// <summary>
/// Removes a previously registered schema-v0 handler.
/// </summary>
void RemoveCultNetMessageListener<TMessage>(Delegate callback)
where TMessage : ICultNetSchemaMessage;
}
}

View file

@ -1,6 +1,10 @@
# GameCult.Networking
`GameCult.Networking` provides client/server messaging over LiteNetLib with encrypted credential exchange and signed session tokens.
`GameCult.Networking` provides CultNet: schema-aware client/server messaging,
database replication contracts, encrypted credential exchange, signed session
tokens, and reliable transport surfaces including CultLib's native
cross-runtime RUDP pipe. LiteNetLib remains supported as the C# production
adapter for legacy clients, but it is no longer the protocol boundary.
It is intended for game or game-service scenarios where you want a compact transport layer with a small set of built-in authentication flows rather than a full HTTP stack.
@ -12,8 +16,9 @@ just sample application messages riding that organ.
The library currently includes:
- a shared message contract model
- a client wrapper around LiteNetLib
- a server wrapper around LiteNetLib
- native RUDP packet/session contracts with reliable ordered schema frames
- client and server wrappers for the legacy LiteNetLib lane
- TCP-framed and WebSocket adapter surfaces used by non-C# runtimes
- encrypted login, register, and verify flows
- signed session-token generation and validation
- `PlayerData` integration with `CultCache`
@ -205,7 +210,7 @@ state.
The built-in flow is:
1. client connects with the shared LiteNetLib connection key
1. client connects through a configured CultNet transport profile
2. credentials are encrypted with AES-GCM using a per-message nonce
3. server validates credentials and issues a signed session token
4. client stores the encrypted session token and can send `VerifyMessage` on reconnect
@ -366,14 +371,14 @@ The client:
- automatically re-verifies using the stored signed session token
- exposes reconnect state for UI/operator surfaces
`Client.TransportProfile` and `Server.TransportProfile` describe the production
LiteNetLib lane as a `litenetlib` transport adapter with reliable ordered
`schema` and `legacy` channels. Discovery and operators can inspect the active
pipe without treating LiteNetLib as an implicit protocol truth. `NetPeer`
send helpers route through `LiteNetLibTransportConnection`, keeping outbound
schema and legacy messages behind that channel-aware adapter surface. Inbound
LiteNetLib payloads are classified through the same adapter before the client
or server dispatches schema-v0 or legacy messages.
`Client.TransportProfile` and `Server.TransportProfile` describe the active
transport as a profile with reliable ordered `schema` and, where needed,
`legacy` channels. The old C# path is now explicitly a `litenetlib` adapter,
not an implicit specification. Discovery and operators can inspect whether a
peer is using native RUDP, TCP-framed schema messages, WebSocket transport, or
LiteNetLib without changing the message contract above it. `NetPeer` send
helpers route through `LiteNetLibTransportConnection`, keeping legacy outbound
schema and union messages behind the same channel-aware adapter surface.
The native RUDP socket path also exposes `CultNetRudpReconnectLoop` for
caller-owned game or service loops. The caller reports closure, calls
@ -389,7 +394,8 @@ in `GameCult.Networking`.
- `Server` currently centers on `PlayerData` as the built-in account model.
- Message authorization is based on the built-in verify/login/register flow.
- Transport is LiteNetLib, so this is not a drop-in substitute for HTTP or WebSocket stacks.
- CultNet is not a drop-in substitute for HTTP. It is a schema-aware realtime
pipe for trusted game, mesh, and service runtimes.
Sensitive payload logging is gated. Raw message JSON should only appear when a
caller explicitly opts into diagnostic logging instead of accidentally bleeding

View file

@ -17,7 +17,7 @@ namespace GameCult.Networking
/// <summary>
/// Hosts the server-side authentication, session, and message dispatch pipeline.
/// </summary>
public class Server : IDisposable
public class Server : ICultNetSchemaServer, IDisposable
{
private const string EmailPattern =
@"^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,17})$";
@ -31,6 +31,7 @@ namespace GameCult.Networking
private readonly ConcurrentDictionary<Type, Delegate> _messageDelegates = new();
private readonly ConcurrentDictionary<Type, Delegate> _cultNetMessageDelegates = new();
private readonly ConcurrentDictionary<Type, Delegate> _cultNetServerPeerMessageDelegates = new();
private readonly ConcurrentDictionary<Delegate, Delegate> _cultNetSchemaPeerAdapters = new();
private readonly ConcurrentDictionary<long, User> _users = new();
private readonly ConcurrentDictionary<string, Queue<DateTimeOffset>> _connectionAttempts = new();
private readonly ConcurrentDictionary<string, object> _connectionAttemptLocks = new();
@ -225,6 +226,16 @@ namespace GameCult.Networking
AddCultNetMessageListener(callback);
}
/// <inheritdoc />
public void OnCultNet<T>(Func<T, ICultNetSchemaServerPeer, Task> callback)
where T : ICultNetSchemaMessage
{
if (callback == null) throw new ArgumentNullException(nameof(callback));
Func<T, CultNetServerPeer, Task> adapter = (message, peer) => callback(message, peer);
_cultNetSchemaPeerAdapters[callback] = adapter;
AddCultNetMessageListener(adapter);
}
/// <summary>
/// Sends a legacy GameCult.Networking union message through this server's per-peer LiteNetLib adapter.
/// </summary>
@ -282,6 +293,11 @@ namespace GameCult.Networking
/// </summary>
public void RemoveCultNetMessageListener<T>(Delegate callback) where T : ICultNetSchemaMessage
{
if (_cultNetSchemaPeerAdapters.TryRemove(callback, out var adapter))
{
callback = adapter;
}
if (_cultNetMessageDelegates.TryGetValue(typeof(T), out var currentDelegate))
{
var newDelegate = Delegate.Remove(currentDelegate, callback);
@ -799,7 +815,7 @@ namespace GameCult.Networking
/// <summary>
/// Transport-aware server peer context for built-in CultNet service bodies.
/// </summary>
public sealed class CultNetServerPeer
public sealed class CultNetServerPeer : ICultNetSchemaServerPeer
{
internal CultNetServerPeer(NetPeer peer, LiteNetLibTransportConnection transport)
{