using AxibugEmuOnline.Client.ClientCore; using System; using System.Collections.Generic; namespace AxibugEmuOnline.Client.Network { public class NetMsg { private static NetMsg instance = new NetMsg(); public static NetMsg Instance { get { return instance; } } private Dictionary> netEventDic = new Dictionary>(128); private Queue<(int, int, byte[])> queueNetMsg = new Queue<(int, int, byte[])>(); private NetMsg() { } #region RegisterMsgEvent public void RegNetMsgEvent(int cmd, Action 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() { callback }); } } #endregion #region UnregisterCMD public void UnregisterCMD(int evt, Action callback) { Delegate tempDelegate = callback; InterUnregisterCMD(evt, 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 EnqueueNesMsg(int cmd, int ERRCODE, byte[] arg) { queueNetMsg.Enqueue((cmd, ERRCODE, arg)); } public void DequeueNesMsg() { while (queueNetMsg.Count > 0) { (int, int, byte[]) msgData = queueNetMsg.Dequeue(); PostNetMsgEvent(msgData.Item1, msgData.Item2, msgData.Item3); } } public void PostNetMsgEvent(int cmd, int ERRCODE, byte[] arg) { List eventList = GetNetEventDicList(cmd); if (eventList != null) { foreach (Delegate callback in eventList) { try { ((Action)callback)(arg); } catch (Exception e) { App.log.Error(e.ToString()); } } } } #endregion /// /// 获取所有事件 /// /// /// private List GetNetEventDicList(int cmd) { if (netEventDic.ContainsKey(cmd)) { List tempList = netEventDic[cmd]; if (null != tempList) { return tempList; } } return null; } } }