mirror of
https://github.com/rafi1212122/PemukulPaku
synced 2025-12-13 05:04:35 +01:00
Namespace refactor, basic Impl for PacketHandlers
This commit is contained in:
@@ -3,8 +3,9 @@ using Common.Resources.Proto;
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Common.Utils;
|
using Common.Utils;
|
||||||
using ProtoBuf;
|
using ProtoBuf;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace PemukulPaku.Gameserver
|
namespace PemukulPaku.GameServer
|
||||||
{
|
{
|
||||||
public class Packet
|
public class Packet
|
||||||
{
|
{
|
||||||
@@ -47,4 +48,56 @@ namespace PemukulPaku.Gameserver
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class PacketCmdId : Attribute
|
||||||
|
{
|
||||||
|
public CmdId Id { get; }
|
||||||
|
|
||||||
|
public PacketCmdId(CmdId id)
|
||||||
|
{
|
||||||
|
Id = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IPacketHandler
|
||||||
|
{
|
||||||
|
public void Handle(Session session, Packet packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class PacketFactory
|
||||||
|
{
|
||||||
|
public static readonly Dictionary<CmdId, IPacketHandler> Handlers = new();
|
||||||
|
static readonly Logger c = new("PKT", ConsoleColor.Yellow);
|
||||||
|
|
||||||
|
public static void LoadPacketHandlers()
|
||||||
|
{
|
||||||
|
c.Log("Loading Packet Handlers...");
|
||||||
|
|
||||||
|
IEnumerable<Type> classes = from t in Assembly.GetExecutingAssembly().GetTypes()
|
||||||
|
select t;
|
||||||
|
|
||||||
|
foreach ((Type t, PacketCmdId attr) in from Type? t in classes.ToList()
|
||||||
|
let attrs = (Attribute[])t.GetCustomAttributes(typeof(PacketCmdId), false)
|
||||||
|
where attrs.Length > 0
|
||||||
|
let attr = (PacketCmdId)attrs[0]
|
||||||
|
where !Handlers.ContainsKey(attr.Id)
|
||||||
|
select (t, attr))
|
||||||
|
{
|
||||||
|
Handlers.Add(attr.Id, (IPacketHandler)Activator.CreateInstance(t));
|
||||||
|
|
||||||
|
c.Log($"Loaded PacketHandler {t.Name} for Packet Type {attr.Id}");
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Log("Finished Loading Packet Handlers");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[PacketCmdId(CmdId.PlayerLoginReq)]
|
||||||
|
public class PlayerLoginReqHandler : IPacketHandler
|
||||||
|
{
|
||||||
|
public void Handle(Session session, Packet packet)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ using System.Net;
|
|||||||
using Common;
|
using Common;
|
||||||
using Common.Utils;
|
using Common.Utils;
|
||||||
|
|
||||||
namespace PemukulPaku.Gameserver
|
namespace PemukulPaku.GameServer
|
||||||
{
|
{
|
||||||
public class Server
|
public class Server
|
||||||
{
|
{
|
||||||
@@ -11,7 +11,8 @@ namespace PemukulPaku.Gameserver
|
|||||||
public readonly Dictionary<string, Session> Sessions = new();
|
public readonly Dictionary<string, Session> Sessions = new();
|
||||||
private static Server? Instance;
|
private static Server? Instance;
|
||||||
|
|
||||||
public static Server GetInstance() {
|
public static Server GetInstance()
|
||||||
|
{
|
||||||
return Instance ??= new Server();
|
return Instance ??= new Server();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using Common.Utils;
|
using Common.Utils;
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace PemukulPaku.Gameserver
|
namespace PemukulPaku.GameServer
|
||||||
{
|
{
|
||||||
public class Session
|
public class Session
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
using Common.Resources.Proto;
|
using Common.Resources.Proto;
|
||||||
using Common;
|
using Common;
|
||||||
using System.Net.NetworkInformation;
|
using System.Net.NetworkInformation;
|
||||||
using PemukulPaku.Gameserver;
|
using PemukulPaku.GameServer;
|
||||||
|
|
||||||
namespace PemukulPaku
|
namespace PemukulPaku
|
||||||
{
|
{
|
||||||
@@ -19,6 +19,7 @@ namespace PemukulPaku
|
|||||||
};
|
};
|
||||||
|
|
||||||
new Thread(HttpServer.Program.Main).Start();
|
new Thread(HttpServer.Program.Main).Start();
|
||||||
|
PacketFactory.LoadPacketHandlers();
|
||||||
_ = Server.GetInstance();
|
_ = Server.GetInstance();
|
||||||
|
|
||||||
Console.Read();
|
Console.Read();
|
||||||
|
|||||||
Reference in New Issue
Block a user