201 lines
4.5 KiB
C#
201 lines
4.5 KiB
C#
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);
|
|
}
|
|
|
|
public void KickClient(int id)
|
|
{
|
|
_netServerImplementation.KickClient(id);
|
|
}
|
|
|
|
[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 path = null, params object[] pars)
|
|
{
|
|
return _serverInstance.GetFromServer<T>(path, pars);
|
|
}
|
|
|
|
public UniTask<T> GetFromClient<T>(int id, string path = null, params object[] pars)
|
|
{
|
|
return _serverInstance.GetFromClient<T>(id, path, pars);
|
|
}
|
|
public void AddRpcHandle(object rpcHandle)
|
|
{
|
|
_server.AddRpcHandle(rpcHandle);
|
|
}
|
|
|
|
public void AddCommandListener<T>(Action<T> handle)
|
|
{
|
|
_server.AddCommandListener(handle);
|
|
}
|
|
|
|
public void AddCommandListener<T>(Func<T,UniTask<T>> func)
|
|
{
|
|
_serverInstance.AddCommandListener(func);
|
|
}
|
|
|
|
public void RemoveCommandListener<T>(Func<T,UniTask<T>> func)
|
|
{
|
|
_serverInstance.RemoveCommandListener(func);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|