using System; using System.IO; using System.Threading.Tasks; using GameCult.Caching; using GameCult.Caching.MessagePack; using GameCult.Networking; namespace GameCult.Mesh { /// /// Options for creating a local CultMesh node. /// public sealed class CultMeshNodeOptions { /// /// Gets or sets the cache-open options used for the underlying CultCache. /// public CultCacheOpenOptions? CacheOptions { get; set; } /// /// Gets or sets the server security configuration. When omitted, development defaults are used. /// public ServerSecurityOptions? Security { get; set; } /// /// Gets or sets whether the server should start immediately after creation. /// public bool StartServer { get; set; } = true; /// /// Gets or sets the mesh database sharding options used for the hosted CultCache. /// public CultNetDatabaseOptions? DatabaseOptions { get; set; } /// /// Gets or sets whether CultMesh should attach a file-backed authoritative shard-log store when none is supplied. /// public bool EnableDurableShardLogs { get; set; } /// /// Gets or sets the directory used for file-backed authoritative shard logs. /// public string? ShardLogPath { get; set; } /// /// Gets or sets the database server bridge options used for shard routing and forwarding. /// public CultNetDatabaseServerOptions? DatabaseServerOptions { get; set; } /// /// Gets or sets an optional callback used to customize the server before start. /// public Action? ConfigureServer { get; set; } internal CultNetHostOptions ToCultNetOptions(string cachePath) { return new CultNetHostOptions { CacheOptions = CacheOptions, Security = Security, StartServer = StartServer, DatabaseOptions = CreateDatabaseOptions(cachePath), DatabaseServerOptions = DatabaseServerOptions, ConfigureServer = ConfigureServer }; } private CultNetDatabaseOptions? CreateDatabaseOptions(string cachePath) { if (!EnableDurableShardLogs) { return DatabaseOptions; } var source = DatabaseOptions ?? new CultNetDatabaseOptions(); if (source.MutationLogStore != null) { return source; } return new CultNetDatabaseOptions { RuntimeId = source.RuntimeId, Shards = source.Shards, ClientAuthorityScopes = source.ClientAuthorityScopes, DocumentRegistry = source.DocumentRegistry, MutationLogStore = new CultNetFileShardMutationLogStore(ResolveShardLogPath(cachePath)) }; } private string ResolveShardLogPath(string cachePath) { if (!string.IsNullOrWhiteSpace(ShardLogPath)) { return ShardLogPath!; } var fullPath = Path.GetFullPath(cachePath); var directory = Path.GetDirectoryName(fullPath) ?? Directory.GetCurrentDirectory(); var name = Path.GetFileNameWithoutExtension(fullPath); return Path.Combine(directory, name + ".cultmesh", "shard-logs"); } } /// /// A locally hosted CultMesh node over CultCache, CultNet transport, and the mesh database facade. /// public sealed class CultMeshNode : IDisposable { private readonly CultNetHost _host; internal CultMeshNode(CultNetHost host) { _host = host ?? throw new ArgumentNullException(nameof(host)); } /// /// Gets the durable cache used by the node. /// public CultCache Cache => _host.Cache; /// /// Gets the canonical single-file MessagePack store, when one is attached. /// public SingleFileMessagePackBackingStore? Store => _host.Store; /// /// Gets the underlying CultNet server. /// public Server Server => _host.Server; /// /// Gets the distributed realtime database facade. /// public CultNetDatabase Database => _host.Database; /// /// Gets the schema-v0 server bridge for distributed database messages. /// public CultNetDatabaseServer DatabaseServer => _host.DatabaseServer; /// /// Starts the underlying server. /// public void Start() { _host.Start(); } /// /// Flushes the durable cache. /// public Task FlushAsync(bool soft = false) { return _host.FlushAsync(soft); } /// public void Dispose() { _host.Dispose(); } } /// /// Public CultMesh entrypoints. /// public static class CultMesh { /// /// Creates a local CultMesh node with a durable CultCache and the canonical MessagePack backing store. /// public static async Task CreateNodeAsync(string cachePath, CultMeshNodeOptions? options = null) { var host = await CultNetLocal.CreateHostAsync( cachePath, (options ?? new CultMeshNodeOptions()).ToCultNetOptions(cachePath)).ConfigureAwait(false); return new CultMeshNode(host); } /// /// Creates and starts a local CultMesh node with development-friendly defaults. /// public static Task StartNodeAsync(string cachePath, CultMeshNodeOptions? options = null) { options ??= new CultMeshNodeOptions(); options.StartServer = true; return CreateNodeAsync(cachePath, options); } /// /// Creates a local reactive catalog for discovered Verses. /// public static CultMeshVerseCatalog CreateVerseCatalog() { return new CultMeshVerseCatalog(); } /// /// Creates a local reactive catalog for discovered peers. /// public static CultMeshPeerCatalog CreatePeerCatalog() { return new CultMeshPeerCatalog(); } /// /// Creates a local authority lease catalog. /// public static CultMeshAuthorityLeaseCatalog CreateAuthorityLeaseCatalog() { return new CultMeshAuthorityLeaseCatalog(); } /// /// Creates a local stream catalog for zero-copy-oriented audio, video, tensor, and byte frame streams. /// public static CultMeshStreamCatalog CreateStreamCatalog() { return new CultMeshStreamCatalog(); } /// /// Creates a committer for writing quorum simulation facts to a mesh database. /// public static CultMeshSimulationFactCommitter CreateSimulationFactCommitter(CultNetDatabase database) { return new CultMeshSimulationFactCommitter(database); } /// /// Creates a gameplay-facing session facade over a CultMesh node. /// public static CultMeshGameSession CreateGameSession( CultMeshNode node, CultMeshGameSessionOptions? options = null) { return new CultMeshGameSession(node, options); } /// /// Attaches Verse discovery responses to a CultMesh node. /// public static CultMeshVerseDiscoveryServer ServeVerseCatalog( CultMeshNode node, CultMeshVerseCatalog catalog) { if (node == null) throw new ArgumentNullException(nameof(node)); return new CultMeshVerseDiscoveryServer(node.Server, catalog); } /// /// Creates a client for fetching Verse catalogs from discovery endpoints. /// public static CultMeshVerseDiscoveryClient CreateVerseDiscoveryClient( CultMeshVerseDiscoveryClientOptions? options = null) { return new CultMeshVerseDiscoveryClient(options); } /// /// Attaches peer exchange responses to a CultMesh node. /// public static CultMeshPeerExchangeServer ServePeerExchange( CultMeshNode node, CultMeshPeerCatalog catalog) { if (node == null) throw new ArgumentNullException(nameof(node)); return new CultMeshPeerExchangeServer(node.Server, catalog); } /// /// Creates a client for fetching peer cards from peer exchange endpoints. /// public static CultMeshPeerExchangeClient CreatePeerExchangeClient( CultMeshPeerExchangeClientOptions? options = null) { return new CultMeshPeerExchangeClient(options); } /// /// Creates a CultNet client configured for CultMesh use. /// public static Client CreateClient(ClientSecurityOptions? security = null, Action? configureClient = null) { return CultNetLocal.CreateClient(security, configureClient); } /// /// Creates and connects a CultNet client to a CultMesh node. /// public static Client ConnectClient( string host = "localhost", int port = 3075, ClientSecurityOptions? security = null, Action? configureClient = null) { return CultNetLocal.ConnectClient(host, port, security, configureClient); } } }