BITKit/Packages/Runtime~/Core/Kcp/KcpNetServer.cs

111 lines
3.1 KiB
C#
Raw Normal View History

2023-06-29 14:57:11 +08:00
using System;
using Cysharp.Threading.Tasks;
using kcp2k;
namespace BITKit.Net
{
public class KCPNetServer:INetServer,INetProvider
{
public event Action<int> OnClientConnect;
public event Action<int> 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<KCPNetServer>($"{Id}已加入");
}
private void OnDisconnect(int Id)
{
OnClientDisconnected?.Invoke(Id);
BIT4Log.Log<KCPNetServer>($"{Id}已断开");
}
private void OnData(int Id, ArraySegment<byte> bytes, KcpChannel channel)
{
var command = BITBinary.ReadAsValue(bytes.ToArray());
BIT4Log.Log<KCPNetServer>($"已收到指令:command");
}
private void OnError(int Id, ErrorCode errorCode, string message)
{
BIT4Log.Log<KCPNetServer>($"异常:{errorCode},{message}");
}
public void ServerCommand<T>(T command = default)
{
throw new NotImplementedException();
}
public void RpcClientCommand<T>(T command = default)
{
throw new NotImplementedException();
}
public void ClientCommand<T>(int id, T command)
{
throw new NotImplementedException();
}
public UniTask<T> GetFromServer<T>(string addressablePath = Constant.System.Internal)
{
throw new NotImplementedException();
}
public UniTask<T> GetFromClient<T>(int id, string addressablePath = Constant.System.Internal)
{
throw new NotImplementedException();
}
public void AddRpcHandle(object rpcHandle)
{
throw new NotImplementedException();
}
public void AddCommandListener<T>(Action<T> handle)
{
throw new NotImplementedException();
}
public void RemoveCommandListener<T>(Action<T> 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();
}
}
}