private void Invoke <T>(NetworkMessageHandler handler, T msg) where T : INetworkMessage { if (handler.isActivated) { handler.Handle(msg); } }
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 }