using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using GameCult.Caching;
using LiteNetLib;
using R3;
namespace GameCult.Networking
{
///
/// Routes CultNet schema-v0 document messages through a .
///
public sealed class CultNetDatabaseServer : IDisposable
{
private readonly Server _server;
private readonly CultNetDatabase _database;
private readonly CultNetDatabaseServerOptions _options;
private readonly Func _snapshotHandler;
private readonly Func _putHandler;
private readonly Func _deleteHandler;
private readonly Func _shardCatalogHandler;
private readonly Func _subscribeHandler;
private readonly Func _unsubscribeHandler;
private readonly ConcurrentDictionary _subscriptions = new(StringComparer.Ordinal);
private bool _disposed;
///
/// Creates and attaches a database message bridge to a server.
///
public CultNetDatabaseServer(
Server server,
CultNetDatabase database,
CultNetDatabaseServerOptions? options = null)
{
_server = server ?? throw new ArgumentNullException(nameof(server));
_database = database ?? throw new ArgumentNullException(nameof(database));
_options = options ?? new CultNetDatabaseServerOptions();
_snapshotHandler = HandleSnapshotRequestAsync;
_putHandler = HandlePutAsync;
_deleteHandler = HandleDeleteAsync;
_shardCatalogHandler = HandleShardCatalogRequestAsync;
_subscribeHandler = HandleSubscribeAsync;
_unsubscribeHandler = HandleUnsubscribeAsync;
_server.OnCultNet(_snapshotHandler);
_server.OnCultNet(_putHandler);
_server.OnCultNet(_deleteHandler);
_server.OnCultNet(_shardCatalogHandler);
_server.OnCultNet(_subscribeHandler);
_server.OnCultNet(_unsubscribeHandler);
}
///
/// Gets the database used by this bridge.
///
public CultNetDatabase Database => _database;
///
/// Applies a raw put message through the database shard policy.
///
public Task