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(); }
public bool ExitServer() { AutoResetEvent reset = new AutoResetEvent(false); bool success = false; client.ExecuteCommandAsync(new Commands.DoExit(), (s, e) => { success = e.Successful; reset.Set(); }); reset.WaitOne(); Disconnect(); return success; }
public void ExecuteCommandAsyncTest() { RconClient client = new RconClient(); Assert.IsTrue(client.Connect(Host, Port, Password)); Assert.IsTrue(client.IsConnected); client.ExecuteCommandAsync(new Rcon.Commands.ListPlayers(), (s, e) => Assert.IsTrue(e.Successful)); client.Disconnect(); }
public static async Task <List <string> > GetMaps(RconClient rconClient) { var mapList = new List <string>(); var mapResponse = await rconClient.ExecuteCommandAsync("maps *"); // create an array from response var responseArray = mapResponse.Split("\n"); // split by lines responseArray = responseArray.Skip(1).ToArray(); //shift responseArray = responseArray.Reverse().Skip(1).Reverse().ToArray(); // pop // get those map names only foreach (var item in responseArray) { var elements = item.Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (elements.Length > 2) { mapList.Add(elements[2].Remove(elements[2].Length - 4)); // 'Path' not working here because of workshop maps } } mapList.Sort(); return(mapList); }
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; } }