remove over debug, and fix mainui login, and reinvent handlers

This commit is contained in:
rfi
2023-10-14 20:00:56 +07:00
parent 630b0aab84
commit 6fb53b1c46
6 changed files with 456 additions and 669 deletions

View File

@@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MessagePack" Version="2.5.129" />
<PackageReference Include="MessagePack" Version="2.4.59" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

File diff suppressed because one or more lines are too long

View File

@@ -77,24 +77,29 @@ namespace AscNet.GameServer
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
[AttributeUsage(AttributeTargets.Method)]
public class PacketHandler : Attribute
public class RequestPacketHandler : Attribute
{
public string Name { get; }
public PacketHandler(string name)
public RequestPacketHandler(string name)
{
Name = name;
}
}
public delegate void PacketHandlerDelegate(Session session, byte[] packet);
public delegate void RequestPacketHandlerDelegate(Session session, Packet.Request packet);
public static class PacketFactory
{
public static readonly Dictionary<string, PacketHandlerDelegate> Handlers = new();
public static readonly Dictionary<string, RequestPacketHandlerDelegate> ReqHandlers = new();
static readonly Logger c = new("Factory", ConsoleColor.Yellow);
public static void LoadPacketHandlers()
{
LoadRequestPacketHandlers();
}
private static void LoadRequestPacketHandlers()
{
c.Log("Loading Packet Handlers...");
@@ -103,9 +108,9 @@ namespace AscNet.GameServer
foreach (var method in classes.SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)))
{
var attr = method.GetCustomAttribute<PacketHandler>(false);
if (attr == null || Handlers.ContainsKey(attr.Name)) continue;
Handlers.Add(attr.Name, (PacketHandlerDelegate)Delegate.CreateDelegate(typeof(PacketHandlerDelegate), method));
var attr = method.GetCustomAttribute<RequestPacketHandler>(false);
if (attr == null || ReqHandlers.ContainsKey(attr.Name)) continue;
ReqHandlers.Add(attr.Name, (RequestPacketHandlerDelegate)Delegate.CreateDelegate(typeof(RequestPacketHandlerDelegate), method));
#if DEBUG
c.Log($"Loaded {method.Name}");
#endif
@@ -114,9 +119,9 @@ namespace AscNet.GameServer
c.Log("Finished Loading Packet Handlers");
}
public static PacketHandlerDelegate? GetPacketHandler(string name)
public static RequestPacketHandlerDelegate? GetRequestPacketHandler(string name)
{
Handlers.TryGetValue(name, out PacketHandlerDelegate? handler);
ReqHandlers.TryGetValue(name, out RequestPacketHandlerDelegate? handler);
return handler;
}
}

View File

@@ -12,7 +12,7 @@ namespace AscNet.GameServer
public readonly TcpClient client;
public readonly Logger c;
private long lastPacketTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
private ushort packetNo = 1;
private ushort packetNo = 0;
private readonly MessagePackSerializerOptions lz4Options = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4Block);
public Session(string id, TcpClient tcpClient)
@@ -76,12 +76,21 @@ namespace AscNet.GameServer
case Packet.ContentType.Request:
Packet.Request request = MessagePackSerializer.Deserialize<Packet.Request>(packet.Content);
debugContent = request.Content;
PacketFactory.GetPacketHandler(request.Name)?.Invoke(this, request.Content);
RequestPacketHandlerDelegate? requestPacketHandler = PacketFactory.GetRequestPacketHandler(request.Name);
if (requestPacketHandler is not null)
{
c.Log(request.Name);
requestPacketHandler.Invoke(this, request);
}
else
c.Warn($"{request.Name} handler not found!");
break;
case Packet.ContentType.Push:
Packet.Push push = MessagePackSerializer.Deserialize<Packet.Push>(packet.Content);
debugContent = push.Content;
PacketFactory.GetPacketHandler(push.Name)?.Invoke(this, push.Content);
c.Log(push.Name);
throw new NotImplementedException($"Packet push handlers not implemented ({push.Name})");
break;
case Packet.ContentType.Exception:
Packet.Exception exception = MessagePackSerializer.Deserialize<Packet.Exception>(packet.Content);
@@ -122,30 +131,44 @@ namespace AscNet.GameServer
};
Send(new Packet()
{
No = packetNo,
No = ++packetNo,
Type = Packet.ContentType.Push,
Content = MessagePackSerializer.Serialize(packet)
});
c.Log(packet.Name + " " + JsonConvert.SerializeObject(push));
packetNo++;
c.Log(packet.Name);
}
public void SendResponse<T>(T response)
public void SendPush(string name, byte[] push)
{
Packet.Push packet = new()
{
Name = name,
Content = push
};
Send(new Packet()
{
No = ++packetNo,
Type = Packet.ContentType.Push,
Content = MessagePackSerializer.Serialize(packet)
});
c.Log(packet.Name);
}
public void SendResponse<T>(T response, int clientSeq = 0)
{
Packet.Response packet = new()
{
Id = 1,
Name = typeof(T).Name,
Id = clientSeq,
Name = response!.GetType().Name,
Content = MessagePackSerializer.Serialize(response)
};
Send(new Packet()
{
No = packetNo,
No = 0,
Type = Packet.ContentType.Response,
Content = MessagePackSerializer.Serialize(packet)
});
c.Log(packet.Name + " " + JsonConvert.SerializeObject(response));
packetNo++;
c.Log(packet.Name);
}
private void Send(Packet packet)