1
This commit is contained in:
@@ -19,18 +19,24 @@ namespace BITKit.Net
|
||||
public event Action OnDisconnected;
|
||||
public event Action OnConnectedFailed;
|
||||
public bool IsConnected => client.connected;
|
||||
public int Ping => -1;
|
||||
|
||||
public bool ManualTick { get; set; }
|
||||
|
||||
public int Ping { get; private set; }
|
||||
public int Id { get; private set; } = -1;
|
||||
private readonly KcpClient client;
|
||||
|
||||
private readonly Queue<byte[]> commandQueue = new();
|
||||
|
||||
private DateTime _lastPingTime = DateTime.Now;
|
||||
|
||||
private readonly Timer _timer = new(100)
|
||||
{
|
||||
AutoReset = true
|
||||
};
|
||||
|
||||
private readonly GenericEvent _events = new();
|
||||
|
||||
public KcpNetClient()
|
||||
{
|
||||
client = new KcpClient(
|
||||
@@ -50,15 +56,8 @@ namespace BITKit.Net
|
||||
|
||||
private void Tick(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
//await UniTask.SwitchToThreadPool();
|
||||
while (commandQueue.TryDequeue(out var bytes))
|
||||
{
|
||||
client.Send(bytes, KcpChannel.Reliable);
|
||||
}
|
||||
//for (var i = 0; i < 32; i++)
|
||||
{
|
||||
client.Tick();
|
||||
}
|
||||
if (!ManualTick)
|
||||
Tick();
|
||||
}
|
||||
|
||||
public async void Disconnect()
|
||||
@@ -145,6 +144,9 @@ namespace BITKit.Net
|
||||
case NetCommandType.Heartbeat:
|
||||
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
||||
break;
|
||||
case NetCommandType.Ping:
|
||||
Ping = (int)(DateTime.Now - _lastPingTime).TotalMilliseconds;
|
||||
break;
|
||||
default:
|
||||
BIT4Log.Log<KcpClient>($"未知消息类型:{type},字节:{(byte)type}");
|
||||
if (bytes.Array != null)
|
||||
@@ -250,7 +252,29 @@ namespace BITKit.Net
|
||||
client.Send(bytes, KcpChannel.Reliable);
|
||||
}
|
||||
|
||||
public void Tick() => client.Tick();
|
||||
#if UNITY_EDITOR
|
||||
private readonly IntervalUpdate _pingInterval = new(1);
|
||||
#endif
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
while (commandQueue.TryDequeue(out var bytes))
|
||||
{
|
||||
client.Send(bytes, KcpChannel.Reliable);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if (_pingInterval.AllowUpdate)
|
||||
{
|
||||
_lastPingTime = DateTime.Now;
|
||||
client.Send(new[] { (byte)NetCommandType.Ping }, KcpChannel.Reliable);
|
||||
}
|
||||
#endif
|
||||
|
||||
client.Tick();
|
||||
|
||||
}
|
||||
|
||||
public void HandShake()
|
||||
{
|
||||
// send client to server
|
||||
|
@@ -14,6 +14,7 @@ namespace BITKit.Net
|
||||
{
|
||||
public class KCPNetServer:INetServer,INetProvider
|
||||
{
|
||||
public bool ManualTick { get; set; }
|
||||
public event Action<int> OnClientConnected;
|
||||
public event Action<int> OnClientDisconnected;
|
||||
public event Action OnStartServer;
|
||||
@@ -24,7 +25,7 @@ namespace BITKit.Net
|
||||
{
|
||||
AutoReset = true
|
||||
};
|
||||
|
||||
|
||||
public KCPNetServer()
|
||||
{
|
||||
server = new KcpServer(
|
||||
@@ -40,21 +41,38 @@ namespace BITKit.Net
|
||||
|
||||
private void Tick(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
if (server.IsActive() is false) return;
|
||||
if (server.IsActive() is false || ManualTick) return;
|
||||
server.Tick();
|
||||
}
|
||||
|
||||
public void StartServer(ushort port = 27014)
|
||||
{
|
||||
OnStartServer?.Invoke();
|
||||
server.Start(port);
|
||||
_timer.Start();
|
||||
if (IsRunningServer is false)
|
||||
{
|
||||
OnStartServer?.Invoke();
|
||||
server.Start(port);
|
||||
_timer.Start();
|
||||
BIT4Log.Log<KCPNetServer>($"已启动KCP服务器:{port}");
|
||||
}
|
||||
else
|
||||
{
|
||||
BIT4Log.Warning<KCPNetServer>($"KCP服务器已经启动,忽略此次请求");
|
||||
}
|
||||
|
||||
}
|
||||
public void StopServer(bool dispose = false)
|
||||
{
|
||||
server.Stop();
|
||||
OnStopServer?.Invoke();
|
||||
_timer.Stop();
|
||||
if (IsRunningServer)
|
||||
{
|
||||
server.Stop();
|
||||
OnStopServer?.Invoke();
|
||||
_timer.Stop();
|
||||
BIT4Log.Log<KCPNetServer>($"已停止KCP服务器");
|
||||
}
|
||||
else
|
||||
{
|
||||
BIT4Log.Warning<KCPNetServer>($"KCP服务器未运行,忽略此次请求");
|
||||
}
|
||||
}
|
||||
public bool IsRunningServer => server.IsActive();
|
||||
public void SendMessageToClient(int id, string message)
|
||||
@@ -140,6 +158,9 @@ namespace BITKit.Net
|
||||
var targetId = reader.ReadInt32();
|
||||
server.Send(targetId,bytes,channel);
|
||||
break;
|
||||
case NetCommandType.Ping:
|
||||
server.Send(Id,new byte[]{(byte)NetCommandType.Ping},channel);
|
||||
break;
|
||||
default:
|
||||
BIT4Log.Log<KCPNetServer>($"未知消息类型:{type},字节:{(byte)type}");
|
||||
BIT4Log.Log<KCPNetServer>($"已收到:({Id}, {BitConverter.ToString(bytes.Array, bytes.Offset, bytes.Count)} @ {channel})");
|
||||
@@ -235,5 +256,6 @@ namespace BITKit.Net
|
||||
.Build();
|
||||
server.Send(id,bytes, KcpChannel.Reliable);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user