process TODO.md step6

This commit is contained in:
SepComet
2026-03-29 09:53:33 +08:00
parent d3d25c8921
commit 4826bf15a5
20 changed files with 668 additions and 10 deletions
@@ -0,0 +1,58 @@
using System;
using Network.NetworkApplication;
using Network.NetworkTransport;
namespace Network.NetworkHost
{
public sealed class ServerRuntimeConfiguration
{
public ServerRuntimeConfiguration(int reliablePort)
{
if (reliablePort <= 0)
{
throw new ArgumentOutOfRangeException(nameof(reliablePort), "Reliable port must be positive.");
}
ReliablePort = reliablePort;
}
public int ReliablePort { get; }
public int? SyncPort { get; set; }
public INetworkMessageDispatcher Dispatcher { get; set; }
public SessionReconnectPolicy ReconnectPolicy { get; set; }
public Func<DateTimeOffset> UtcNowProvider { get; set; }
public IMessageDeliveryPolicyResolver DeliveryPolicyResolver { get; set; }
public SyncSequenceTracker SyncSequenceTracker { get; set; }
public Func<int, ITransport> TransportFactory { get; set; }
internal void Validate()
{
if (ReliablePort <= 0)
{
throw new ArgumentOutOfRangeException(nameof(ReliablePort), "Reliable port must be positive.");
}
if (!SyncPort.HasValue)
{
return;
}
if (SyncPort.Value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(SyncPort), "Sync port must be positive.");
}
if (SyncPort.Value == ReliablePort)
{
throw new ArgumentException("Sync port must differ from reliable port.", nameof(SyncPort));
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f6cf6d0542534955ad0dc92dd55a5429
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,40 @@
using System;
using System.Threading.Tasks;
using Network.NetworkApplication;
namespace Network.NetworkHost
{
public static class ServerRuntimeEntryPoint
{
public static async Task<ServerRuntimeHandle> StartAsync(ServerRuntimeConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}
configuration.Validate();
var host = NetworkIntegrationFactory.CreateServerHost(
configuration.ReliablePort,
configuration.SyncPort,
configuration.Dispatcher,
configuration.ReconnectPolicy,
configuration.UtcNowProvider,
configuration.DeliveryPolicyResolver,
configuration.SyncSequenceTracker,
configuration.TransportFactory);
try
{
await host.StartAsync();
return new ServerRuntimeHandle(host);
}
catch
{
host.Stop();
throw;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4f683e87b65449fdb2ace6f6826dcc27
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using Network.NetworkApplication;
namespace Network.NetworkHost
{
public sealed class ServerRuntimeHandle : IDisposable
{
private readonly ServerNetworkHost host;
private bool isStopped;
internal ServerRuntimeHandle(ServerNetworkHost host)
{
this.host = host ?? throw new ArgumentNullException(nameof(host));
IsRunning = true;
}
public ServerNetworkHost Host => host;
public bool IsRunning { get; private set; }
public IReadOnlyList<ManagedNetworkSession> ManagedSessions => host.ManagedSessions;
public event Action<MultiSessionLifecycleEvent> LifecycleChanged
{
add => host.LifecycleChanged += value;
remove => host.LifecycleChanged -= value;
}
public Task<int> DrainPendingMessagesAsync(int maxMessages = int.MaxValue)
{
return host.DrainPendingMessagesAsync(maxMessages);
}
public void UpdateLifecycle()
{
host.UpdateLifecycle();
}
public bool TryGetSession(IPEndPoint remoteEndPoint, out ManagedNetworkSession session)
{
return host.TryGetSession(remoteEndPoint, out session);
}
public void Stop()
{
if (isStopped)
{
return;
}
isStopped = true;
IsRunning = false;
host.Stop();
}
public void Dispose()
{
Stop();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5580fbf7c9f748f3a33bc6043d14faea
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: