Esempio n. 1
0
        public static async Task <QueryResults> QueryAsync(RSAAsymmetricKey key, Target target, TimeSpan timeout)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            var tcs = new TaskCompletionSource <QueryResults>();

            var connection = new UdpClientConnection(GablarskiProtocol.Instance, key);

            connection.Start(MessageTypes.Unreliable);

            var cancelSources = new CancellationTokenSource(timeout);

            cancelSources.Token.Register(() => {
                tcs.TrySetCanceled();
                connection.Dispose();
            });

            connection.ConnectionlessMessageReceived += (sender, args) => {
                var results = args.Message as QueryServerResultMessage;
                if (results == null)
                {
                    return;
                }

                tcs.TrySetResult(new QueryResults(results.ServerInfo, results.Channels, results.Users));

                connection.Dispose();
            };

            await connection.SendConnectionlessMessageAsync(new QueryServerMessage(), target).ConfigureAwait(false);

            return(await tcs.Task.ConfigureAwait(false));
        }