예제 #1
0
파일: RCON.cs 프로젝트: Nerus87/CoreRcon
        /// <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);
        }