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 OnClientConnected { add => _netServerImplementation.OnClientConnected += value; remove => _netServerImplementation.OnClientConnected -= value; } public event Action 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 Connections => _netServerImplementation.Connections; public void AddCommandListenerWithId(Action 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 command = default) { _server.ServerCommand(command); } public void AllClientCommand(T command = default) { _server.AllClientCommand(command); } public void ClientCommand(int id, T command) { _server.ClientCommand(id, command); } public UniTask GetFromServer(string path = null, params object[] pars) { return _serverInstance.GetFromServer(path, pars); } public UniTask GetFromClient(int id, string path = null, params object[] pars) { return _serverInstance.GetFromClient(id, path, pars); } public void AddRpcHandle(object rpcHandle) { _server.AddRpcHandle(rpcHandle); } public void AddCommandListener(Action handle) { _server.AddCommandListener(handle); } public void AddCommandListener(Func> func) { _serverInstance.AddCommandListener(func); } public void RemoveCommandListener(Func> func) { _serverInstance.RemoveCommandListener(func); } public void RemoveCommandListener(Action 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; } } }