Ejemplo n.º 1
0
        /// <summary>
        ///     Register a entity message handler for given entity id.
        /// </summary>
        /// <typeparam name="TMessageType">The type of the message.</typeparam>
        /// <param name="entityId">The entity id.</param>
        /// <param name="handler">The handler executed on received message of given entity id.</param>
        public void Register <TMessageType>(long entityId, EntityMessageHandler <TMessageType> handler) where TMessageType : IEntityMessage
        {
            if (!_entityMessageHandler.ContainsKey(entityId))
            {
                _entityMessageHandler.Add(entityId, new HashSet <MessageHandlerWrapper>(_wrapperComparer));
            }

            _entityMessageHandler[entityId].Add(MessageHandlerWrapper.Create(handler));
        }
Ejemplo n.º 2
0
 /// <summary>
 ///     Unregister a message handler for given entity id.
 /// </summary>
 /// <typeparam name="TMessageType">The type of the message.</typeparam>
 /// <param name="entityId">The entity id.</param>
 /// <param name="handler">The handler to unregister.</param>
 public void Unregister <TMessageType>(long entityId, EntityMessageHandler <TMessageType> handler) where TMessageType : IEntityMessage
 {
     if (_entityMessageHandler.ContainsKey(entityId))
     {
         var wrapper = MessageHandlerWrapper.Create(handler);
         if (_entityMessageHandler[entityId].Contains(wrapper))
         {
             _entityMessageHandler[entityId].Remove(wrapper);
         }
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        ///     Unregister a message handler for given entity id.
        /// </summary>
        /// <typeparam name="TMessageType">The type of the message.</typeparam>
        /// <param name="entityId">The entity id.</param>
        /// <param name="handler">The handler to unregister.</param>
        public void Unregister <TMessageType>(long entityId, EntityMessageHandler <TMessageType> handler) where TMessageType : IEntityMessage
        {
            var type = typeof(TMessageType).FullName;

            if (type == null)
            {
                throw new Exception($"Unable to get type of {typeof(TMessageType)}");
            }

            if (_entityMessageHandler.ContainsKey(entityId))
            {
                var wrapper = MessageHandlerWrapper.Create(handler);
                if (_entityMessageHandler.ContainsKey(entityId) && _entityMessageHandler[entityId].ContainsKey(type) && _entityMessageHandler[entityId][type].Contains(wrapper))
                {
                    _entityMessageHandler[entityId][type].Remove(wrapper);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Register a entity message handler for given entity id.
        /// </summary>
        /// <typeparam name="TMessageType">The type of the message.</typeparam>
        /// <param name="entityId">The entity id.</param>
        /// <param name="handler">The handler executed on received message of given entity id.</param>
        public void Register <TMessageType>(long entityId, EntityMessageHandler <TMessageType> handler) where TMessageType : IEntityMessage
        {
            var type = typeof(TMessageType).FullName;

            if (type == null)
            {
                throw new Exception($"Unable to get type of {typeof(TMessageType)}");
            }

            if (!_entityMessageHandler.ContainsKey(entityId))
            {
                _entityMessageHandler.Add(entityId, new Dictionary <string, HashSet <MessageHandlerWrapper> > {
                    { type, new HashSet <MessageHandlerWrapper>(_wrapperComparer) }
                });
            }

            if (!_entityMessageHandler[entityId].ContainsKey(type))
            {
                _entityMessageHandler[entityId].Add(type, new HashSet <MessageHandlerWrapper>(_wrapperComparer));
            }

            _entityMessageHandler[entityId][type].Add(MessageHandlerWrapper.Create(handler));
        }
Ejemplo n.º 5
0
 public static MessageHandlerWrapper Create <TMessageType>(EntityMessageHandler <TMessageType> handler) where TMessageType : IEntityMessage
 {
     return(new MessageHandlerWrapper {
         Action = (sender, message) => handler(sender, (TMessageType)message), HashCode = handler.Method.GetHashCode(), DeserializeAction = wrapper => Wrapper.GetContent <TMessageType>(wrapper)
     });
 }