//TODO: 把这里的classID和functionID都改成结构或者枚举
        protected void TryRegisterRemoteMessageHandler <T>(int classID, int functionID, NetworkMessageHandlerDelegate <T> handler) where T : INetworkMessage
        {
            if (handler != null)
            {
                // Register to remote message central hub
                NetworkMessageCentralHub.Instance.RegisterHandler(classID, functionID, this, handler);
            }
#if UNITY_EDITOR
            else
            {
                if (LogUtil.ShowError != null)
                {
                    LogUtil.ShowError(string.Format("模块[{0}]注册消息的调用中有无效参数:消息类型[{1}] 监视器委托[{2}]",
                                                    GetType().Name, typeof(T).Name, StringUtility.ToString(handler)));
                }
            }
#endif
        }
Beispiel #2
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
        }