/// <summary> /// 检测处理器链表中是否已经存在相同的处理器 /// </summary> protected bool ExistHandler(HandlerLink link, IMsgHandler msgHandler) { while (link != null) { if (link.Handler == msgHandler) { return(true); } link = link.Next; } return(false); }
protected void SendMsg(HandlerLink link, IMsgPack msg) { while (link != null) { if (link.Handler != null) { try { link.Handler.ProcessMsg(msg); } catch (System.Exception e) { this.LOG("SendMsg: " + e.Message); } } link = link.Next; } }
/// <summary> /// 取消指定消息处理器的注册 /// </summary> /// <param name="msgHandler">要取消注册的消息处理器</param> public void UnRegister(IMsgHandler msgHandler) { if (msgHandler == null) { return; } int[] keys = msgHandler.MsgIds; if (keys != null && keys.Length > 0) { foreach (int key in keys) { if (MsgHandlerMap.ContainsKey(key)) { HandlerLink link = MsgHandlerMap [key] as HandlerLink; HandlerLink up = null; while (link != null) { if (link.Handler == msgHandler) { link.Handler = null; if (up != null) { up.Next = link.Next; } else if (link.Next == null) { MsgHandlerMap.Remove(key); } else { MsgHandlerMap [key] = link.Next; } break; } else { up = link; link = link.Next; } } } } } }
/// <summary> /// 注册消息处理服务 /// </summary> /// <param name="msgHandler">消息处理器对象</param> public void Register(IMsgHandler msgHandler, int msgID) { if (msgHandler == null || msgID == 0) { return; } HandlerLink link = GetHandlerLink(msgID); if (link == null) { link = new HandlerLink(); link.Handler = msgHandler; MsgHandlerMap.Add(msgID, link); } else if (!ExistHandler(link, msgHandler)) { while (link.Next != null) { link = link.Next; } link.Next = new HandlerLink(); link.Next.Handler = msgHandler; } }