/// <summary> /// Retrieves the creation delegate for a particular event type and subtype ID /// </summary> public PacketCreationDelegate GetHandlerDelegate(int typeID, int subTypeID) { PacketCreationDelegate handler = null; Dictionary <int, PacketCreationDelegate> map; // the map we look in for the handler int type = typeID; if (subTypeID == 0 || typeID == (int)PacketType.PacketGenericMessage) // then this handler is found in the main map { map = m_HandlerMap; } else if (m_HandlerSubtypeMaps.TryGetValue(typeID, out map)) //this handler sb found in the sub types map { type = subTypeID; } else { return(null); } if (map.TryGetValue(type, out handler)) { return(handler); } return(null); }
/// <summary> /// Registers an action handler, based on a event type ID and subtype ID /// </summary> public bool RegisterHandler(int typeID, int subType, PacketCreationDelegate handlerMethod) { Dictionary <int, PacketCreationDelegate> map; // the map we store the handler in PacketCreationDelegate handler = GetHandlerDelegate(typeID, subType); if (handler == null) { handler = handlerMethod; if (subType == 0) { lock (PrimaryHandlerMap) { PrimaryHandlerMap.Add(typeID, handlerMethod); } } else { lock (SubtypeHandlerMap) { if (!SubtypeHandlerMap.TryGetValue(typeID, out map)) { map = new Dictionary <int, PacketCreationDelegate>(); // map doesnt exist yet. add it. SubtypeHandlerMap.Add(typeID, map); } map.Add(subType, handler); } } return(true); } return(false); }
/// <summary> /// Removes a previously registered delegate from the handler map /// </summary> public void UnregisterHandler(int typeID, int subTypeID, PacketCreationDelegate handlerMethod) { PacketCreationDelegate handler = GetHandlerDelegate(typeID); if (handler == null) { return; } handler -= handlerMethod; if (subTypeID == 0) { lock (PrimaryHandlerMap) { PrimaryHandlerMap.Remove(typeID); if (handler.GetInvocationList().Length > 0) { PrimaryHandlerMap.Add(typeID, handler); } } } else { lock (SubtypeHandlerMap) { Dictionary <int, PacketCreationDelegate> map; if (SubtypeHandlerMap.TryGetValue(typeID, out map)) { map.Remove(subTypeID); if (handler.GetInvocationList().Length > 0) { map.Add(subTypeID, handler); } if (map.Count < 1) { SubtypeHandlerMap.Remove(typeID); } } } } }
/// <summary> /// Registers an action handler, based on a event type ID and subtype ID /// </summary> public bool RegisterHandler(int typeID, int subType, PacketCreationDelegate handlerMethod) { Dictionary<int, PacketCreationDelegate> map; // the map we store the handler in PacketCreationDelegate handler = GetHandlerDelegate(typeID, subType); if (handler == null) { handler = handlerMethod; if (subType == 0) { lock (PrimaryHandlerMap) { PrimaryHandlerMap.Add(typeID, handlerMethod); } } else { lock (SubtypeHandlerMap) { if (!SubtypeHandlerMap.TryGetValue(typeID, out map)) { map = new Dictionary<int, PacketCreationDelegate>(); // map doesnt exist yet. add it. SubtypeHandlerMap.Add(typeID, map); } map.Add(subType, handler); } } return true; } return false; }
/// <summary> /// Registers an action handler, based on a type ID /// </summary> public bool RegisterHandler(int eventTypeID, PacketCreationDelegate handlerMethod) { return RegisterHandler(eventTypeID, 0, handlerMethod); }
/// <summary> /// Removes a previously registered delegate from the handler map /// </summary> public void UnregisterHandler(int typeID, int subTypeID, PacketCreationDelegate handlerMethod) { PacketCreationDelegate handler = GetHandlerDelegate(typeID); if (handler == null) { return; } handler -= handlerMethod; if (subTypeID == 0) { lock (PrimaryHandlerMap) { PrimaryHandlerMap.Remove(typeID); if (handler.GetInvocationList().Length > 0) { PrimaryHandlerMap.Add(typeID, handler); } } } else { lock (SubtypeHandlerMap) { Dictionary<int, PacketCreationDelegate> map; if (SubtypeHandlerMap.TryGetValue(typeID, out map)) { map.Remove(subTypeID); if (handler.GetInvocationList().Length > 0) { map.Add(subTypeID, handler); } if (map.Count < 1) { SubtypeHandlerMap.Remove(typeID); } } } } }
/// <summary> /// Removes a previously registered delegate from the handler map /// </summary> public void UnregisterHandler(int typeID, PacketCreationDelegate handlerMethod) { UnregisterHandler(typeID, 0, handlerMethod); }
/// <summary> /// Registers an action handler, based on a type ID /// </summary> public bool RegisterHandler(int eventTypeID, PacketCreationDelegate handlerMethod) { return(RegisterHandler(eventTypeID, 0, handlerMethod)); }
/// <summary> /// Packets contain a PType (packet type) ID in the header. Based on this ID, incoming (and sometimes outgoing) the appropriate packet objects /// are instantiated. New packet type IDs must be registered with this method before the system knows about them. A quick way to do this is via /// this method as such: /// <para>Example Usage: RegisterPacketCreationDelegate((int)PacketType.ServerGreeting, delegate { return new PacketServerGreeting(); });</para> /// </summary> /// <returns>false, if @packetType ID is already registered for another delegate</returns> public static bool RegisterPacketCreationDelegate(int packetType, int packetSubType, PacketCreationDelegate packetContructionMethod) { return PacketCreationMap.RegisterHandler(packetType, packetSubType, packetContructionMethod); }
/// <summary> /// Packets contain a PType (packet type) ID in the header. Based on this ID, incoming (and sometimes outgoing) the appropriate packet objects /// are instantiated. New packet type IDs must be registered with this method before the system knows about them. A quick way to do this is via /// this method as such: /// <para>Example Usage: RegisterPacketCreationDelegate((int)PacketType.ServerGreeting, delegate { return new PacketServerGreeting(); });</para> /// </summary> /// <returns>false, if @packetType ID is already registered for another delegate</returns> public static bool RegisterPacketCreationDelegate(int packetType, PacketCreationDelegate packetContructionMethod) { return RegisterPacketCreationDelegate(packetType, 0, packetContructionMethod); }