Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        /// <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);
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <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;
        }
Exemplo n.º 5
0
 /// <summary>
 ///  Registers an action handler, based on a type ID
 /// </summary>
 public bool RegisterHandler(int eventTypeID, PacketCreationDelegate handlerMethod)
 {
     return RegisterHandler(eventTypeID, 0, handlerMethod);
 }
Exemplo n.º 6
0
        /// <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);
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Removes a previously registered delegate from the handler map
 /// </summary>
 public void UnregisterHandler(int typeID, PacketCreationDelegate handlerMethod)
 {
     UnregisterHandler(typeID, 0, handlerMethod);
 }
Exemplo n.º 8
0
 /// <summary>
 ///  Registers an action handler, based on a type ID
 /// </summary>
 public bool RegisterHandler(int eventTypeID, PacketCreationDelegate handlerMethod)
 {
     return(RegisterHandler(eventTypeID, 0, handlerMethod));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Removes a previously registered delegate from the handler map
 /// </summary>
 public void UnregisterHandler(int typeID, PacketCreationDelegate handlerMethod)
 {
     UnregisterHandler(typeID, 0, handlerMethod);
 }
Exemplo n.º 10
0
 /// <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);
 }
Exemplo n.º 11
0
 /// <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);
 }