Exemplo n.º 1
0
        /// <summary>
        /// Listens on the socket for a parseable class to read.
        /// </summary>
        /// <typeparam name="T">Class to be parsed; must have a ParserAttribute.</typeparam>
        /// <param name="result">Parsed class.</param>
        public void Listen <T>(Action <T> result)
            where T : class, IParseable, new()
        {
            // Instantiate the parser associated with the type parameter
            var instance = ParserHelpers.CreateParser <T>();

            // Create the parser container
            _parseListeners.Add(new ParserContainer
            {
                IsMatch  = line => instance.IsMatch(line),
                Parse    = line => instance.Parse(line),
                Callback = parsed => result((T)parsed)
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Send a command to the server, and wait for the response before proceeding.  Expect the result to be parsable into T.
        /// </summary>
        /// <typeparam name="T">Type to parse the command as.</typeparam>
        /// <param name="command">Command to send to the server.</param>
        /// <exception cref = "System.FormatException" > Unable to parse response </ exception >
        /// <exception cref = "System.AggregateException" >Connection exceptions</ exception >
        public async Task <T> SendCommandAsync <T>(string command)
            where T : class, IParseable, new()
        {
            string response = await SendCommandAsync(command);

            // Se comment about TaskCreationOptions.RunContinuationsAsynchronously in SendComandAsync<string>
            var source    = new TaskCompletionSource <T>();
            var instance  = ParserHelpers.CreateParser <T>();
            var container = new ParserContainer
            {
                IsMatch = line => instance.IsMatch(line),
                Parse   = line => instance.Parse(line),
            };


            object parsed;

            if (!container.TryParse(response, out parsed))
            {
                throw new FormatException("Failed to parse server response");
            }
            return((T)parsed);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Send a command to the server, and wait for the response before proceeding.  Expect the result to be parseable into T.
        /// </summary>
        /// <typeparam name="T">Type to parse the command as.</typeparam>
        /// <param name="command">Command to send to the server.</param>
        public async Task <T> SendCommandAsync <T>(string command)
            where T : class, IParseable, new()
        {
            Monitor.Enter(_lock);
            var source   = new TaskCompletionSource <T>();
            var instance = ParserHelpers.CreateParser <T>();

            var container = new ParserContainer
            {
                IsMatch  = line => instance.IsMatch(line.Body),
                Parse    = line => instance.Parse(line.Body),
                Callback = parsed => source.SetResult((T)parsed)
            };

            _pendingCommands.Add(++_packetId, container.TryCallback);
            var packet = new RCONPacket(_packetId, PacketType.ExecCommand, command);

            Monitor.Exit(_lock);

            await SendPacketAsync(packet);

            return(await source.Task);
        }