//When requested this function will be started as it's own thread to just output the consoles output public void handleConsoleOutput(object obj, JObject InArgs) { //Get the client object, and the IP from that object. string consoleLine = "null"; JObject jsonReponse; //Get client and the clients stream TcpClient client = (TcpClient)obj; NetworkStream cStream = client.GetStream(); //Keep outputting console while client is connected while (client.Connected) { string newconsoleLine = srvMgr.getServer(Convert.ToInt32(InArgs["id"])).consoleLine; if (newconsoleLine != consoleLine) //Only broadcast to the client if the message is actually new { consoleLine = newconsoleLine; //No longer need to debug the output from the server //Console.WriteLine(consoleLine); jsonReponse = JObject.FromObject(new { success = consoleLine }); outputResponse linqResponse = new outputResponse("ConsoleOutput", InArgs, jsonReponse); string response = JsonConvert.SerializeObject(linqResponse); Byte[] data = System.Text.Encoding.ASCII.GetBytes(response); Byte[] length = System.Text.Encoding.ASCII.GetBytes(response.Length.ToString("D9")); try //Send the message { cStream.Write(length, 0, length.Length); cStream.Write(data, 0, data.Length); } catch (Exception exception) { // client.Close(); } } } }
//Receive messages from the client and act accordingly public void HandleClientInput(object obj, String inData, NetworkStream cStream) { //Get client object TcpClient client = (TcpClient)obj; //Get the command and generally put out some debug outputs for the received input - most likely these console prints will be removed once a usable version is there. Console.WriteLine("RECEIVED DATA: {0}", inData); inputCommand command = JsonConvert.DeserializeObject<inputCommand>(inData); Console.WriteLine("COMMAND RECEIVED: {0} {1}", command.Function, command.Args["id"]); JObject jsonReponse; //Starts the server by ID, and returns whether it was successfull or not if (command.Function == "StartServer") { Boolean commandResponse = srvMgr.startServer(Int32.Parse(command.Args["id"].ToString())); jsonReponse = JObject.FromObject(new { success = commandResponse }); outputResponse linqResponse = new outputResponse(command.Function, command.Args, jsonReponse); string response = JsonConvert.SerializeObject(linqResponse); Byte[] data = System.Text.Encoding.ASCII.GetBytes(response); Byte[] length = System.Text.Encoding.ASCII.GetBytes(response.Length.ToString("D9")); cStream.Write(length, 0, length.Length); cStream.Write(data, 0, data.Length); } //Requests the console's output and sends the cleint said output else if (command.Function == "GetConsoleOutput") { Thread t = new Thread(() => handleConsoleOutput(client, command.Args)); t.Name = "Console Output Thread"; t.Start(); } //Stops server else if (command.Function == "StopServer") { srvMgr.stopServer(Int32.Parse(command.Args["id"].ToString())); } //Unwritten - will send the client a list of the servers they have access to (admins will see all) else if (command.Function == "GetServers") { } }