HaoYueTunnel/ServerCore/NetWork/NetMsg.cs

105 lines
2.8 KiB
C#
Raw Normal View History

2023-05-25 18:30:22 +08:00
using ServerCore.Manager;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace ServerCore.NetWork
{
public class NetMsg
{
private static NetMsg instance = new NetMsg();
public static NetMsg Instance { get { return instance; } }
private Dictionary<int, List<Delegate>> netEventDic = new Dictionary<int, List<Delegate>>(128);
private NetMsg() { }
#region RegisterMsgEvent
public void RegNetMsgEvent(int cmd, Action<Socket, byte[]> callback)
{
InterRegNetMsgEvent(cmd, callback);
}
private void InterRegNetMsgEvent(int cmd, Delegate callback)
{
if (netEventDic.ContainsKey(cmd))
{
if (netEventDic[cmd].IndexOf(callback) < 0)
{
netEventDic[cmd].Add(callback);
}
}
else
{
netEventDic.Add(cmd, new List<Delegate>() { callback });
}
}
#endregion
#region UnregisterCMD
public void UnregisterCMD(int cmd, Action<Socket, byte[]> callback)
{
Delegate tempDelegate = callback;
InterUnregisterCMD(cmd, tempDelegate);
}
private void InterUnregisterCMD(int cmd, Delegate callback)
{
if (netEventDic.ContainsKey(cmd))
{
netEventDic[cmd].Remove(callback);
if (netEventDic[cmd].Count == 0) netEventDic.Remove(cmd);
}
}
#endregion
#region PostEvent
public void PostNetMsgEvent(int cmd, Socket arg1, byte[] arg2)
{
List<Delegate> eventList = GetNetEventDicList(cmd);
2023-06-15 15:45:58 +08:00
if (eventList == null)
return;
for (int i = 0; i < eventList.Count; i++)
2023-05-25 18:30:22 +08:00
{
2023-06-15 15:45:58 +08:00
Delegate callback = eventList[i];
try
{
((Action<Socket, byte[]>)callback)(arg1, arg2);
}
catch (Exception e)
2023-05-25 18:30:22 +08:00
{
2023-06-15 15:45:58 +08:00
ServerManager.g_Log.Error(e.Message);
2023-05-25 18:30:22 +08:00
}
}
}
#endregion
/// <summary>
/// 获取所有事件
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
private List<Delegate> GetNetEventDicList(int cmd)
{
if (netEventDic.ContainsKey(cmd))
{
List<Delegate> tempList = netEventDic[cmd];
if (null != tempList)
{
return tempList;
}
}
return null;
}
}
}