This commit is contained in:
CortexCore
2023-10-24 23:38:22 +08:00
parent 2c4710bc5d
commit bd40165ade
152 changed files with 3681 additions and 1531 deletions

View File

@@ -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)