[성현모] Config 기능 추가, Socket Recv Json, string 기능 분리
This commit is contained in:
@ -20,9 +20,9 @@ namespace HubX.Library.Socket.Packet
|
||||
}
|
||||
|
||||
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>>();
|
||||
Dictionary<ushort, Action<PacketSession, ArraySegment<byte>>> _handler = new Dictionary<ushort, Action<PacketSession, ArraySegment<byte>>>();
|
||||
|
||||
public Action<PacketSession, IMessage, ushort> CustomHandler { get; set; }
|
||||
public Action<PacketSession, ArraySegment<byte>, ushort> CustomHandler { get; set; }
|
||||
|
||||
public void Register()
|
||||
{
|
||||
@ -39,31 +39,34 @@ namespace HubX.Library.Socket.Packet
|
||||
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);
|
||||
ushort packetSize = (ushort)(buffer.Count - count);
|
||||
byte[] packet = new byte[packetSize];
|
||||
Array.Copy(buffer.ToArray(), count, packet, 0, packetSize);
|
||||
|
||||
if (_onRecv.TryGetValue(id, out var action))
|
||||
action.Invoke(session, packet, id);
|
||||
}
|
||||
|
||||
void MakePacket<T>(PacketSession session, ArraySegment<byte> buffer, ushort id) where T : IMessage, new()
|
||||
void MakePacket<T>(PacketSession session, ArraySegment<byte> buffer, ushort id) where T : new()
|
||||
{
|
||||
T pkt = new T();
|
||||
// T pkt = new T();
|
||||
//pkt.MergeFrom(buffer.Array, buffer.Offset + 4, buffer.Count - 4);
|
||||
|
||||
if (CustomHandler != null)
|
||||
{
|
||||
CustomHandler.Invoke(session, pkt, id);
|
||||
CustomHandler.Invoke(session, buffer, id);
|
||||
}
|
||||
else
|
||||
{
|
||||
Action<PacketSession, IMessage> action = null;
|
||||
Action<PacketSession, ArraySegment<byte>> action = null;
|
||||
if (_handler.TryGetValue(id, out action))
|
||||
action.Invoke(session, pkt);
|
||||
action.Invoke(session, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public Action<PacketSession, IMessage> GetPacketHandler(ushort id)
|
||||
public Action<PacketSession, ArraySegment<byte>> GetPacketHandler(ushort id)
|
||||
{
|
||||
Action<PacketSession, IMessage> action = null;
|
||||
Action<PacketSession, ArraySegment<byte>> action = null;
|
||||
if (_handler.TryGetValue(id, out action))
|
||||
return action;
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user