Пример #1
0
 public ZoneMessageHandler(List <RedfoxExtension> extensions) : base(typeof(IZoneRequestMessage))
 {
     foreach (RedfoxExtension extension in extensions)
     {
         foreach (Type type in Core.ExtensionManager.extensionHandlers[extension.ExtensionName])
         {
             IRequestMessage message = Activator.CreateInstance(type) as IRequestMessage;
             this.handlers.Add(message.type, type);
             LogManager.GetCurrentClassLogger().Debug($"Added a new message handler {message.type} handled by class {type.Name}");
         }
     }
 }
Пример #2
0
        public IMessageHandler(Type _messageType)
        {
            this.messageType = _messageType;
            var types = Assembly.GetExecutingAssembly().GetTypes().Where(p => messageType.IsAssignableFrom(p) && !p.IsAbstract).ToList();

            this.handlers = new Dictionary <string, Type>();
            foreach (Type type in types)
            {
                IRequestMessage message = Activator.CreateInstance(type) as IRequestMessage;
                this.handlers.Add(message.type, type);
                LogManager.GetCurrentClassLogger().Debug($"Added a new message handler {message.type} handled by class {type.Name}");
            }
        }
Пример #3
0
        public virtual void HandleMessage(User user, string message_str)
        {
            IRequestMessage message = JsonConvert.DeserializeObject <GenericMessage>(message_str);

            if (this.handlers.ContainsKey(message.type))
            {
                Type            type          = this.handlers[message.type];
                IRequestMessage typed_message = JsonConvert.DeserializeObject(message_str, type) as IRequestMessage;
                typed_message.Handle(user);
            }
            else
            {
                LogManager.GetCurrentClassLogger().Warn($"Unknown message type {message.type} for target {message.target}");
            }
        }
Пример #4
0
        public void HandleMessage(User user, string message_str)
        {
            if (message_str.StartsWith("{") && message_str.EndsWith("}"))
            {
                IRequestMessage message = JsonConvert.DeserializeObject <GenericMessage>(message_str);
                switch (message.target)
                {
                case "global":
                {
                    lock (globalMessageHandler)
                    {
                        globalMessageHandler.HandleMessage(user, message_str);
                    }
                    break;
                }

                case "zone":
                {
                    lock (globalMessageHandler)
                    {
                        if (user.Zone != null)
                        {
                            user.Zone.messageHandler.HandleMessage(user, message_str);
                        }
                        else
                        {
                            throw new Exception("User requested to handle a zone message but is not in a zone");
                        }
                        break;
                    }
                }

                default:
                {
                    LogManager.GetCurrentClassLogger().Warn($"Unknown message target {message.target}");
                    break;
                }
                }
            }
            else
            {
                LogManager.GetCurrentClassLogger().Warn($"Malformed message received {message_str}");
            }
        }