using System; using Cysharp.Threading.Tasks; using kcp2k; namespace BITKit.Net { public class KCPNetServer:INetServer,INetProvider { public event Action OnClientConnect; public event Action OnClientDisconnected; public event Action OnStartServer; public event Action OnStopServer; private readonly KcpServer server; public KCPNetServer() { server = new KcpServer( OnConnected, OnData, OnDisconnect, OnError, KCPNet.Config ); } public void StartServer(ushort port = 27014) { OnStartServer?.Invoke(); server.Start(port); } public void StopServer(bool dispose = false) { server.Stop(); OnStopServer?.Invoke(); } public bool IsRunningServer => server.IsActive(); private void OnConnected(int Id) { OnClientConnect?.Invoke(Id); BIT4Log.Log($"{Id}已加入"); } private void OnDisconnect(int Id) { OnClientDisconnected?.Invoke(Id); BIT4Log.Log($"{Id}已断开"); } private void OnData(int Id, ArraySegment bytes, KcpChannel channel) { var command = BITBinary.ReadAsValue(bytes.ToArray()); BIT4Log.Log($"已收到指令:command"); } private void OnError(int Id, ErrorCode errorCode, string message) { BIT4Log.Log($"异常:{errorCode},{message}"); } public void ServerCommand(T command = default) { throw new NotImplementedException(); } public void RpcClientCommand(T command = default) { throw new NotImplementedException(); } public void ClientCommand(int id, T command) { throw new NotImplementedException(); } public UniTask GetFromServer(string addressablePath = Constant.System.Internal) { throw new NotImplementedException(); } public UniTask GetFromClient(int id, string addressablePath = Constant.System.Internal) { throw new NotImplementedException(); } public void AddRpcHandle(object rpcHandle) { throw new NotImplementedException(); } public void AddCommandListener(Action handle) { throw new NotImplementedException(); } public void RemoveCommandListener(Action handle) { throw new NotImplementedException(); } public void SendRT(string rpcName, params object[] pars) { throw new NotImplementedException(); } public void SendTargetRT(int id, string rpcName, params object[] pars) { throw new NotImplementedException(); } public void SendAllRT(string rpcName, params object[] pars) { throw new NotImplementedException(); } } }