/// <summary> /// 注册消息,必须实现接口IMsgReceiver /// </summary> public static void RegisterLogicMsg(this IMsgReceiver self, int msgName, Action <IMsgParam> callback = null) { if (msgName == 0) { return; } if (!mMsgHandlerDict.ContainsKey(msgName)) { mMsgHandlerDict[msgName] = new List <LogicMsgHandler>(); } List <LogicMsgHandler> handlers = mMsgHandlerDict[msgName]; int handlerCount = handlers.Count; // 防止重复注册 for (int i = 0; i < handlerCount; i++) { LogicMsgHandler handler = handlers[i]; if (handler.Receiver == self && handler.Callback == callback) { return; } } handlers.Add(new LogicMsgHandler(self, callback)); }
//订阅消息 public void Subscribe(string msg, IMsgReceiver recevier) { if (recevier == null) { Debug.LogError("SubscribeMsg : recevier == null"); return; } if (Check(msg, recevier)) { Debug.LogFormat("SubscribeMsg: recevier has been subscribed ,msg={0},recevier={1}", msg, recevier.ToString()); } else { if (this.m_Msgs.ContainsKey(msg)) { this.m_Msgs[msg].Add(recevier); } else { List <IMsgReceiver> list = new List <IMsgReceiver>(); list.Add(recevier); this.m_Msgs.Add(msg, list); } } }
public static void SendMsg(string msgName, IMsgSender sender, IMsgReceiver receiver, params object[] args) { lock (lockObj) { receiver.HandleMsg(msgName, sender, args); } }
/// <summary> /// 注册消息, /// 注意第一个参数,使用了C# this的扩展, /// 所以只有实现IMsgReceiver的对象才能调用此方法 /// </summary> public static void RegisterLogicMsg(this IMsgReceiver self, string msgName, VoidDelegate.WithParams callback) { if (string.IsNullOrEmpty(msgName)) { Debug.Log("消息系统 【注册】:" + msgName + " is Null or Empty"); return; } if (null == callback) { Debug.Log("消息系统 【注册】:" + msgName + " callback is Null"); return; } if (!mMsgHandlerDict.ContainsKey(msgName)) { mMsgHandlerDict [msgName] = new List <LogicMsgHandler>(); } var handlers = mMsgHandlerDict [msgName]; // 防止重复注册 foreach (var handler in handlers) { if (handler.receiver == self && handler.callback == callback) { Debug.Log("消息系统 【注册】:" + msgName + " 已经注册!"); return; } } handlers.Add(new LogicMsgHandler(self, callback)); }
/// <summary> /// 注册消息接收者 /// </summary> /// <param name="msgReceiver"></param> /// <param name="msgNames"></param> public static void RegisterReceiver(IMsgReceiver msgReceiver, params string[] msgNames) { lock (lockObj) { if (msgNamesMap.ContainsKey(msgReceiver)) { throw new System.Exception("MsgReceiver has already been registered!"); } if (msgNames == null || msgNames.Length == 0) { throw new System.Exception("Msg names associated with the msg receiver can not be empty."); } msgNamesMap.Add(msgReceiver, msgNames); foreach (string msgName in msgNames) { List <IMsgReceiver> msgReceiverList = null; if (!msgReceiverMap.ContainsKey(msgName)) { msgReceiverList = new List <IMsgReceiver>(); msgReceiverMap.Add(msgName, msgReceiverList); } else { msgReceiverList = msgReceiverMap[msgName]; } msgReceiverList.Add(msgReceiver); } } }
/// <summary> /// 注销逻辑消息 /// </summary> public static void UnRegisterLogicMsg(this IMsgReceiver self, string msgName, VoidDelegate.WithParams callback) { if (string.IsNullOrEmpty(msgName)) { QPrint.FrameworkError("UnRegisterMsg is Null or Empty"); return; } if (null == callback) { QPrint.FrameworkError("RegisterMsg callback is Null"); return; } var handlers = mMsgHandlerDict [msgName]; int handlerCount = handlers.Count; // 删除List需要从后向前遍历 for (int index = handlerCount - 1; index >= 0; index--) { var handler = handlers [index]; if (handler.receiver == self && handler.callback == callback) { handlers.Remove(handler); break; } } }
public static void UnRegisterMsg(this IMsgReceiver self, string msgName, QVoidDelegate.WithParams callback, QMsgChannel channel = QMsgChannel.Global) { if (CheckStrNullOrEmpty(msgName) || CheckDelegateNull(callback)) { return; } // 添加通道 if (!mMsgHandlerDict.ContainsKey(channel)) { Debug.LogError("Channel:" + channel.ToString() + " doesn't exist"); return; } var handlers = mMsgHandlerDict [channel] [msgName]; int handlerCount = handlers.Count; // 删除List需要从后向前遍历 for (int index = handlerCount - 1; index >= 0; index--) { var handler = handlers [index]; if (handler.receiver == self && handler.callback == callback) { handlers.Remove(handler); break; } } }
public MsgInfo(string msgName, IMsgSender sender, IMsgReceiver receiver, object[] args) { this.MsgName = msgName; this.Sender = sender; this.Receiver = receiver; this.Args = args; }
/// <summary> /// 注册 /// 扩展方法必须为静态 /// </summary> /// <param name="self">Self.</param> /// <param name="msg">Message.</param> /// <param name="callBack">Call back.</param> public static void RegisterSelf(this IMsgReceiver self, string msg, PublicDelegate.VoidDelegateWithParam callBack) { if (string.IsNullOrEmpty(msg)) { Debug.LogError("消息不能为空"); return; } if (null == callBack) { Debug.LogError("回调不能为空"); return; } if (false == mMsgTrapDict.ContainsKey(msg)) { mMsgTrapDict.Add(msg, new List <MsgTrap>()); } var handers = mMsgTrapDict [msg]; foreach (var item in handers) { if (item.receiver == self && item.callBack == callBack) { Debug.Log(self + "has already a" + msg); return; } } handers.Add(new MsgTrap(self, callBack)); }
/// <summary> /// 注册消息 /// </summary> public static void RegisteMsg(this IMsgReceiver self, string msgName, UnityAction <object> handler) { if (string.IsNullOrEmpty(msgName)) { Debug.LogError("要注册的消息名为空"); return; } if (handler == null) { Debug.LogError("要注册的消息处理方法为空"); return; } if (!_msgHandlerDict.ContainsKey(msgName)) { _msgHandlerDict.Add(msgName, new List <UnityAction <object> >()); } //防止重复注册 List <UnityAction <object> > handlers = _msgHandlerDict[msgName]; for (int i = 0; i < handlers.Count; i++) { if (handlers[i] == handler) { Debug.Log("重复注册"); return; } } //添加进列表里 handlers.Add(handler); }
/// <summary> /// 注销指定消息的指定处理方法 /// </summary> public static void UnregisteMsg(this IMsgReceiver self, string msgName, UnityAction <object> handler) { if (string.IsNullOrEmpty(msgName)) { Debug.LogError("要注销的消息名为空"); return; } if (handler == null) { Debug.LogError("要注销的消息处理方法为空"); return; } if (!_msgHandlerDict.ContainsKey(msgName)) { Debug.LogError("要注销的消息不在列表中"); return; } List <UnityAction <object> > handlers = _msgHandlerDict[msgName]; for (int i = handlers.Count - 1; i >= 0; i--) { if (handlers[i] == handler) { handlers.RemoveAt(i); } } if (handlers.Count == 0) { _msgHandlerDict.Remove(msgName); } }
/// <summary> /// 移除掉某个对象身上的事件处理 /// 一般在他的OnDestory里执行 /// </summary> /// <param name="self">this 扩展,扩展对象必须是IMsgReceiver 的实例</param> /// <param name="MsgType">消息类型</param> public static void RemoveMessageHandler(this IMsgReceiver self, ECustomMessageType MsgType) { if (MsgType == ECustomMessageType.NULL) { CLOG.E("error msg type is {0}", MsgType); return; } // 没有注册该消息时的处理 if (!m_MsgHandlerList.ContainsKey(MsgType)) { return; } // 得到所有注册的处理 var Handlers = m_MsgHandlerList[MsgType]; // 得到数量 var HandlerCount = Handlers.Count; // 倒序遍历,防止删除引起的循环异常 for (int i = HandlerCount - 1; i >= 0; i--) { var Handler = Handlers[i]; // 存在处理对象才调用 if (Handler.m_Receiver == self) { Handlers.Remove(Handler); } } }
/// <summary> /// 注册消息处理 /// </summary> /// <param name="self">this扩展,this必须是 ImsgReceiver 的实例</param> /// <param name="MsgType"> 消息名 </param> /// <param name="Callback"> 回调函数,无返回值,支持任意类型,任意长度的参数 </param> public static void AddMessageHandler(this IMsgReceiver self, ECustomMessageType MsgType, DelegateCustomMessage Callback) { if (MsgType == ECustomMessageType.NULL) { CLOG.E("error msg type is {0}", MsgType); return; } if (Callback == null) { CLOG.E("none call back!!", MsgType); return; } if (!m_MsgHandlerList.ContainsKey(MsgType)) { // 如果不包含这个Key,那么就创建他 m_MsgHandlerList[MsgType] = new List <LogicMsgHandler>(); } // 防止反复注册 foreach (var item in m_MsgHandlerList[MsgType]) { if (item.m_Receiver == self && item.m_Callback == Callback) { return; } } // 注册 m_MsgHandlerList[MsgType].Add(new LogicMsgHandler(self, Callback)); }
public static void UnRegisterMsg(this IMsgReceiver self, string msgName) { if (CheckStrNullOrEmpty(msgName)) { return; } if (!mMsgHandlerDict.ContainsKey(QMsgChannel.Global)) { Debug.LogError("Channel:" + QMsgChannel.Global.ToString() + " doesn't exist"); return; } var handlers = mMsgHandlerDict[QMsgChannel.Global] [msgName]; int handlerCount = handlers.Count; // 删除List需要从后向前遍历 for (int index = handlerCount - 1; index >= 0; index--) { var handler = handlers [index]; if (handler.receiver == self) { handlers.Remove(handler); break; } } }
public static void RegisterLogicMsg(this IMsgReceiver self, string msgName,QVoidDelegate.WithParams callback,QMsgChannel channel = QMsgChannel.Global) { if (CheckStrNullOrEmpty (msgName)||CheckDelegateNull(callback)) { return; } // 添加消息通道 if (!mMsgHandlerDict.ContainsKey (channel)) { mMsgHandlerDict [channel] = new Dictionary<string, List<QMsgHandler>> (); } // 略过 if (!mMsgHandlerDict[channel].ContainsKey (msgName)) { mMsgHandlerDict[channel] [msgName] = new List<QMsgHandler> (); } // 看下这里 var handlers = mMsgHandlerDict [channel][msgName]; // 略过 // 防止重复注册 foreach (var handler in handlers) { if (handler.receiver == self && handler.callback == callback) { Debug.LogWarning ("RegisterMsg:" + msgName + " ayready Register"); return; } } // 再看下这里 handlers.Add (new QMsgHandler (self, callback)); }
//检查订阅 public bool Check(string msg, IMsgReceiver recevier) { if (m_Msgs.ContainsKey(msg)) { var list = m_Msgs[msg]; return(list.Contains(recevier)); } return(false); }
public object BuildItem(object caller, Codon codon, System.Collections.ArrayList subItems) { IMsgReceiver receiver = MsgMonitor.GetReceiver(codon.ID); if (receiver == null) { receiver = new GameConnecter(codon.ID); } return(receiver); }
public void UnSubscribe(IMsgReceiver recevier) { if (recevier == null) { Debug.LogError("UnSubscribeMsg: recevier == null"); return; } foreach (var iter in m_Msgs) { iter.Value.Remove(recevier); } }
// 取消订阅消息 public void UnSubscribe(string msg, IMsgReceiver recevier) { if (recevier == null) { Debug.LogError("UnSubscribeMsg: recevier == null"); return; } if (Check(msg, recevier)) { this.m_Msgs[msg].Remove(recevier); } }
/// <summary> /// 注册消息接收者到列表 /// </summary> /// <param name="receiver">接收者</param> public static void RegisteReceiver(IMsgReceiver receiver) { if (receiver == null) { throw new ArgumentNullException("receiver"); } if (receiverList.ContainsKey(receiver.ID)) { throw new Exception(receiver.ID + "接收者已存在,请更换ID。"); } receiverList[receiver.ID] = receiver; }
/// <summary> /// 移除指定消息的接收者 /// </summary> /// <param name="msg"></param> /// <param name="receiver"></param> public static void RemoveMsgReceiver(uint msg, IMsgReceiver receiver) { if (!msgReceiverList.ContainsKey(msg)) { return; } if (!msgReceiverList[msg].Contains(receiver)) { return; } msgReceiverList[msg].Remove(receiver); }
/// <summary> /// 注册指定消息的接收者 /// </summary> /// <param name="msg"></param> /// <param name="receiver"></param> public static void RegisteMsgReceiver(uint msg, IMsgReceiver receiver) { if (!msgReceiverList.ContainsKey(msg)) { msgReceiverList[msg] = new List <IMsgReceiver>(); } if (msgReceiverList[msg].Contains(receiver)) { //throw new Exception(receiver.ID + "已订阅 " + msg + " 消息,请不要重复订阅。"); return; } msgReceiverList[msg].Add(receiver); }
/// <summary> /// 注销指定消息 /// </summary> public static void UnregisteMsg(this IMsgReceiver self, string msgName) { if (string.IsNullOrEmpty(msgName)) { Debug.LogError("要注销的消息名为空"); return; } if (!_msgHandlerDict.ContainsKey(msgName)) { Debug.LogError("要注销的消息不在列表中"); return; } _msgHandlerDict[msgName].Clear(); _msgHandlerDict.Remove(msgName); }
/// <summary> /// 注册消息, /// 注意第一个参数,使用了C# this的扩展, /// 所以只有实现IMsgReceiver的对象才能调用此方法 /// </summary> public static void RegisterGlobalMsg(this IMsgReceiver self, string msgName, QVoidDelegate.WithParams callback) { // 略过 if (string.IsNullOrEmpty(msgName)) { Debug.LogError("RegisterMsg:" + msgName + " is Null or Empty"); return; } // 略过 if (null == callback) { Debug.LogError("RegisterMsg:" + msgName + " callback is Null"); return; } // 添加消息通道 if (!mMsgHandlerDict.ContainsKey(QMsgChannel.Global)) { mMsgHandlerDict [QMsgChannel.Global] = new Dictionary <string, List <QMsgHandler> > (); } // 略过 if (!mMsgHandlerDict[QMsgChannel.Global].ContainsKey(msgName)) { mMsgHandlerDict[QMsgChannel.Global] [msgName] = new List <QMsgHandler> (); } // 看下这里 var handlers = mMsgHandlerDict [QMsgChannel.Global][msgName]; // 略过 // 防止重复注册 foreach (var handler in handlers) { if (handler.receiver == self && handler.callback == callback) { Debug.LogWarning("RegisterMsg:" + msgName + " ayready Register"); return; } } // 再看下这里 handlers.Add(new QMsgHandler(self, callback)); }
/// <summary> /// 注销消息,必须实现接口IMsgReceiver /// </summary> public static void UnRegisterLogicMsg(this IMsgReceiver self, int msgName, Action <IMsgParam> callback) { if (msgName == 0) { return; } List <LogicMsgHandler> handlers = mMsgHandlerDict[msgName]; for (int i = handlers.Count - 1; i >= 0; i--) { LogicMsgHandler handler = handlers[i]; if (handler.Receiver == self && handler.Callback == callback) { handlers.Remove(handler); break; } } }
/// <summary> /// 注销消息 /// 注意第一个参数,使用了C# this的扩展, /// 所以只有实现IMsgReceiver的对象才能调用此方法 /// </summary> public static void UnRegisterLogicMsg(this IMsgReceiver self, string msgName, Action <object[]> callback) { if (msgName.IsNullOrEmpty() || null == callback) { return; } var handlers = mMsgHandlerDict[msgName]; // 删除List需要从后向前遍历 for (var index = handlers.Count - 1; index >= 0; index--) { var handler = handlers[index]; if (handler.Receiver == self && handler.Callback == callback) { handlers.Remove(handler); break; } } }
/// <summary> /// 解除注册消息接收者 /// </summary> /// <param name="msgReceiver"></param> public static void UnregisterReceiver(IMsgReceiver msgReceiver) { lock (lockObj) { if (!msgNamesMap.ContainsKey(msgReceiver)) { throw new System.Exception("MsgReceiver has not been registered yet!"); } string[] msgNames = msgNamesMap[msgReceiver]; msgNamesMap.Remove(msgReceiver); foreach (string msgName in msgNames) { if (!msgReceiverMap.ContainsKey(msgName)) { throw new System.Exception("Unexpected status : msg name(" + msgName + ") can not be found in msgReceiverMap"); } msgReceiverMap[msgName].Remove(msgReceiver); } } }
/// <summary> /// 移除一个对象身上建立的所有事件监听 /// </summary> /// <param name="self">this 扩展,必须是IMsgReceiver的实例 </param> public static void RemoveAllMessageHandler(this IMsgReceiver self) { // 遍历所有事件注册 foreach (var item in m_MsgHandlerList) { // 得到数量 var HandlerCount = item.Value.Count; // 倒序遍历,防止删除引起的循环异常 for (int i = HandlerCount - 1; i >= 0; i--) { var Handler = item.Value[i]; // 存在处理对象才调用 if (Handler.m_Receiver == self) { item.Value.Remove(Handler); } } } }
/// <summary> /// 注册消息, /// 注意第一个参数,使用了C# this的扩展, /// 所以只有实现IMsgReceiver的对象才能调用此方法 /// </summary> public static void RegisterLogicMsg(this IMsgReceiver self, string msgName, Action <object[]> callback) { // 略过 if (string.IsNullOrEmpty(msgName)) { Log.W("RegisterMsg:" + msgName + " is Null or Empty"); return; } // 略过 if (null == callback) { Log.W("RegisterMsg:" + msgName + " callback is Null"); return; } // 略过 if (!mMsgHandlerDict.ContainsKey(msgName)) { mMsgHandlerDict[msgName] = new List <LogicMsgHandler>(); } // 看下这里 var handlers = mMsgHandlerDict[msgName]; // 略过 // 防止重复注册 foreach (var handler in handlers) { if (handler.Receiver == self && handler.Callback == callback) { Log.W("RegisterMsg:" + msgName + " ayready Register"); return; } } // 再看下这里 handlers.Add(new LogicMsgHandler(self, callback)); }
/// <summary> /// 注册消息, /// 注意第一个参数,使用了C# this的扩展, /// 所以只有实现IMsgReceiver的对象才能调用此方法 /// </summary> public static void RegisterLogicMsg(this IMsgReceiver self, string msgName, VoidDelegate.WithParams callback) { // 略过 if (string.IsNullOrEmpty(msgName)) { QPrint.FrameworkWarn("RegisterMsg:" + msgName + " is Null or Empty"); return; } // 略过 if (null == callback) { QPrint.FrameworkWarn("RegisterMsg:" + msgName + " callback is Null"); return; } // 略过 if (!mMsgHandlerDict.ContainsKey(msgName)) { mMsgHandlerDict [msgName] = new List <LogicMsgHandler> (); } // 看下这里 var handlers = mMsgHandlerDict [msgName]; // 略过 // 防止重复注册 foreach (var handler in handlers) { if (handler.receiver == self && handler.callback == callback) { QPrint.FrameworkWarn("RegisterMsg:" + msgName + " ayready Register"); return; } } // 再看下这里 handlers.Add(new LogicMsgHandler(self, callback)); }
/// <summary> /// 注册指定消息的接收者 /// </summary> /// <param name="msg"></param> /// <param name="receiver"></param> public static void RegisteMsgReceiver(uint msg, IMsgReceiver receiver) { if (!msgReceiverList.ContainsKey(msg)) { msgReceiverList[msg] = new List<IMsgReceiver>(); } if (msgReceiverList[msg].Contains(receiver)) { //throw new Exception(receiver.ID + "已订阅 " + msg + " 消息,请不要重复订阅。"); return; } msgReceiverList[msg].Add(receiver); }
/* * VoidDelegate.WithParams 是一种委托 ,定义是这样的 * * public class VoidDelegate{ * public delegate void WithParams(params object[] paramList); * } */ public QMsgHandler(IMsgReceiver receiver,QVoidDelegate.WithParams callback) { this.receiver = receiver; this.callback = callback; }
/// <summary> /// Инициализирует поля brainsMessageReceiver и brainsNotifier. /// </summary> /// <param name="mr">Интерфейс IMsgReceiver</param> /// <param name="notifier">Интерфейл INotifier</param> /// <param name="allmessagesform">Интерфейс IAllMessagesForm</param> /// <returns>True, если инициализация успешна, иначе false.</returns> public bool InitBrain(IMsgReceiver mr, INotifier notifier, IAllMessagesForm allmessagesform) { if (mr == null || notifier == null || allmessagesform == null) return false; brainsMessageReceiver = mr; brainsNotifier = notifier; brainsAllMessagesForm = allmessagesform; isInit = true; return true; }