internal void ParseFromBytes(byte[] bytes, SourceRcon parent) { int BPtr = 0; ArrayList stringcache; UTF8Encoding utf = new UTF8Encoding(); // First 4 bytes are ReqId. RequestId = BitConverter.ToInt32(bytes, BPtr); BPtr += 4; // Next 4 are server data. ServerDataReceived = (SERVERDATA_rec)BitConverter.ToInt32(bytes, BPtr); BPtr += 4; // string1 till /0 stringcache = new ArrayList(); while (bytes[BPtr] != 0) { stringcache.Add(bytes[BPtr]); BPtr++; } String1 = utf.GetString((byte[])stringcache.ToArray(typeof(byte))); BPtr++; // string2 till /0 stringcache = new ArrayList(); while (bytes[BPtr] != 0) { stringcache.Add(bytes[BPtr]); BPtr++; } String2 = utf.GetString((byte[])stringcache.ToArray(typeof(byte))); BPtr++; // Repeat if there's more data? if (BPtr != bytes.Length) { parent.OnError("Urk, extra data!"); } }
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); } }
internal void ParseFromBytes( byte[] bytes, SourceRcon parent ) { int BPtr = 0; ArrayList stringcache; UTF8Encoding utf = new UTF8Encoding(); //if (bytes.Length < 4) // return; // First 4 bytes are ReqId. RequestId = BitConverter.ToInt32( bytes, BPtr ); BPtr += 4; // Next 4 are server data. ServerDataReceived = (SERVERDATA_rec)BitConverter.ToInt32( bytes, BPtr ); BPtr += 4; // string1 till /0 stringcache = new ArrayList(); while (bytes[BPtr] != 0) { stringcache.Add( bytes[BPtr] ); BPtr++; } String1 = utf.GetString( (byte[])stringcache.ToArray( typeof( byte ) ) ); BPtr++; // string2 till /0 stringcache = new ArrayList(); while (bytes[BPtr] != 0) { stringcache.Add( bytes[BPtr] ); BPtr++; } String2 = utf.GetString( (byte[])stringcache.ToArray( typeof( byte ) ) ); BPtr++; // Repeat if there's more data? if (BPtr != bytes.Length) { parent.OnError( "Urk, extra data!" ); } }