1
This commit is contained in:
@@ -7,7 +7,7 @@ namespace BITKit.Net
|
||||
public static readonly KcpConfig Config = new KcpConfig(
|
||||
NoDelay: true,
|
||||
DualMode: false,
|
||||
Interval: 1, // 1ms so at interval code at least runs.
|
||||
Interval: 200, // 1ms so at interval code at least runs.
|
||||
Timeout: 2000,
|
||||
CongestionWindow: false
|
||||
|
||||
|
@@ -48,14 +48,17 @@ namespace BITKit.Net
|
||||
});
|
||||
}
|
||||
|
||||
private async void Tick(object sender, ElapsedEventArgs e)
|
||||
private void Tick(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
await UniTask.SwitchToThreadPool();
|
||||
//await UniTask.SwitchToThreadPool();
|
||||
while (commandQueue.TryDequeue(out var bytes))
|
||||
{
|
||||
client.Send(bytes, KcpChannel.Reliable);
|
||||
}
|
||||
client.Tick();
|
||||
//for (var i = 0; i < 32; i++)
|
||||
{
|
||||
client.Tick();
|
||||
}
|
||||
}
|
||||
|
||||
public async void Disconnect()
|
||||
@@ -88,6 +91,10 @@ namespace BITKit.Net
|
||||
if (BITApp.SynchronizationContext is not null)
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
|
||||
OnConnected?.Invoke();
|
||||
if (client.connected)
|
||||
{
|
||||
SendServerMessage(Environment.MachineName);
|
||||
}
|
||||
return client.connected;
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -136,7 +143,7 @@ namespace BITKit.Net
|
||||
|
||||
break;
|
||||
case NetCommandType.Heartbeat:
|
||||
//client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
||||
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
||||
break;
|
||||
default:
|
||||
BIT4Log.Log<KcpClient>($"未知消息类型:{type},字节:{(byte)type}");
|
||||
|
@@ -41,7 +41,7 @@ namespace BITKit.Net
|
||||
private void Tick(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
if (server.IsActive() is false) return;
|
||||
server.Tick();
|
||||
server.Tick();
|
||||
}
|
||||
|
||||
public void StartServer(ushort port = 27014)
|
||||
@@ -66,7 +66,7 @@ namespace BITKit.Net
|
||||
{
|
||||
foreach (var Id in server.connections.Keys)
|
||||
{
|
||||
|
||||
SendMessageToClient(Id,message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,9 +108,11 @@ namespace BITKit.Net
|
||||
using var ms = new MemoryStream(bytes.ToArray());
|
||||
using var reader = new BinaryReader(ms);
|
||||
|
||||
BIT4Log.Log<INetServer>(Id);
|
||||
//BIT4Log.Log<INetServer>(Id);
|
||||
|
||||
var type = (NetCommandType)ms.ReadByte();
|
||||
|
||||
//BIT4Log.Log<INetServer>(type);
|
||||
switch (type)
|
||||
{
|
||||
case NetCommandType.Message:
|
||||
@@ -119,7 +121,7 @@ namespace BITKit.Net
|
||||
case NetCommandType.Command:
|
||||
var command = BITBinary.Read(reader);
|
||||
if (command is object[] { Length: 1 } objs) command = objs[0];
|
||||
BIT4Log.Log<KCPNetServer>($"已收到指令:{command},值:\n{JsonConvert.SerializeObject(command, Formatting.Indented)}");
|
||||
//BIT4Log.Log<KCPNetServer>($"已收到指令:{command},值:\n{JsonConvert.SerializeObject(command, Formatting.Indented)}");
|
||||
_events.Invoke(command.GetType().FullName, command);
|
||||
|
||||
(int Id,object Command) tuple = (Id,command);
|
||||
@@ -189,13 +191,20 @@ namespace BITKit.Net
|
||||
|
||||
public void AddCommandListenerWithId<T>(Action<int, T> handle)
|
||||
{
|
||||
_events.AddListenerDirect(typeof(T).FullName, x =>
|
||||
_events.AddListenerDirect(typeof(T).FullName, Callback);
|
||||
return;
|
||||
|
||||
void Callback(object value)
|
||||
{
|
||||
if (x is ValueTuple<int, T> tuple)
|
||||
if (value is ValueTuple<int, object> tuple && tuple.Item2 is T)
|
||||
{
|
||||
handle.Invoke(tuple.Item1,tuple.Item2);
|
||||
handle.Invoke(tuple.Item1, (T)tuple.Item2);
|
||||
}
|
||||
});
|
||||
else
|
||||
{
|
||||
Console.WriteLine(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveCommandListener<T>(Action<T> handle)
|
||||
|
Reference in New Issue
Block a user