[성현모] Http, Socket 통신 추가

This commit is contained in:
SHM
2025-04-21 08:30:59 +09:00
parent 1bfc56f437
commit a12a3949bf
25 changed files with 1370 additions and 264 deletions

View File

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemX.Core.Communication;
namespace HubX.Library.Socket.Packet
{
public class PacketManager
{
#region Singleton
static PacketManager _instance = new PacketManager();
public static PacketManager Instance { get { return _instance; } }
#endregion
PacketManager()
{
Register();
}
Dictionary<ushort, Action<PacketSession, ArraySegment<byte>, ushort>> _onRecv = new Dictionary<ushort, Action<PacketSession, ArraySegment<byte>, ushort>>();
Dictionary<ushort, Action<PacketSession, IMessage>> _handler = new Dictionary<ushort, Action<PacketSession, IMessage>>();
public Action<PacketSession, IMessage, ushort> CustomHandler { get; set; }
public void Register()
{
_onRecv.Add((ushort)EnumMessageId.C2S_INSERT_UniqueKey, MakePacket<C2S_INSERT_UniqueKey>);
_handler.Add((ushort)EnumMessageId.C2S_INSERT_UniqueKey, PacketHandler.C2S_INSERT_UniqueKeyHandler);
}
public void OnRecvPacket(PacketSession session, ArraySegment<byte> buffer)
{
ushort count = 0;
ushort size = BitConverter.ToUInt16(buffer.Array, buffer.Offset);
count += 2;
ushort id = BitConverter.ToUInt16(buffer.Array, buffer.Offset + count);
count += 2;
Action<PacketSession, ArraySegment<byte>, ushort> action = null;
if (_onRecv.TryGetValue(id, out action))
action.Invoke(session, buffer, id);
}
void MakePacket<T>(PacketSession session, ArraySegment<byte> buffer, ushort id) where T : IMessage, new()
{
T pkt = new T();
//pkt.MergeFrom(buffer.Array, buffer.Offset + 4, buffer.Count - 4);
if (CustomHandler != null)
{
CustomHandler.Invoke(session, pkt, id);
}
else
{
Action<PacketSession, IMessage> action = null;
if (_handler.TryGetValue(id, out action))
action.Invoke(session, pkt);
}
}
public Action<PacketSession, IMessage> GetPacketHandler(ushort id)
{
Action<PacketSession, IMessage> action = null;
if (_handler.TryGetValue(id, out action))
return action;
return null;
}
}
}