Пример #1
0
        private async Task MainAsync()
        {
            var config = new RconClientConfiguration("127.0.0.1", 7600);

            try
            {
                using (var client = new RconClient(config))
                {
                    await client.ConnectAsync();

                    await client.StartClientAsync();

                    if (client.Socket.Connected)
                    {
                        if (await client.AuthenticateAsync("test"))
                        {
                            _ = Task.Factory.StartNew(async() =>
                            {
                                while (true)
                                {
                                    await client.ExecuteCommandAsync <RconResultPacket>("string ping");
                                    await Task.Delay(1000);
                                    Console.WriteLine(client.QueuedPackets.Count);
                                }
                            });

                            Console.WriteLine("Authentication successful!");
                            while (true)
                            {
                                var inp    = Console.ReadLine();
                                var result = await client.ExecuteCommandAsync <RconResultPacket>(inp);

                                Console.WriteLine(result.Result);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Authentication failed!");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.ReadLine();
        }
Пример #2
0
        private async Task TryConnectAndAuthenticate()
        {
            if (!_authenticated)
            {
                using (LogContext.PushProperty("Server", _ipEndPoint.ToString()))
                {
                    _logger.LogDebug("Authenticating to RCon socket");
                }

                await _rconClient.ConnectAsync().WithTimeout(ConnectionTimeout);

                _authenticated = await _rconClient.AuthenticateAsync(_password).WithTimeout(ConnectionTimeout);

                if (!_authenticated)
                {
                    using (LogContext.PushProperty("Server", _ipEndPoint.ToString()))
                    {
                        _logger.LogError("Could not login to server");
                    }

                    throw new ServerException("Could not authenticate to server with provided password");
                }
            }
        }