Beispiel #1
0
        /// <summary>
        /// Tries handling an IGC message, whether sent via broadcast or unicast
        /// </summary>
        /// <param name="msg">Message</param>
        /// <returns>Did the handling succeed?</returns>
        private bool TryHandle(MyIGCMessage msg)
        {
            if (!CommandParsers.ContainsKey(msg.Tag))
            {
                logger($"Can't find parser for tag {msg.Tag}");
                return(false);
            }
            var cmd = CommandParsers[msg.Tag](msg.Data);

            if (cmd == null)
            {
                logger($"Failed to parse message of tag {msg.Tag}, whose data is {msg.Data}");
                return(false);
            }
            if (!CommandHandlers.ContainsKey(msg.Tag))
            {
                logger($"Parsed message of tag {msg.Tag} but couldn't find handler");
                return(false);
            }
            var handled = CommandHandlers[msg.Tag].tryHandle(cmd, msg.Source);

            if (!handled)
            {
                logger($"Command handler couldn't handle message of tag {msg.Tag} whose inner data is {msg.Data}");
                return(false);
            }
            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Registers an ICommand parser
        /// </summary>
        /// <typeparam name="TCommand">Type of command, this is used to determine the parsing process</typeparam>
        /// <param name="messageType">Integer identifying message type, to differentiate between parsers</param>
        private void RegisterParser <TCommand>() where TCommand : ICommand, new()
        {
            var messageType = new TCommand().Tag;

            if (CommandParsers.ContainsKey(messageType))
            {
                throw new ArgumentException($"A parser for tag {messageType} is already registered");
            }
            CommandParsers.Add(messageType, (object obj) =>
            {
                TCommand cmd = new TCommand();
                if (cmd.Deserialize(obj))
                {
                    return(cmd);
                }
                return(null);
            });
        }