Example #1
0
        public void RegisterHandler <T>(int classID, int functionID, NetworkMessageProcessUnitBase module, NetworkMessageHandlerDelegate <T> handler) where T : INetworkMessage
        {
            Type msgType = typeof(T);

#if UNITY_EDITOR
            if (!msgType.IsValueType)
            {
                if (LogUtil.ShowError != null)
                {
                    LogUtil.ShowError(string.Format("网络消息结构体[{0}]必须定义为值类型(struct)", msgType.Name));
                }
                return;
            }
            object[] fieldsDefined = msgType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            if (fieldsDefined.Length <= 0)
            {
                if (LogUtil.ShowError != null)
                {
                    LogUtil.ShowError(string.Format("网络消息结构体[{0}]中没有定义任何成员,如果不需要向外通知任何参数的话,使用Notification", msgType.Name));
                }
                return;
            }
#endif
            if (module != null && handler != null)
            {
                long identity = ((long)classID << 32) | (uint)functionID;
#if UNITY_EDITOR
                if (messageHandlerTable.ContainsKey(identity) && messageHandlerTable[identity].Count > 0 && messageHandlerTable[identity][0].messageType != msgType)
                {
                    if (LogUtil.ShowError != null)
                    {
                        LogUtil.ShowError(string.Format("网络消息[{0}-{1}]绑定了多个不同的消息结构体!", classID, functionID));
                    }
                    return;
                }
#endif
                NetworkMessageHandler messageHandler = new NetworkMessageHandler(identity, msgType, handler);
                CollectionUtil.AddIntoTable(identity, messageHandler, messageHandlerTable);
                CollectionUtil.AddIntoTable(module, identity, messageHandler, moduleMessageRegisterTable);
#if UNITY_EDITOR
                if (LogUtil.ShowDebug != null)
                {
                    LogUtil.ShowDebug(string.Format("[网络消息中心]1.注册消息处理器:来自[{0}] 消息类型[{1}-{2}][{3}] 处理回调[{4}]", module.GetType().Name, classID, functionID, msgType.Name, StringUtility.ToString(handler)));
                }
#endif
            }
#if UNITY_EDITOR
            else
            {
                if (LogUtil.ShowError != null)
                {
                    LogUtil.ShowError(string.Format("注册网络消息处理器时传递的参数有[null]值:消息标识[{0}-{1}] 参数类型[{2}] 模块[{3}] 处理回调[{4}]",
                                                    classID, functionID, msgType.Name, module.GetType().Name, StringUtility.ToString(handler)));
                }
            }
#endif
        }
Example #2
0
        public void PauseAllHandlersBelongToModule(NetworkMessageProcessUnitBase module)
        {
            if (module != null)
            {
                Dictionary <long, List <NetworkMessageHandler> > tmpTable;
                if (moduleMessageRegisterTable.TryGetValue(module, out tmpTable))
                {
                    foreach (List <NetworkMessageHandler> handlerList in tmpTable.Values)
                    {
                        for (int i = 0; i < handlerList.Count; ++i)
                        {
                            handlerList[i].Deactivate();
                        }
                    }
                }
            }
#if UNITY_EDITOR
            else
            {
                if (LogUtil.ShowError != null)
                {
                    LogUtil.ShowError(string.Format("暂停网络消息处理器时传递的参数有[null]值:模块[{0}]", module.GetType().Name));
                }
            }
#endif
        }
Example #3
0
        public void UnregisterAllHandlersBelongToModule(NetworkMessageProcessUnitBase module)
        {
            if (module != null)
            {
                Dictionary <long, List <NetworkMessageHandler> > tmpTable;
                if (moduleMessageRegisterTable.TryGetValue(module, out tmpTable))
                {
                    foreach (KeyValuePair <long, List <NetworkMessageHandler> > tmpKV in tmpTable)
                    {
                        long identity = tmpKV.Key;
                        List <NetworkMessageHandler> tempList = tmpKV.Value;
                        for (int i = 0; i < tempList.Count; ++i)
                        {
                            CollectionUtil.RemoveFromTable(identity, tempList[i], messageHandlerTable);
#if UNITY_EDITOR
                            if (LogUtil.ShowDebug != null)
                            {
                                LogUtil.ShowDebug(string.Format("[网络消息中心]2.注销消息处理器:来自[{0}] 消息类型[{1}] 处理回调[{2}]", module.GetType().Name, tempList[i].messageType.Name, StringUtility.ToString(tempList[i].Handler)));
                            }
#endif
                        }
                    }
                    moduleMessageRegisterTable.Remove(module);
                }
            }
#if UNITY_EDITOR
            else
            {
                if (LogUtil.ShowError != null)
                {
                    LogUtil.ShowError(string.Format("注销网络消息处理器时传递的参数有[null]值:模块[{0}]", module.GetType().Name));
                }
            }
#endif
        }