BITKit/Src/Unity/Scripts/NetProvider/Kcp/MonoKcpServer.cs

201 lines
4.5 KiB
C#
Raw Normal View History

2024-03-31 23:31:00 +08:00
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);
}
2024-06-08 15:12:48 +08:00
public void KickClient(int id)
{
_netServerImplementation.KickClient(id);
}
2024-03-31 23:31:00 +08:00
[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);
}
2024-06-12 16:49:42 +08:00
public UniTask<T> GetFromServer<T>(string path = null, params object[] pars)
2024-03-31 23:31:00 +08:00
{
2024-06-12 16:49:42 +08:00
return _serverInstance.GetFromServer<T>(path, pars);
2024-03-31 23:31:00 +08:00
}
2024-06-12 16:49:42 +08:00
public UniTask<T> GetFromClient<T>(int id, string path = null, params object[] pars)
2024-03-31 23:31:00 +08:00
{
2024-06-12 16:49:42 +08:00
return _serverInstance.GetFromClient<T>(id, path, pars);
2024-03-31 23:31:00 +08:00
}
public void AddRpcHandle(object rpcHandle)
{
_server.AddRpcHandle(rpcHandle);
}
public void AddCommandListener<T>(Action<T> handle)
{
_server.AddCommandListener(handle);
}
2024-06-08 15:12:48 +08:00
public void AddCommandListener<T>(Func<T,UniTask<T>> func)
2024-06-08 10:07:40 +08:00
{
_serverInstance.AddCommandListener(func);
}
2024-06-08 15:12:48 +08:00
public void RemoveCommandListener<T>(Func<T,UniTask<T>> func)
2024-06-08 10:07:40 +08:00
{
_serverInstance.RemoveCommandListener(func);
}
2024-03-31 23:31:00 +08:00
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;
}
}
}