1
This commit is contained in:
@@ -29,7 +29,7 @@ namespace BITKit.Net
|
||||
public int Id { get; private set; } = -1;
|
||||
private readonly KcpClient client;
|
||||
|
||||
private readonly Queue<byte[]> commandQueue = new();
|
||||
private readonly ConcurrentQueue<byte[]> _commandQueue = new();
|
||||
|
||||
private DateTime _lastPingTime = DateTime.Now;
|
||||
|
||||
@@ -92,6 +92,7 @@ namespace BITKit.Net
|
||||
{
|
||||
client.Disconnect();
|
||||
_isConnected.RemoveElement(this);
|
||||
_timer.Stop();
|
||||
try
|
||||
{
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext,BITApp.CancellationToken);
|
||||
@@ -108,30 +109,35 @@ namespace BITKit.Net
|
||||
await UniTask.SwitchToThreadPool();
|
||||
try
|
||||
{
|
||||
_lastHeartbeat = DateTime.Now;
|
||||
client.Connect(address, port);
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
client.Tick();
|
||||
await Task.Delay(100);
|
||||
}
|
||||
_timer.Start();
|
||||
Traffic+=new float2(1,0);
|
||||
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
||||
// for (var i = 0; i < 5; i++)
|
||||
// {
|
||||
// client.Tick();
|
||||
// await Task.Delay(100);
|
||||
// }
|
||||
|
||||
_commandQueue.Enqueue(new []{(byte)NetCommandType.Heartbeat });
|
||||
|
||||
if (BITApp.SynchronizationContext is not null)
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
|
||||
if (client.connected)
|
||||
{
|
||||
SendServerMessage(Environment.MachineName);
|
||||
Traffic+=new float2(1,0);
|
||||
_commandQueue.Enqueue(new []{(byte)NetCommandType.Heartbeat});
|
||||
}
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
client.Tick();
|
||||
await Task.Delay(100);
|
||||
}
|
||||
|
||||
if (client.connected is false)
|
||||
{
|
||||
OnConnectedFailed?.Invoke();
|
||||
Disconnect();
|
||||
return false;
|
||||
}
|
||||
return client.connected;
|
||||
}
|
||||
@@ -141,6 +147,7 @@ namespace BITKit.Net
|
||||
if (BITApp.SynchronizationContext is not null)
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
|
||||
OnConnectedFailed?.Invoke();
|
||||
_timer.Stop();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -191,8 +198,7 @@ namespace BITKit.Net
|
||||
|
||||
break;
|
||||
case NetCommandType.Heartbeat:
|
||||
Traffic+=new float2(1,0);
|
||||
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
||||
_commandQueue.Enqueue(new []{(byte)NetCommandType.Heartbeat });
|
||||
_isConnected.AddElement(this);
|
||||
_lastHeartbeat = DateTime.Now;
|
||||
break;
|
||||
@@ -282,7 +288,8 @@ namespace BITKit.Net
|
||||
returnWriter.Write(e.Message);
|
||||
}
|
||||
var _bytes = returnMS.ToArray();
|
||||
commandQueue.Enqueue(_bytes);
|
||||
|
||||
_commandQueue.Enqueue(_bytes);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
@@ -300,29 +307,16 @@ namespace BITKit.Net
|
||||
|
||||
private async void OnConnectedInternal()
|
||||
{
|
||||
if (BITApp.SynchronizationContext is not null)
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
|
||||
OnConnected?.Invoke();
|
||||
BIT4Log.Log<KcpNetClient>("已连接");
|
||||
// if (BITApp.SynchronizationContext is not null)
|
||||
// await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
|
||||
// OnConnected?.Invoke();
|
||||
// BIT4Log.Log<KcpNetClient>("已连接");
|
||||
}
|
||||
|
||||
private async void OnDisconnectInternal()
|
||||
{
|
||||
//BIT4Log.Log<KcpNetClient>("断开连接");
|
||||
_timer.Stop();
|
||||
try
|
||||
{
|
||||
if (BITApp.SynchronizationContext is not null)
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext,
|
||||
BITApp.CancellationToken);
|
||||
_isConnected.RemoveElement(this);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
BIT4Log.Log<KcpNetClient>("连接被断开");
|
||||
Disconnect();
|
||||
}
|
||||
private void OnError(ErrorCode errorCode, string message)
|
||||
{
|
||||
@@ -331,17 +325,31 @@ namespace BITKit.Net
|
||||
|
||||
public void ServerCommand<T>(T command = default)
|
||||
{
|
||||
Send(NetCommandType.Command,command);
|
||||
using var ms = new MemoryStream();
|
||||
using var writer = new BinaryWriter(ms);
|
||||
writer.Write((byte)NetCommandType.Command);
|
||||
BITBinary.Write(writer,command);
|
||||
var bytes = ms.ToArray();
|
||||
_commandQueue.Enqueue(bytes);
|
||||
}
|
||||
|
||||
public void AllClientCommand<T>(T command = default)
|
||||
{
|
||||
Send(NetCommandType.AllClientCommand,command);
|
||||
using var ms = new MemoryStream();
|
||||
using var writer = new BinaryWriter(ms);
|
||||
writer.Write((byte)NetCommandType.AllClientCommand);
|
||||
BITBinary.Write(writer,command);
|
||||
var bytes = ms.ToArray();
|
||||
}
|
||||
|
||||
public void ClientCommand<T>(int id, T command)
|
||||
{
|
||||
Send(NetCommandType.TargetCommand,id,command);
|
||||
using var ms = new MemoryStream();
|
||||
using var writer = new BinaryWriter(ms);
|
||||
writer.Write((byte)NetCommandType.Command);
|
||||
BITBinary.Write(writer,command);
|
||||
var bytes = ms.ToArray();
|
||||
_commandQueue.Enqueue(bytes);
|
||||
}
|
||||
|
||||
public async UniTask<T> GetFromServer<T>(string path = default,params object[] pars)
|
||||
@@ -363,8 +371,12 @@ namespace BITKit.Net
|
||||
BITBinary.Write(writer,pars);
|
||||
|
||||
var bytes = ms.ToArray();
|
||||
commandQueue.Enqueue(bytes);
|
||||
_commandQueue.Enqueue(bytes);
|
||||
var startTime = DateTime.Now;
|
||||
|
||||
|
||||
|
||||
|
||||
while (true)
|
||||
{
|
||||
if(DateTime.Now-startTime>TimeSpan.FromSeconds(5) || IsConnected is false)
|
||||
@@ -380,10 +392,6 @@ namespace BITKit.Net
|
||||
{
|
||||
return t;
|
||||
}
|
||||
if (typeof(T) == typeof(UniTaskVoid))
|
||||
{
|
||||
return default;
|
||||
}
|
||||
return value.As<T>();
|
||||
}
|
||||
|
||||
@@ -403,8 +411,16 @@ namespace BITKit.Net
|
||||
{
|
||||
var att = methodInfo.GetCustomAttribute<NetRpcAttribute>();
|
||||
if(att is null)continue;
|
||||
_rpcMethods.TryAdd(methodInfo.Name, methodInfo);
|
||||
_rpcHandles.TryAdd(methodInfo.Name, rpcHandle);
|
||||
_rpcMethods.AddOrUpdate(methodInfo.Name, methodInfo, (s, info) => methodInfo);
|
||||
_rpcHandles.AddOrUpdate(methodInfo.Name, rpcHandle, (s, o) => rpcHandle);
|
||||
}
|
||||
|
||||
foreach (var eventInfo in rpcHandle.GetType().GetEvents())
|
||||
{
|
||||
var att = eventInfo.GetCustomAttribute<NetRpcAttribute>();
|
||||
if(att is null)continue;
|
||||
var handle = eventInfo.EventHandlerType.GetMethod("Invoke");
|
||||
_rpcMethods.AddOrUpdate(handle.Name, handle, (s, info) => handle);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -436,7 +452,13 @@ namespace BITKit.Net
|
||||
|
||||
public void SendRT(string rpcName, params object[] pars)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
using var ms = new MemoryStream();
|
||||
using var writer = new BinaryWriter(ms);
|
||||
writer.Write((byte)NetCommandType.GetFromClient);
|
||||
writer.Write(rpcName);
|
||||
BITBinary.Write(writer,pars);
|
||||
var bytes = ms.ToArray();
|
||||
_commandQueue.Enqueue(bytes);
|
||||
}
|
||||
|
||||
public void SendTargetRT(int id, string rpcName, params object[] pars)
|
||||
@@ -477,7 +499,7 @@ namespace BITKit.Net
|
||||
}
|
||||
}
|
||||
|
||||
while (commandQueue.TryDequeue(out var bytes))
|
||||
while (_commandQueue.TryDequeue(out var bytes))
|
||||
{
|
||||
Traffic+=new float2(bytes.Length,0);
|
||||
client.Send(bytes, KcpChannel.Reliable);
|
||||
@@ -506,17 +528,5 @@ namespace BITKit.Net
|
||||
Traffic+=new float2(2,0);
|
||||
client.Send(new byte[]{0x01, 0x02}, KcpChannel.Reliable);
|
||||
}
|
||||
|
||||
private void Send(NetCommandType commandType,params object[] values)
|
||||
{
|
||||
var bytes = BinaryBuilder
|
||||
.Create()
|
||||
.Write((byte)commandType)
|
||||
.WriteObject(values)
|
||||
.Build();
|
||||
commandQueue.Enqueue(bytes);
|
||||
//client.Send(bytes, KcpChannel.Reliable);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user