process TODO.md step 9
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using Network.NetworkHost;
|
||||
using Network.NetworkTransport;
|
||||
|
||||
namespace Network.NetworkApplication
|
||||
{
|
||||
public static class NetworkIntegrationFactory
|
||||
{
|
||||
public static SharedNetworkRuntime CreateClientRuntime(
|
||||
string serverIp,
|
||||
int reliablePort,
|
||||
INetworkMessageDispatcher dispatcher,
|
||||
int? syncPort = null,
|
||||
SessionReconnectPolicy reconnectPolicy = null,
|
||||
Func<DateTimeOffset> utcNowProvider = null,
|
||||
IMessageDeliveryPolicyResolver deliveryPolicyResolver = null,
|
||||
SyncSequenceTracker syncSequenceTracker = null,
|
||||
ClockSyncState clockSync = null,
|
||||
Func<string, int, ITransport> transportFactory = null)
|
||||
{
|
||||
if (dispatcher == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(dispatcher));
|
||||
}
|
||||
|
||||
ValidateDualPortConfiguration(reliablePort, syncPort);
|
||||
|
||||
transportFactory ??= static (ip, port) => new KcpTransport(ip, port);
|
||||
|
||||
var reliableTransport = transportFactory(serverIp, reliablePort)
|
||||
?? throw new InvalidOperationException("Reliable transport factory returned null.");
|
||||
var syncTransport = syncPort.HasValue
|
||||
? transportFactory(serverIp, syncPort.Value)
|
||||
: null;
|
||||
|
||||
if (syncPort.HasValue && syncTransport == null)
|
||||
{
|
||||
throw new InvalidOperationException("Sync transport factory returned null.");
|
||||
}
|
||||
|
||||
return new SharedNetworkRuntime(
|
||||
reliableTransport,
|
||||
dispatcher,
|
||||
reconnectPolicy,
|
||||
utcNowProvider,
|
||||
syncTransport,
|
||||
deliveryPolicyResolver,
|
||||
syncSequenceTracker,
|
||||
clockSync);
|
||||
}
|
||||
|
||||
public static ServerNetworkHost CreateServerHost(
|
||||
int reliablePort,
|
||||
int? syncPort = null,
|
||||
INetworkMessageDispatcher dispatcher = null,
|
||||
SessionReconnectPolicy reconnectPolicy = null,
|
||||
Func<DateTimeOffset> utcNowProvider = null,
|
||||
IMessageDeliveryPolicyResolver deliveryPolicyResolver = null,
|
||||
SyncSequenceTracker syncSequenceTracker = null,
|
||||
Func<int, ITransport> transportFactory = null)
|
||||
{
|
||||
ValidateDualPortConfiguration(reliablePort, syncPort);
|
||||
|
||||
transportFactory ??= static port => new KcpTransport(port);
|
||||
|
||||
var reliableTransport = transportFactory(reliablePort)
|
||||
?? throw new InvalidOperationException("Reliable transport factory returned null.");
|
||||
var syncTransport = syncPort.HasValue
|
||||
? transportFactory(syncPort.Value)
|
||||
: null;
|
||||
|
||||
if (syncPort.HasValue && syncTransport == null)
|
||||
{
|
||||
throw new InvalidOperationException("Sync transport factory returned null.");
|
||||
}
|
||||
|
||||
return new ServerNetworkHost(
|
||||
reliableTransport,
|
||||
dispatcher,
|
||||
reconnectPolicy,
|
||||
utcNowProvider,
|
||||
syncTransport,
|
||||
deliveryPolicyResolver,
|
||||
syncSequenceTracker);
|
||||
}
|
||||
|
||||
private static void ValidateDualPortConfiguration(int reliablePort, int? syncPort)
|
||||
{
|
||||
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: bfefd18b5face1d4496541e42b3a011b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -3,13 +3,15 @@ using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Network.Defines;
|
||||
using Network.NetworkApplication;
|
||||
using Network.NetworkTransport;
|
||||
using UnityEngine;
|
||||
using Vector3 = UnityEngine.Vector3;
|
||||
|
||||
public class NetworkManager : MonoBehaviour
|
||||
{
|
||||
private const int MaxNetworkMessagesPerFrame = 32;
|
||||
private const string DefaultServerIp = "127.0.0.1";
|
||||
private const int DefaultReliablePort = 8080;
|
||||
private const int DefaultSyncPort = 8081;
|
||||
|
||||
public static NetworkManager Instance;
|
||||
private SharedNetworkRuntime _networkRuntime;
|
||||
@@ -18,6 +20,9 @@ public class NetworkManager : MonoBehaviour
|
||||
private Task _networkDrainTask = Task.CompletedTask;
|
||||
[SerializeField] private GameObject _wrongWindow;
|
||||
[SerializeField] private bool _enableNetworkDiagnosticsOverlay = true;
|
||||
[SerializeField] private string _serverIp = DefaultServerIp;
|
||||
[SerializeField] private int _reliablePort = DefaultReliablePort;
|
||||
[SerializeField] private int _syncPort = DefaultSyncPort;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -28,9 +33,13 @@ public class NetworkManager : MonoBehaviour
|
||||
|
||||
private IEnumerator InitNetwork()
|
||||
{
|
||||
var transport = new KcpTransport("127.0.0.1", 8080);
|
||||
var dispatcher = new MainThreadNetworkDispatcher();
|
||||
_networkRuntime = new SharedNetworkRuntime(transport, dispatcher);
|
||||
int? syncPort = _syncPort > 0 ? _syncPort : null;
|
||||
_networkRuntime = NetworkIntegrationFactory.CreateClientRuntime(
|
||||
_serverIp,
|
||||
_reliablePort,
|
||||
dispatcher,
|
||||
syncPort: syncPort);
|
||||
_networkRuntime.LifecycleChanged += HandleLifecycleChanged;
|
||||
|
||||
var startTask = _networkRuntime.StartAsync();
|
||||
@@ -227,4 +236,4 @@ public class NetworkManager : MonoBehaviour
|
||||
_networkRuntime.MessageManager.SendMessage(request, MessageType.LogoutRequest);
|
||||
Debug.Log($"Sent logout request to player {playerId}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user