public Queue <string> ExecuteCommand(string cmd) { try { var consoleOutput = new Queue <string>(); var rcon = new SourceRcon(); rcon.ServerOutput += consoleOutput.Enqueue; if (rcon.Connect(new IPEndPoint(_localhost, _port), _password)) { if (!rcon.Connected) { Thread.Sleep(50); if (!rcon.Connected) { Thread.Sleep(100); } } if (rcon.Connected) { rcon.ServerCommand(cmd); Thread.Sleep(100); return(consoleOutput); } throw new System.TimeoutException("The rcon connection timed out."); consoleOutput.Enqueue("The Rcon connection timed out."); return(consoleOutput); } consoleOutput.Enqueue("Command did not execute successfully."); return(consoleOutput); } catch (Exception e) { Logger.Log(e.ToString()); throw; } }
static void Main(string[] args) { string ipaddress, password, command; int port; bool interactive; if (args.Length > 0) { if (args.Length == 4) { interactive = false; ipaddress = args[0]; port = int.Parse(args[1]); password = args[2]; command = args[3]; } else { Console.WriteLine("To use in interactive mode, use no parameters."); Console.WriteLine("Else use parameters in the form: ip port password command"); Console.WriteLine("Enclose the command in \" marks if it is more than one word"); Console.WriteLine("E.g. sourcercon 192.168.0.5 27015 testpass \"say Testing!\""); return; } } else { interactive = true; Console.WriteLine("Enter IP Address:"); ipaddress = Console.ReadLine(); Console.WriteLine("Enter port:"); port = int.Parse(Console.ReadLine()); Console.WriteLine("Enter password:"******"Ready for commands:"); while (true) { Sr.ServerCommand(Console.ReadLine()); } } else { Sr.ServerCommand(command); Thread.Sleep(1000); return; } } else { Console.WriteLine("No connection!"); Thread.Sleep(1000); } }