/// <summary> /// 注册网络消息包处理方法 /// </summary> /// <param name="handler">要注册的网络消息包处理器</param> public void RegisterHandler(PacketHandlerBase handler) { if (handler == null) { Debug.LogError("要注册的网络消息包处理器为空"); } m_ReceivePacketPool.Subscribe(handler.Id, handler.Handle); }
/// <summary> /// 初始化网络频道辅助器 /// </summary> /// <param name="networkChannel">网络频道</param> public override void Initialize(NetworkChannel networkChannel) { m_NetworkChannel = networkChannel; m_EventManager = FrameworkEntry.Instance.GetManager <EventManager>(); // 反射注册包和包处理器 Type packetBaseType = typeof(SCPacketBase); Type packetHandlerBaseType = typeof(PacketHandlerBase); Assembly assembly = Assembly.GetExecutingAssembly(); Type[] types = assembly.GetTypes(); for (int i = 0; i < types.Length; i++) { if (!types[i].IsClass || types[i].IsAbstract) { continue; } //注册消息包 if (types[i].BaseType == packetBaseType) { PacketBase packetBase = (PacketBase)Activator.CreateInstance(types[i]); Type packetType = GetServerToClientPacketType(packetBase.Id); if (packetType != null) { Debug.LogError(string.Format("消息包Type: {0} 已经存在, 检查 '{1}' 或 '{2}'?.", packetBase.Id.ToString(), packetType.Name, packetBase.GetType().Name)); continue; } m_ServerToClientPacketTypes.Add(packetBase.Id, types[i]); } //注册消息包处理器 else if (types[i].BaseType == packetHandlerBaseType) { PacketHandlerBase packetHandler = (PacketHandlerBase)Activator.CreateInstance(types[i]); m_NetworkChannel.RegisterHandler(packetHandler); } } //注册全局事件监听 m_EventManager.Subscribe(NetworkConnectedEventArgs.EventId, OnNetworkConnected); m_EventManager.Subscribe(NetworkClosedEventArgs.EventId, OnNetworkClosed); m_EventManager.Subscribe(NetworkMissHeartBeatEventArgs.EventId, OnNetworkMissHeartBeat); m_EventManager.Subscribe(NetworkErrorEventArgs.EventId, OnNetworkError); m_EventManager.Subscribe(NetworkCustomErrorEventArgs.EventId, OnNetworkCustomError); }