JSON backing store, I/O fixes, Networking Security
Introduced Newtonsoft.Json-backed persistence. Improved backing-store reliability and serialization across the caching stack. Improved networking security posture. Key changes: - Add new GameCult.Caching.NewtonsoftJson project with SingleFile/MultiFile backing stores and polymorphic known-type envelopes. - Add MessagePack helper serialization (DatabaseEntrySerialization) and update MessagePack backing stores to use the shared options/resolver; add DatabaseLink formatter and generator/analyzer docs. - Update CultLib.sln to include the new projects and new caching tests project. - Improve CultCache persistence behavior: track pending store operations, wait for operations on PullAll, and centralize async push handling with error logging. - Harden file I/O in multi-file/single-file stores: atomic temp-file writes, safe Replace/Move semantics, rename-safe deletes, and improved file-watcher subscription and debounce disposal. - Add README files and documentation for several subprojects and packaging projects. These changes add JSON persistence support, improve serialization consistency, and make on-disk operations more robust and observable.
This commit is contained in:
parent
d88eecb4ba
commit
0bfc85a4f2
50 changed files with 3957 additions and 325 deletions
|
|
@ -1,52 +1,89 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using GameCult.Logging;
|
||||
using LiteNetLib;
|
||||
using MessagePack;
|
||||
using MessagePack.Resolvers;
|
||||
using R3;
|
||||
|
||||
namespace GameCult.Networking
|
||||
{
|
||||
public class Client
|
||||
/// <summary>
|
||||
/// Provides client-side connection, authentication, and message dispatch logic.
|
||||
/// </summary>
|
||||
public class Client : IDisposable
|
||||
{
|
||||
public event Action<string> OnError; // Hook for important user-facing feedback (e.g. via modal UI element)
|
||||
private NetManager _client;
|
||||
private NetPeer _peer;
|
||||
/// <summary>
|
||||
/// Raised when the client encounters a user-visible error.
|
||||
/// </summary>
|
||||
public event Action<string>? OnError; // Hook for important user-facing feedback (e.g. via modal UI element)
|
||||
private NetManager? _client;
|
||||
private NetPeer? _peer;
|
||||
private IDisposable? _pollSubscription;
|
||||
private IDisposable? _reconnectSubscription;
|
||||
private bool _disposed;
|
||||
private bool _manualDisconnect;
|
||||
private bool _isReconnecting;
|
||||
|
||||
private readonly ConcurrentDictionary<Type, Delegate> _messageDelegates = new();
|
||||
|
||||
|
||||
private Guid _sessionToken;
|
||||
private string _lastHost;
|
||||
private string? _sessionToken;
|
||||
private string _lastHost = "localhost";
|
||||
private int _lastPort;
|
||||
private readonly ClientSecurityOptions _security;
|
||||
private ILogger _logger = new NullLogger();
|
||||
private static readonly TimeSpan ReconnectDelay = TimeSpan.FromSeconds(1);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the logger used by the client.
|
||||
/// </summary>
|
||||
public ILogger Logger
|
||||
{
|
||||
get => _logger;
|
||||
set => _logger = value ?? new NullLogger();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the last reported round-trip latency in milliseconds.
|
||||
/// </summary>
|
||||
public int Ping { get; private set; }
|
||||
public bool Verified => _sessionToken != Guid.Empty;
|
||||
public bool Connected => _client != null;
|
||||
|
||||
public Client()
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the client currently has a verified session.
|
||||
/// </summary>
|
||||
public bool Verified => !string.IsNullOrWhiteSpace(_sessionToken);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the network manager has been created.
|
||||
/// </summary>
|
||||
public bool Connected => _client is { IsRunning: true } && _peer != null;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new client instance.
|
||||
/// </summary>
|
||||
/// <param name="security">Validated client security configuration. This must be provided explicitly by the caller.</param>
|
||||
public Client(ClientSecurityOptions security)
|
||||
{
|
||||
_security = security ?? throw new ArgumentNullException(nameof(security));
|
||||
OnError += s => Logger.LogError(s);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a message when the client is connected and authorized to do so.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The message type.</typeparam>
|
||||
/// <param name="m">The message to send.</param>
|
||||
public void Send<T>(T m) where T : Message
|
||||
{
|
||||
if (Connected)
|
||||
if (_disposed)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(Client));
|
||||
}
|
||||
|
||||
if (Connected && _peer != null)
|
||||
{
|
||||
if (Verified || m is LoginMessage || m is RegisterMessage || m is VerifyMessage)
|
||||
{
|
||||
Logger.LogDebug($"Sending message {MessagePackSerializer.SerializeToJson(m as Message)}");
|
||||
Logger.LogDebug($"Sending message {MessagePackSerializer.SerializeToJson(m)}");
|
||||
_peer.Send(m);
|
||||
}
|
||||
else Logger.LogError("Cannot send, client is not verified!");
|
||||
|
|
@ -54,11 +91,19 @@ namespace GameCult.Networking
|
|||
else Logger.LogError("Cannot send, client is not connected!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes all registered message listeners.
|
||||
/// </summary>
|
||||
public void ClearMessageListeners()
|
||||
{
|
||||
_messageDelegates.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a listener for a specific message type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The message type to subscribe to.</typeparam>
|
||||
/// <param name="callback">The callback to invoke when the message is received.</param>
|
||||
public void AddMessageListener<T>(Action<T> callback) where T : Message
|
||||
{
|
||||
var type = typeof(T);
|
||||
|
|
@ -73,15 +118,25 @@ namespace GameCult.Networking
|
|||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a previously registered listener for a specific message type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The message type to unsubscribe from.</typeparam>
|
||||
/// <param name="callback">The callback to remove.</param>
|
||||
public void RemoveMessageListener<T>(Action<T> callback) where T : Message
|
||||
{
|
||||
if (_messageDelegates.TryGetValue(typeof(T), out var currentDelegate))
|
||||
{
|
||||
var newDelegate = Delegate.Remove(currentDelegate, callback) as Action<T>;
|
||||
_messageDelegates[typeof(T)] = newDelegate; // Null if empty
|
||||
_messageDelegates[typeof(T)] = newDelegate ?? (_ => { }); // Null if empty
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a login request.
|
||||
/// </summary>
|
||||
/// <param name="auth">The email address or username.</param>
|
||||
/// <param name="password">The plaintext password.</param>
|
||||
public void Login(string auth, string password)
|
||||
{
|
||||
if (auth.Length > 0)
|
||||
|
|
@ -92,8 +147,8 @@ namespace GameCult.Networking
|
|||
Send(new LoginMessage
|
||||
{
|
||||
Nonce = nonce,
|
||||
Auth = Secret.EncryptString(auth, nonce),
|
||||
Password = Secret.EncryptString(password, nonce)
|
||||
Auth = Secret.EncryptString(auth, nonce, _security) ?? Array.Empty<byte>(),
|
||||
Password = Secret.EncryptString(password, nonce, _security) ?? Array.Empty<byte>()
|
||||
});
|
||||
}
|
||||
else OnError?.Invoke("Password Invalid: needs to be at least 8 characters");
|
||||
|
|
@ -101,6 +156,12 @@ namespace GameCult.Networking
|
|||
else OnError?.Invoke("Email/Username Empty");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a registration request.
|
||||
/// </summary>
|
||||
/// <param name="email">The email address for the new account.</param>
|
||||
/// <param name="username">The requested username.</param>
|
||||
/// <param name="password">The plaintext password.</param>
|
||||
public void Register(string email, string username, string password)
|
||||
{
|
||||
if (email.Length > 0)
|
||||
|
|
@ -113,9 +174,9 @@ namespace GameCult.Networking
|
|||
Send(new RegisterMessage
|
||||
{
|
||||
Nonce = nonce,
|
||||
Email = Secret.EncryptString(email, nonce),
|
||||
Name = Secret.EncryptString(username, nonce),
|
||||
Password = Secret.EncryptString(password, nonce)
|
||||
Email = Secret.EncryptString(email, nonce, _security) ?? Array.Empty<byte>(),
|
||||
Name = Secret.EncryptString(username, nonce, _security) ?? Array.Empty<byte>(),
|
||||
Password = Secret.EncryptString(password, nonce, _security) ?? Array.Empty<byte>()
|
||||
});
|
||||
}
|
||||
else OnError?.Invoke("Password Invalid: needs to be at least 8 characters");
|
||||
|
|
@ -125,20 +186,35 @@ namespace GameCult.Networking
|
|||
else OnError?.Invoke("Email Empty");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Connects the client to a server and starts polling LiteNetLib events.
|
||||
/// </summary>
|
||||
/// <param name="host">The remote host name or IP address.</param>
|
||||
/// <param name="port">The remote server port.</param>
|
||||
public void Connect(string host = "localhost", int port = 3075)
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(Client));
|
||||
}
|
||||
|
||||
_manualDisconnect = false;
|
||||
_isReconnecting = false;
|
||||
_reconnectSubscription?.Dispose();
|
||||
_reconnectSubscription = null;
|
||||
_lastHost = host;
|
||||
_lastPort = port;
|
||||
|
||||
EventBasedNetListener listener = new EventBasedNetListener();
|
||||
DisposeTransport();
|
||||
|
||||
var listener = new EventBasedNetListener();
|
||||
_client = new NetManager(listener)
|
||||
{
|
||||
// UnsyncedEvents = true,
|
||||
NatPunchEnabled = true
|
||||
};
|
||||
_client.Start(3074);
|
||||
_peer = _client.Connect(host, port, Secret.ConnectionKey);
|
||||
Observable.EveryUpdate().Subscribe(_ => _client.PollEvents());
|
||||
_peer = _client.Connect(host, port, _security.ConnectionKey);
|
||||
_pollSubscription = Observable.EveryUpdate().Subscribe(_ => _client?.PollEvents());
|
||||
listener.NetworkErrorEvent += (point, code) => Logger.LogInfo($"{point.Address}: Error {code}");
|
||||
listener.NetworkReceiveEvent += (peer, reader, channel, method) =>
|
||||
{
|
||||
|
|
@ -146,27 +222,24 @@ namespace GameCult.Networking
|
|||
{
|
||||
var bytes = reader.GetRemainingBytes();
|
||||
Logger.LogDebug($"Received message: {MessagePackSerializer.ConvertToJson(new ReadOnlyMemory<byte>(bytes))}");
|
||||
var message = MessagePackSerializer.Deserialize<Message>(bytes);
|
||||
var message = MessageSerialization.Deserialize<Message>(bytes);
|
||||
var type = message.GetType();
|
||||
|
||||
if (!Verified)
|
||||
switch (message)
|
||||
{
|
||||
switch (message)
|
||||
{
|
||||
case LoginSuccessMessage loginSuccess:
|
||||
_sessionToken = new Guid(Secret.DecryptBytes(loginSuccess.Session, loginSuccess.Nonce));
|
||||
break;
|
||||
case ErrorMessage error:
|
||||
OnError?.Invoke(error.Error);
|
||||
break;
|
||||
}
|
||||
case LoginSuccessMessage loginSuccess:
|
||||
_sessionToken = Secret.DecryptString(loginSuccess.Session, loginSuccess.Nonce, _security);
|
||||
break;
|
||||
case ErrorMessage error:
|
||||
OnError?.Invoke(error.Error);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if (_messageDelegates.TryGetValue(message.GetType(), out var del) && del != null)
|
||||
if (_messageDelegates.TryGetValue(type, out var del) && del != null)
|
||||
del.DynamicInvoke(message);
|
||||
else
|
||||
Logger.LogWarning($"No listener for {message.GetType().Name}");
|
||||
Logger.LogWarning($"No listener for {type.Name}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -182,19 +255,106 @@ namespace GameCult.Networking
|
|||
if (Verified)
|
||||
{
|
||||
var nonce = Secret.NewNonce;
|
||||
peer.Send(new VerifyMessage { Nonce = nonce, Session = Secret.EncryptBytes(_sessionToken.ToByteArray(), nonce) });
|
||||
peer.Send(new VerifyMessage
|
||||
{
|
||||
Nonce = nonce,
|
||||
Session = Secret.EncryptString(_sessionToken, nonce, _security) ?? Array.Empty<byte>()
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
listener.PeerDisconnectedEvent += (peer, info) =>
|
||||
{
|
||||
Logger.LogInfo($"Peer {peer.Address}:{peer.Port} disconnected: {info.Reason}.");
|
||||
_peer = null;
|
||||
Connect(_lastHost, _lastPort);
|
||||
DisposeTransport();
|
||||
ScheduleReconnect();
|
||||
};
|
||||
|
||||
listener.NetworkLatencyUpdateEvent +=
|
||||
(peer, latency) => Ping = latency; //Logger($"Ping received: {latency} ms");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disconnects the client and suppresses automatic reconnect.
|
||||
/// </summary>
|
||||
public void Disconnect()
|
||||
{
|
||||
_manualDisconnect = true;
|
||||
_reconnectSubscription?.Dispose();
|
||||
_reconnectSubscription = null;
|
||||
DisposeTransport();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
_manualDisconnect = true;
|
||||
_reconnectSubscription?.Dispose();
|
||||
_reconnectSubscription = null;
|
||||
DisposeTransport();
|
||||
}
|
||||
|
||||
private void ScheduleReconnect()
|
||||
{
|
||||
if (_disposed || _manualDisconnect || string.IsNullOrWhiteSpace(_lastHost) || _isReconnecting)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isReconnecting = true;
|
||||
_reconnectSubscription = Observable.Timer(ReconnectDelay).Subscribe(_ =>
|
||||
{
|
||||
_isReconnecting = false;
|
||||
_reconnectSubscription?.Dispose();
|
||||
_reconnectSubscription = null;
|
||||
|
||||
try
|
||||
{
|
||||
Connect(_lastHost, _lastPort);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError($"Reconnect failed: {ex.Message}");
|
||||
ScheduleReconnect();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void DisposeTransport()
|
||||
{
|
||||
_pollSubscription?.Dispose();
|
||||
_pollSubscription = null;
|
||||
|
||||
if (_peer != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_peer.Disconnect();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Best-effort cleanup.
|
||||
}
|
||||
}
|
||||
|
||||
if (_client != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_client.Stop();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_client = null;
|
||||
_peer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue