/// <summary> /// 添加消息处理器 /// </summary> /// <typeparam name="M"></typeparam> /// <param name="reusable"></param> public void AddGCMsgHandler <M>(GCMsgHandler <M> handlerFunc, bool reusable) where M : BaseGCMsg { if (handlerFunc == null) { // 如果参数对象为空, // 则直接退出! return; } // 获取类型关键字 System.Type K = typeof(M); // 获取内置字典 IDictionary <Delegate, WrappedGCMsgH> innerDict = this._handlerDict.ContainsKey(K) ? this._handlerDict[K] : null; if (innerDict == null) { // 如果内置字典为空, // 则新建字典! innerDict = new Dictionary <Delegate, WrappedGCMsgH>(); this._handlerDict[K] = innerDict; } // 设置处理器 innerDict[handlerFunc] = new WrappedGCMsgH( handlerFunc, reusable ); }
/// <summary> /// 执行 GC 消息 /// </summary> public void ProcessGCMsg() { for (int i = 0; i < 8 && this._gcMsgQ.Count > 0; i++) { // 获取 GC 消息 BaseGCMsg gcMSG = this._gcMsgQ.Dequeue(); if (gcMSG == null) { // 如果 GC 消息为空, // 则直接退出! return; } // 获取消息类型 Type msgT = gcMSG.GetType(); // 获取内置字典 IDictionary <Delegate, WrappedGCMsgH> innerDict = this._handlerDict.ContainsKey(msgT) ? this._handlerDict[msgT] : null; if (innerDict == null) { // 如果内置字典为空, // 则直接跳过! continue; } // 关键字列表 List <Delegate> keyFuncList = null; foreach (Delegate keyFunc in innerDict.Keys) { // 获取消息处理器 WrappedGCMsgH wrappedH = innerDict[keyFunc]; if (wrappedH == null || wrappedH._hRef == null) { // 如果消息处理器为空, // 则直接跳过! continue; } // 记录调试信息 MsgLog.LOG.DebugFormat( "执行消息 {0}", gcMSG.GetType().Name ); // 处理 GC 消息 wrappedH._hRef.DynamicInvoke(gcMSG); if (!wrappedH._reusable) { if (keyFuncList == null) { keyFuncList = new List <Delegate>(); } keyFuncList.Add(keyFunc); } } foreach (Delegate keyFunc in keyFuncList) { // 从字典中移除关键字 innerDict.Remove(keyFunc); } if (innerDict.Count <= 0) { // 如果内置字典为空, // 则从根字典中移除消息类型 this._handlerDict.Remove(msgT); } } }