Route CultNet schema messages through database bridge

This commit is contained in:
Meta Vens 2026-05-19 17:03:31 +01:00
commit 6065bb9ad5
7 changed files with 469 additions and 4 deletions

View file

@ -52,6 +52,7 @@ namespace GameCult.Networking
private int _reconnectAttemptCount;
private readonly ConcurrentDictionary<Type, Delegate> _messageDelegates = new();
private readonly ConcurrentDictionary<Type, Delegate> _cultNetMessageDelegates = new();
private string? _sessionToken;
private string _lastHost = "localhost";
@ -137,12 +138,43 @@ namespace GameCult.Networking
else Logger.LogError("Cannot send, client is not connected!");
}
/// <summary>
/// Sends a modern CultNet schema-v0 message when the client is connected and authorized.
/// </summary>
public void SendCultNet<T>(T message) where T : ICultNetSchemaMessage
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(Client));
}
if (message == null) throw new ArgumentNullException(nameof(message));
if (Connected && _peer != null)
{
if (Verified || message is CultNetLoginMessage or CultNetRegisterMessage or CultNetVerifyMessage)
{
Logger.LogDebug($"Sending CultNet schema message {message.SchemaVersion}");
_peer.SendCultNet(message);
}
else
{
Logger.LogError("Cannot send CultNet schema message, client is not verified!");
}
}
else
{
Logger.LogError("Cannot send CultNet schema message, client is not connected!");
}
}
/// <summary>
/// Removes all registered message listeners.
/// </summary>
public void ClearMessageListeners()
{
_messageDelegates.Clear();
_cultNetMessageDelegates.Clear();
}
/// <summary>
@ -172,6 +204,29 @@ namespace GameCult.Networking
AddMessageListener(callback);
}
/// <summary>
/// Adds a listener for a modern CultNet schema-v0 message type.
/// </summary>
public void AddCultNetMessageListener<T>(Action<T> callback) where T : ICultNetSchemaMessage
{
var type = typeof(T);
_cultNetMessageDelegates.AddOrUpdate(type,
_ => callback,
(t, current) =>
{
var combined = Delegate.Combine(current, callback) as Action<T>;
return combined ?? throw new InvalidOperationException($"Failed to combine delegates for {t.Name}");
});
}
/// <summary>
/// Adds a listener for a modern CultNet schema-v0 message type.
/// </summary>
public void OnCultNet<T>(Action<T> callback) where T : ICultNetSchemaMessage
{
AddCultNetMessageListener(callback);
}
/// <summary>
/// Removes a previously registered listener for a specific message type.
/// </summary>
@ -194,6 +249,26 @@ namespace GameCult.Networking
RemoveMessageListener(callback);
}
/// <summary>
/// Removes a previously registered modern CultNet schema-v0 listener.
/// </summary>
public void RemoveCultNetMessageListener<T>(Action<T> callback) where T : ICultNetSchemaMessage
{
if (_cultNetMessageDelegates.TryGetValue(typeof(T), out var currentDelegate))
{
var newDelegate = Delegate.Remove(currentDelegate, callback) as Action<T>;
_cultNetMessageDelegates[typeof(T)] = newDelegate ?? (_ => { });
}
}
/// <summary>
/// Removes a previously registered modern CultNet schema-v0 listener.
/// </summary>
public void OffCultNet<T>(Action<T> callback) where T : ICultNetSchemaMessage
{
RemoveCultNetMessageListener(callback);
}
/// <summary>
/// Sends a login request.
/// </summary>
@ -293,6 +368,23 @@ namespace GameCult.Networking
try
{
var bytes = reader.GetRemainingBytes();
if (TryDeserializeCultNet(bytes, out var cultNetMessage))
{
Logger.LogDebug($"Received CultNet schema message {cultNetMessage.SchemaVersion}");
var cultNetType = cultNetMessage.GetType();
if (_cultNetMessageDelegates.TryGetValue(cultNetType, out var cultNetDelegate) &&
cultNetDelegate != null)
{
cultNetDelegate.DynamicInvoke(cultNetMessage);
}
else
{
Logger.LogWarning($"No listener for CultNet schema message {cultNetMessage.SchemaVersion}");
}
return;
}
var message = MessageSerialization.Deserialize<Message>(bytes);
if (LogSensitivePayloads)
{
@ -355,6 +447,20 @@ namespace GameCult.Networking
(peer, latency) => Ping = latency; //Logger($"Ping received: {latency} ms");
}
private static bool TryDeserializeCultNet(byte[] payload, out ICultNetSchemaMessage message)
{
try
{
message = CultNetSchemaMessageSerialization.Deserialize(payload);
return true;
}
catch (MessagePackSerializationException)
{
message = null!;
return false;
}
}
/// <summary>
/// Disconnects the client and suppresses automatic reconnect.
/// </summary>