修复NetClient的等待主线程的错误
This commit is contained in:
@@ -76,7 +76,7 @@ namespace BITKit.Net
|
|||||||
|
|
||||||
private async void ConnectionCallback(bool x)
|
private async void ConnectionCallback(bool x)
|
||||||
{
|
{
|
||||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
|
await BITApp.SwitchToMainThread();
|
||||||
if (x)
|
if (x)
|
||||||
{
|
{
|
||||||
OnConnected?.Invoke();
|
OnConnected?.Invoke();
|
||||||
@@ -102,7 +102,7 @@ namespace BITKit.Net
|
|||||||
_timer.Stop();
|
_timer.Stop();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext,BITApp.CancellationToken);
|
await BITApp.SwitchToMainThread();
|
||||||
OnDisconnected?.Invoke();
|
OnDisconnected?.Invoke();
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException){}
|
catch (OperationCanceledException){}
|
||||||
@@ -113,37 +113,27 @@ namespace BITKit.Net
|
|||||||
if (IsConnecting) return false;
|
if (IsConnecting) return false;
|
||||||
IsConnecting = true;
|
IsConnecting = true;
|
||||||
if (client.connected) return false;
|
if (client.connected) return false;
|
||||||
if (BITApp.SynchronizationContext is not null)
|
await BITApp.SwitchToMainThread();
|
||||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext, BITApp.CancellationToken);
|
|
||||||
OnStartConnect?.Invoke();
|
OnStartConnect?.Invoke();
|
||||||
await UniTask.SwitchToThreadPool();
|
await UniTask.SwitchToThreadPool();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_lastHeartbeat = DateTime.Now;
|
_lastHeartbeat = DateTime.Now;
|
||||||
client.Connect(address, port);
|
|
||||||
|
client.Connect(address, port);
|
||||||
|
|
||||||
_timer.Start();
|
_timer.Start();
|
||||||
_interval = TimeSpan.FromMilliseconds(_timer.Interval);
|
_interval = TimeSpan.FromMilliseconds(_timer.Interval);
|
||||||
// for (var i = 0; i < 5; i++)
|
|
||||||
// {
|
|
||||||
// client.Tick();
|
|
||||||
// await Task.Delay(100);
|
|
||||||
// }
|
|
||||||
|
|
||||||
//_commandQueue.Enqueue(new []{(byte)NetCommandType.Heartbeat });
|
|
||||||
|
|
||||||
//client.Send(new []{(byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
|
||||||
HandShake();
|
HandShake();
|
||||||
|
|
||||||
if (BITApp.SynchronizationContext is not null)
|
await BITApp.SwitchToMainThread();
|
||||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
|
|
||||||
|
|
||||||
Traffic += new float2(1, 0);
|
|
||||||
//_commandQueue.Enqueue(new []{(byte)NetCommandType.Heartbeat});
|
|
||||||
|
|
||||||
|
|
||||||
for (var i = 0; i < 5; i++)
|
for (var i = 0; i < 5; i++)
|
||||||
{
|
{
|
||||||
client.Send(new []{(byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
||||||
|
Traffic += new float2(1, 0);
|
||||||
client.Tick();
|
client.Tick();
|
||||||
await Task.Delay(100);
|
await Task.Delay(100);
|
||||||
}
|
}
|
||||||
|
@@ -46,7 +46,7 @@ namespace BITKit.Net
|
|||||||
private DateTime _now=DateTime.Now;
|
private DateTime _now=DateTime.Now;
|
||||||
private TimeSpan _interval=TimeSpan.FromSeconds(0.32);
|
private TimeSpan _interval=TimeSpan.FromSeconds(0.32);
|
||||||
|
|
||||||
private byte[] _heartBeat = new byte[] { (byte)NetCommandType.Heartbeat };
|
private readonly byte[] _heartBeat = new byte[] { (byte)NetCommandType.Heartbeat };
|
||||||
|
|
||||||
public KCPNetServer()
|
public KCPNetServer()
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user