Esempio n. 1
0
        public void ConnectTestWrongHost()
        {
            RconClient client = new RconClient();

            Assert.IsTrue(client.Connect("google.de", Port, Password));
            Assert.IsTrue(client.IsConnected);

            client.Disconnect();
        }
Esempio n. 2
0
        public void ConnectTestInvalidHost()
        {
            RconClient client = new RconClient();

            Assert.IsTrue(client.Connect("InvalidHost", Port, Password));
            Assert.IsTrue(client.IsConnected);

            client.Disconnect();
        }
Esempio n. 3
0
        public void ConnectTestWrongPassword()
        {
            RconClient client = new RconClient();

            Assert.IsTrue(client.Connect(Host, Port, "password"));
            Assert.IsTrue(client.IsConnected);

            client.Disconnect();
        }
Esempio n. 4
0
        public void ExecuteLowPrioCommandAsyncTest()
        {
            RconClient client = new RconClient();

            Assert.IsTrue(client.Connect(Host, Port, Password));
            Assert.IsTrue(client.IsConnected);

            client.ExecuteLowPrioCommandAsync(new Rcon.Commands.ListPlayers(), (s, e) => Assert.IsTrue(e.Successful));

            client.Disconnect();
        }
Esempio n. 5
0
        public void DisconnectTest()
        {
            RconClient client = new RconClient();

            Assert.IsTrue(client.Connect(Host, Port, Password));
            Assert.IsTrue(client.IsConnected);

            client.Disconnect();

            Assert.IsFalse(client.IsConnected);
        }
Esempio n. 6
0
        public static bool IsServerResponding(ArkServerInfo Server)
        {
            bool       serverIsRunning = false;
            RconClient client          = new RconClient();

            try
            {
                client.Connect(Server.IPAddress, Int32.Parse(Server.RCONPort), Server.ServerPassword);
                if (client.IsConnected)
                {
                    serverIsRunning = true;
                }
            }
            catch (Exception ex)
            {
                serverIsRunning = false;
                //Console.WriteLine(DateTime.Now + ": " + Server.Name + " Exception:"  + ex.Message);
                Methods.Log(Server, DateTime.Now + ": " + Server.Name + " Exception:" + ex.Message);
            }
            client.Disconnect();


            return(serverIsRunning);
        }
Esempio n. 7
0
        public async Task <string[]> SendQueryAsync(StaticHelpers.QueryType type, string parameters = "")
        {
            try
            {
                await _activeQuery.WaitAsync();
                await WaitForAvailable();

                if (_needNewSocket)
                {
                    try
                    {
                        _rconClient?.Disconnect();
                    }
                    catch
                    {
                        // ignored
                    }

                    await Task.Delay(ConnectionTimeout);

                    _rconClient    = _rconClientFactory.CreateClient(_ipEndPoint);
                    _authenticated = false;
                    _needNewSocket = false;
                }

                using (LogContext.PushProperty("Server", _ipEndPoint.ToString()))
                {
                    _logger.LogDebug("Connecting to RCon socket");
                }

                await TryConnectAndAuthenticate().WithTimeout(ConnectionTimeout);

                var multiPacket = false;

                if (type == StaticHelpers.QueryType.COMMAND_STATUS)
                {
                    parameters  = "status";
                    multiPacket = true;
                }

                parameters = parameters.ReplaceUnfriendlyCharacters();
                parameters = parameters.StripColors();

                using (LogContext.PushProperty("Server", _ipEndPoint.ToString()))
                {
                    _logger.LogDebug("Sending query {Type} with parameters \"{Parameters}\"", type, parameters);
                }

                var response = await _rconClient.ExecuteCommandAsync(parameters, multiPacket)
                               .WithTimeout(ConnectionTimeout);

                using (LogContext.PushProperty("Server", $"{_ipEndPoint}"))
                {
                    _logger.LogDebug("Received RCon response {Response}", response);
                }

                var split = response.TrimEnd('\n').Split('\n');
                return(split.Take(split.Length - 1).ToArray());
            }

            catch (TaskCanceledException)
            {
                _needNewSocket = true;
                throw new NetworkException("Timeout while attempting to communicate with server");
            }

            catch (SocketException ex)
            {
                using (LogContext.PushProperty("Server", _ipEndPoint.ToString()))
                {
                    _logger.LogError(ex, "Socket exception encountered while attempting to communicate with server");
                }

                _needNewSocket = true;

                throw new NetworkException("Socket exception encountered while attempting to communicate with server");
            }

            catch (Exception ex) when(ex.GetType() != typeof(NetworkException) &&
                                      ex.GetType() != typeof(ServerException))
            {
                using (LogContext.PushProperty("Server", _ipEndPoint.ToString()))
                {
                    _logger.LogError(ex, "Could not execute RCon query {Parameters}", parameters);
                }

                throw new NetworkException("Unable to communicate with server");
            }

            finally
            {
                if (_activeQuery.CurrentCount == 0)
                {
                    _activeQuery.Release();
                }

                _lastQuery = DateTime.Now;
            }
        }
Esempio n. 8
0
 public void Disconnect()
 {
     client?.Disconnect();
 }