1
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user