using UnityEngine; using System.Collections; using System.Collections.Generic; namespace XFramework.NetOuter { //消息回调事件 public delegate void TCPMsgEvent(ProtocolBase proto); public class MsgDistribution { //事件字典 Dictionary eventDic = new Dictionary(); //单次事件字典 Dictionary onceEventDic = new Dictionary(); //Queue msgList = new Queue(); Queue serverMsgList = new Queue(); //一次调用处理完所有消息,该方法由外部调用 public void Distribution() { while (serverMsgList.Count > 0) { DispatchMsgEvent(serverMsgList.Dequeue()); } } public void Enqueue(ExcuteParam excuteParam) { lock (serverMsgList) { serverMsgList.Enqueue(excuteParam); } } //事件分发 private void DispatchMsgEvent(ExcuteParam excuteParam) { string proName = excuteParam.protocolName; //Debug.Log("消息分发处理:"+proName); if (onceEventDic.ContainsKey(proName)) { onceEventDic[proName].NetExcute(excuteParam); onceEventDic[proName] = null; onceEventDic.Remove(proName); } else if (eventDic.ContainsKey(proName)) { eventDic[proName].NetExcute(excuteParam); } else { Debug.Log($"接到未注册的网络消息事件:{proName}"); } } //添加监听事件 public void AddListener(string name, INetExcute cb) { if (eventDic.ContainsKey(name)) { Debug.Log($"{name}事件覆盖注册"); eventDic[name] = cb; } else eventDic.Add(name, cb); } //添加单次监听事件,单次事件会在处理完后删除 public void AddOnceListener(string name, INetExcute cb) { if (onceEventDic.ContainsKey(name)) { Debug.Log($"{name}事件覆盖注册"); onceEventDic[name] = cb; } else onceEventDic.Add(name, cb); } //移除回调事件 public void RemoveListener(string name) { if (eventDic.ContainsKey(name)) { eventDic.Remove(name); } } } }