2024-04-10 10:12:21 +00:00
|
|
|
|
using System.Reflection;
|
2023-10-14 17:39:26 +00:00
|
|
|
|
using AscNet.Logging;
|
2023-10-10 09:56:08 +00:00
|
|
|
|
using MessagePack;
|
2023-10-08 14:12:01 +00:00
|
|
|
|
|
|
|
|
|
namespace AscNet.GameServer
|
|
|
|
|
{
|
|
|
|
|
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
|
|
|
|
[MessagePackObject(false)]
|
|
|
|
|
public class Packet
|
|
|
|
|
{
|
|
|
|
|
[Key(0)]
|
|
|
|
|
public int No;
|
|
|
|
|
|
|
|
|
|
[Key(1)]
|
|
|
|
|
public ContentType Type;
|
|
|
|
|
|
|
|
|
|
[Key(2)]
|
|
|
|
|
public byte[] Content;
|
|
|
|
|
|
|
|
|
|
public enum ContentType
|
|
|
|
|
{
|
|
|
|
|
Request,
|
|
|
|
|
Response,
|
|
|
|
|
Push,
|
|
|
|
|
Exception
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MessagePackObject(false)]
|
|
|
|
|
public class Request
|
|
|
|
|
{
|
|
|
|
|
[Key(0)]
|
|
|
|
|
public int Id;
|
|
|
|
|
|
|
|
|
|
[Key(1)]
|
|
|
|
|
public string Name;
|
|
|
|
|
|
|
|
|
|
[Key(2)]
|
|
|
|
|
public byte[] Content;
|
2023-11-19 14:02:09 +00:00
|
|
|
|
|
|
|
|
|
public T Deserialize<T>()
|
|
|
|
|
{
|
|
|
|
|
return MessagePackSerializer.Deserialize<T>(Content);
|
|
|
|
|
}
|
2023-10-08 14:12:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MessagePackObject(false)]
|
|
|
|
|
public class Response
|
|
|
|
|
{
|
|
|
|
|
[Key(0)]
|
|
|
|
|
public int Id;
|
|
|
|
|
|
|
|
|
|
[Key(1)]
|
|
|
|
|
public string Name;
|
|
|
|
|
|
|
|
|
|
[Key(2)]
|
|
|
|
|
public byte[] Content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MessagePackObject(false)]
|
|
|
|
|
public class Push
|
|
|
|
|
{
|
|
|
|
|
[Key(0)]
|
|
|
|
|
public string Name;
|
|
|
|
|
|
|
|
|
|
[Key(1)]
|
|
|
|
|
public byte[] Content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MessagePackObject(false)]
|
|
|
|
|
public class Exception
|
|
|
|
|
{
|
|
|
|
|
[Key(0)]
|
|
|
|
|
public int Id;
|
|
|
|
|
|
|
|
|
|
[Key(1)]
|
|
|
|
|
public int Code;
|
|
|
|
|
|
|
|
|
|
[Key(2)]
|
|
|
|
|
public string Message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
2023-10-10 09:56:08 +00:00
|
|
|
|
|
|
|
|
|
[AttributeUsage(AttributeTargets.Method)]
|
2023-10-14 13:00:56 +00:00
|
|
|
|
public class RequestPacketHandler : Attribute
|
2023-10-10 09:56:08 +00:00
|
|
|
|
{
|
|
|
|
|
public string Name { get; }
|
|
|
|
|
|
2023-10-14 13:00:56 +00:00
|
|
|
|
public RequestPacketHandler(string name)
|
2023-10-10 09:56:08 +00:00
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-14 13:00:56 +00:00
|
|
|
|
public delegate void RequestPacketHandlerDelegate(Session session, Packet.Request packet);
|
2023-10-10 09:56:08 +00:00
|
|
|
|
|
|
|
|
|
public static class PacketFactory
|
|
|
|
|
{
|
2023-10-14 13:00:56 +00:00
|
|
|
|
public static readonly Dictionary<string, RequestPacketHandlerDelegate> ReqHandlers = new();
|
2023-10-14 17:39:26 +00:00
|
|
|
|
|
|
|
|
|
// TODO: Configure based on session?
|
|
|
|
|
static readonly Logger log = new(typeof(PacketFactory), LogLevel.DEBUG, LogLevel.DEBUG);
|
2023-10-10 09:56:08 +00:00
|
|
|
|
|
|
|
|
|
public static void LoadPacketHandlers()
|
2023-10-14 13:00:56 +00:00
|
|
|
|
{
|
2023-10-14 18:02:51 +00:00
|
|
|
|
log.LogLevelColor[LogLevel.INFO] = ConsoleColor.White;
|
2023-10-14 13:00:56 +00:00
|
|
|
|
LoadRequestPacketHandlers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void LoadRequestPacketHandlers()
|
2023-10-10 09:56:08 +00:00
|
|
|
|
{
|
2023-10-14 17:39:26 +00:00
|
|
|
|
log.Info("Loading Packet Handlers...");
|
2023-10-10 09:56:08 +00:00
|
|
|
|
|
|
|
|
|
IEnumerable<Type> classes = from t in Assembly.GetExecutingAssembly().GetTypes()
|
|
|
|
|
select t;
|
|
|
|
|
|
|
|
|
|
foreach (var method in classes.SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)))
|
|
|
|
|
{
|
2023-10-14 13:00:56 +00:00
|
|
|
|
var attr = method.GetCustomAttribute<RequestPacketHandler>(false);
|
2023-10-14 18:02:51 +00:00
|
|
|
|
if (attr == null || ReqHandlers.ContainsKey(attr.Name))
|
|
|
|
|
continue;
|
2023-10-14 13:00:56 +00:00
|
|
|
|
ReqHandlers.Add(attr.Name, (RequestPacketHandlerDelegate)Delegate.CreateDelegate(typeof(RequestPacketHandlerDelegate), method));
|
2023-10-10 09:56:08 +00:00
|
|
|
|
#if DEBUG
|
2023-10-14 17:39:26 +00:00
|
|
|
|
log.Info($"Loaded {method.Name}");
|
2023-10-10 09:56:08 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 12:10:02 +00:00
|
|
|
|
log.Info("Finished loading packet handlers");
|
2023-10-10 09:56:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-14 13:00:56 +00:00
|
|
|
|
public static RequestPacketHandlerDelegate? GetRequestPacketHandler(string name)
|
2023-10-10 09:56:08 +00:00
|
|
|
|
{
|
2023-10-14 13:00:56 +00:00
|
|
|
|
ReqHandlers.TryGetValue(name, out RequestPacketHandlerDelegate? handler);
|
2023-10-10 09:56:08 +00:00
|
|
|
|
return handler;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-08 14:12:01 +00:00
|
|
|
|
}
|