1
This commit is contained in:
@@ -3,7 +3,8 @@
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23"
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23",
|
||||
"GUID:49b49c76ee64f6b41bf28ef951cb0e50"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
|
@@ -8,7 +8,9 @@ namespace BITKit.Net.Kcp
|
||||
public class MonoKcpClient : MonoBehaviour,INetClient,INetProvider
|
||||
{
|
||||
internal static MonoKcpClient Singleton { get; private set; }
|
||||
|
||||
|
||||
[SerializeField,ReadOnly] private int rate;
|
||||
[SerializeReference,SubclassSelector] private ITicker ticker;
|
||||
[SerializeField] private string m_host;
|
||||
[SerializeField] private ushort m_port;
|
||||
[SerializeField] private bool connectOnStart;
|
||||
@@ -18,12 +20,12 @@ namespace BITKit.Net.Kcp
|
||||
[SerializeField] private Optional<ushort> allowDebugPort;
|
||||
#endif
|
||||
|
||||
private readonly DeltaTimer timer = new();
|
||||
|
||||
private KcpNetClient client;
|
||||
private INetClient _netClientImplementation=>client;
|
||||
private INetProvider _netProviderImplementation=>client;
|
||||
|
||||
private CancellationTokenSource _cancellationTokenSource;
|
||||
|
||||
public event Action OnStartConnect
|
||||
{
|
||||
add => _netClientImplementation.OnStartConnect += value;
|
||||
@@ -49,6 +51,11 @@ namespace BITKit.Net.Kcp
|
||||
}
|
||||
|
||||
public bool IsConnected => _netClientImplementation.IsConnected;
|
||||
public bool ManualTick
|
||||
{
|
||||
get => client.ManualTick;
|
||||
set => client.ManualTick = value;
|
||||
}
|
||||
|
||||
public int Ping => _netClientImplementation.Ping;
|
||||
|
||||
@@ -61,6 +68,10 @@ namespace BITKit.Net.Kcp
|
||||
|
||||
public UniTask<bool> Connect(string address = "localhost", ushort port = 27014)
|
||||
{
|
||||
if(address is "localhost" or null)
|
||||
address = m_host;
|
||||
if(port is 27014 or 0)
|
||||
port = m_port;
|
||||
return _netClientImplementation.Connect(address, port);
|
||||
}
|
||||
|
||||
@@ -134,21 +145,14 @@ namespace BITKit.Net.Kcp
|
||||
_netProviderImplementation.HandShake();
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Singleton = this;
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
client = new KcpNetClient();
|
||||
client.OnConnectedFailed += Reconnect;
|
||||
client.OnDisconnected += Reconnect;
|
||||
}
|
||||
|
||||
|
||||
private async void Reconnect()
|
||||
{
|
||||
if (autoReconnect is false) return;
|
||||
try
|
||||
{
|
||||
await Task.Delay(1000, _cancellationTokenSource.Token);
|
||||
await Task.Delay(1000, destroyCancellationToken);
|
||||
if (client.IsConnected is false)
|
||||
{
|
||||
await client.Connect(m_host, m_port);
|
||||
@@ -159,10 +163,30 @@ namespace BITKit.Net.Kcp
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Singleton = this;
|
||||
client = new KcpNetClient();
|
||||
client.OnConnectedFailed += Reconnect;
|
||||
client.OnDisconnected += Reconnect;
|
||||
}
|
||||
private void Start()
|
||||
{
|
||||
if (!connectOnStart) return;
|
||||
|
||||
|
||||
if (ticker is not null)
|
||||
{
|
||||
ManualTick = true;
|
||||
ticker.Add(OnTick);
|
||||
}
|
||||
destroyCancellationToken.Register(() =>
|
||||
{
|
||||
if (IsConnected)
|
||||
Disconnect();
|
||||
ticker?.Remove(OnTick);
|
||||
});
|
||||
|
||||
if (!connectOnStart) return;
|
||||
#if UNITY_EDITOR
|
||||
var _host = allowDebugHost.Allow ? allowDebugHost.Value : m_host;
|
||||
var _port = allowDebugPort.Allow ? allowDebugPort.Value : m_port;
|
||||
@@ -172,13 +196,23 @@ namespace BITKit.Net.Kcp
|
||||
#endif
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
private void OnTick(float obj)
|
||||
{
|
||||
_cancellationTokenSource.Cancel();
|
||||
if (client.IsConnected)
|
||||
{
|
||||
client.Disconnect();
|
||||
}
|
||||
timer.Update(obj);
|
||||
rate = timer;
|
||||
Tick();
|
||||
}
|
||||
[BIT]
|
||||
private void EditorConnect()
|
||||
{
|
||||
BITAppForUnity.ThrowIfNotPlaying();
|
||||
Connect(m_host, m_port).Forget();
|
||||
}
|
||||
[BIT]
|
||||
private void EditorDisconnect()
|
||||
{
|
||||
BITAppForUnity.ThrowIfNotPlaying();
|
||||
Disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ namespace BITKit.Net.Kcp
|
||||
[Serializable]
|
||||
public class MonoKcpClientSingleton : INetProvider,INetClient
|
||||
{
|
||||
private bool _manualTick;
|
||||
private INetClient _netClientImplementation => MonoKcpClient.Singleton;
|
||||
private INetProvider _netProviderImplementation => MonoKcpClient.Singleton;
|
||||
|
||||
@@ -35,6 +36,12 @@ namespace BITKit.Net.Kcp
|
||||
|
||||
public bool IsConnected => _netClientImplementation.IsConnected;
|
||||
|
||||
public bool ManualTick
|
||||
{
|
||||
get => _netClientImplementation.ManualTick;
|
||||
set => _netClientImplementation.ManualTick = value;
|
||||
}
|
||||
|
||||
public int Ping => _netClientImplementation.Ping;
|
||||
|
||||
public int Id => _netClientImplementation.Id;
|
||||
|
186
Src/Unity/Scripts/NetProvider/Kcp/MonoKcpServer.cs
Normal file
186
Src/Unity/Scripts/NetProvider/Kcp/MonoKcpServer.cs
Normal file
@@ -0,0 +1,186 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.Net.Kcp
|
||||
{
|
||||
public class MonoKcpServer : MonoBehaviour,INetServer,INetProvider
|
||||
{
|
||||
[SerializeField, ReadOnly] private int rate;
|
||||
[SerializeField] private ushort initialPort;
|
||||
[SerializeReference,SubclassSelector] private ITicker ticker;
|
||||
|
||||
private INetServer _netServerImplementation => _server;
|
||||
private KCPNetServer _server=>_serverInstance??=new KCPNetServer();
|
||||
private KCPNetServer _serverInstance;
|
||||
|
||||
private readonly DeltaTimer timer = new();
|
||||
public bool ManualTick
|
||||
{
|
||||
get => _netServerImplementation.ManualTick;
|
||||
set => _netServerImplementation.ManualTick = value;
|
||||
}
|
||||
public event Action<int> OnClientConnected
|
||||
{
|
||||
add => _netServerImplementation.OnClientConnected += value;
|
||||
remove => _netServerImplementation.OnClientConnected -= value;
|
||||
}
|
||||
|
||||
public event Action<int> OnClientDisconnected
|
||||
{
|
||||
add => _netServerImplementation.OnClientDisconnected += value;
|
||||
remove => _netServerImplementation.OnClientDisconnected -= value;
|
||||
}
|
||||
|
||||
public event Action OnStartServer
|
||||
{
|
||||
add => _netServerImplementation.OnStartServer += value;
|
||||
remove => _netServerImplementation.OnStartServer -= value;
|
||||
}
|
||||
|
||||
public event Action OnStopServer
|
||||
{
|
||||
add => _netServerImplementation.OnStopServer += value;
|
||||
remove => _netServerImplementation.OnStopServer -= value;
|
||||
}
|
||||
public void StartServer(ushort port = 27014)
|
||||
{
|
||||
if(port == 27014)port = initialPort;
|
||||
_netServerImplementation.StartServer(port);
|
||||
}
|
||||
public void StopServer(bool dispose = false)
|
||||
{
|
||||
_netServerImplementation.StopServer(dispose);
|
||||
}
|
||||
|
||||
public bool IsRunningServer => _netServerImplementation.IsRunningServer;
|
||||
|
||||
public void SendMessageToClient(int id, string message)
|
||||
{
|
||||
_netServerImplementation.SendMessageToClient(id, message);
|
||||
}
|
||||
|
||||
public void SendMessageToAll(string message)
|
||||
{
|
||||
_netServerImplementation.SendMessageToAll(message);
|
||||
}
|
||||
|
||||
public IDictionary<int, EndPoint> Connections => _netServerImplementation.Connections;
|
||||
|
||||
public void AddCommandListenerWithId<T>(Action<int, T> handle)
|
||||
{
|
||||
_netServerImplementation.AddCommandListenerWithId(handle);
|
||||
}
|
||||
|
||||
[BIT]
|
||||
private void EditorStartServer()
|
||||
{
|
||||
BITAppForUnity.ThrowIfNotPlaying();
|
||||
StartServer(initialPort);
|
||||
}
|
||||
[BIT]
|
||||
private void EditorStopServer()
|
||||
{
|
||||
BITAppForUnity.ThrowIfNotPlaying();
|
||||
StopServer();
|
||||
}
|
||||
[BIT]
|
||||
private void RandomAPort()
|
||||
{
|
||||
initialPort = (ushort)UnityEngine.Random.Range(10000, ushort.MaxValue);
|
||||
}
|
||||
|
||||
public void ServerCommand<T>(T command = default)
|
||||
{
|
||||
_server.ServerCommand(command);
|
||||
}
|
||||
|
||||
public void AllClientCommand<T>(T command = default)
|
||||
{
|
||||
_server.AllClientCommand(command);
|
||||
}
|
||||
|
||||
public void ClientCommand<T>(int id, T command)
|
||||
{
|
||||
_server.ClientCommand(id, command);
|
||||
}
|
||||
|
||||
public UniTask<T> GetFromServer<T>(string addressablePath = Constant.System.Internal)
|
||||
{
|
||||
return _server.GetFromServer<T>(addressablePath);
|
||||
}
|
||||
|
||||
public UniTask<T> GetFromClient<T>(int id, string addressablePath = Constant.System.Internal)
|
||||
{
|
||||
return _server.GetFromClient<T>(id, addressablePath);
|
||||
}
|
||||
|
||||
public void AddRpcHandle(object rpcHandle)
|
||||
{
|
||||
_server.AddRpcHandle(rpcHandle);
|
||||
}
|
||||
|
||||
public void AddCommandListener<T>(Action<T> handle)
|
||||
{
|
||||
_server.AddCommandListener(handle);
|
||||
}
|
||||
|
||||
public void RemoveCommandListener<T>(Action<T> handle)
|
||||
{
|
||||
_server.RemoveCommandListener(handle);
|
||||
}
|
||||
|
||||
public void SendRT(string rpcName, params object[] pars)
|
||||
{
|
||||
_server.SendRT(rpcName, pars);
|
||||
}
|
||||
|
||||
public void SendTargetRT(int id, string rpcName, params object[] pars)
|
||||
{
|
||||
_server.SendTargetRT(id, rpcName, pars);
|
||||
}
|
||||
|
||||
public void SendAllRT(string rpcName, params object[] pars)
|
||||
{
|
||||
_server.SendAllRT(rpcName, pars);
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
_server.Tick();
|
||||
}
|
||||
|
||||
public void HandShake()
|
||||
{
|
||||
_server.HandShake();
|
||||
}
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (ticker is not null)
|
||||
{
|
||||
ManualTick = true;
|
||||
ticker.Add(OnTick);
|
||||
}
|
||||
|
||||
destroyCancellationToken.Register(() =>
|
||||
{
|
||||
if (IsRunningServer)
|
||||
StopServer(true);
|
||||
ticker?.Remove(OnTick);
|
||||
});
|
||||
}
|
||||
|
||||
private void OnTick(float deltaTime)
|
||||
{
|
||||
Tick();
|
||||
timer.Update(deltaTime);
|
||||
rate = timer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
11
Src/Unity/Scripts/NetProvider/Kcp/MonoKcpServer.cs.meta
Normal file
11
Src/Unity/Scripts/NetProvider/Kcp/MonoKcpServer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 23c7dbd4bc26161488daef24f737e581
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user