/// <summary> /// Asynchronously sends an RCON command to the server. /// Returns the response the server sent back. /// </summary> /// <param name="command">Command to send.</param> /// <returns>Server response.</returns> public async Task <string> RunCommand(string command) { INetworkSocket socket = new RconSocket(); RconMessenger messenger = new RconMessenger(socket); try { bool isConnected = await messenger.ConnectAsync(Address, Port); bool authenticated = await messenger.AuthenticateAsync(Password); if (authenticated) { return(await messenger.ExecuteCommandAsync(command)); } else { return("Authentication failed."); } } catch (SocketException exc) { return("Connection failed:\n" + exc.Message); } catch (AggregateException exc) { if (exc.InnerException.GetType() == typeof(SocketException)) { return("Connection failed:\n" + exc.InnerException); } else { return("Exception unaccounted for:\n" + exc.Message); } } catch (Exception exc) { return("Exception unaccounted for:\n" + exc.Message); } finally { messenger.CloseConnection(); socket.CloseConnection(); } }
private void Disconnect(INetworkSocket socket, RconMessenger messenger) { try { if (messenger != null) { messenger.CloseConnection(); } if (socket != null) { socket.CloseConnection(); } } catch (Exception e) { Console.WriteLine(e); } }