static void Run(Args input) { if (input.cscs_path != null) { // Console.WriteLine($"Input {input.cscs_path} ..."); // Console.WriteLine($"Probing 0 {csscript.cscs_path} ..."); csscript.cscs_path = Path.GetFullPath(input.cscs_path); } Console.WriteLine($"Probing {csscript.cscs_path} ..."); if (csscript.cscs_path == null || !File.Exists(csscript.cscs_path)) { Console.WriteLine("Probing cscs.exe ..."); if (File.Exists(csscript.default_cscs_path)) { csscript.cscs_path = csscript.default_cscs_path; } else if (File.Exists(csscript.default_cscs_path2)) { csscript.cscs_path = csscript.default_cscs_path2; } else if (File.Exists(csscript.default_cscs_path3)) { csscript.cscs_path = csscript.default_cscs_path3; } else { Console.WriteLine("Probing cscs.exe failed..."); } } else { Console.WriteLine("cscs.exe: " + csscript.cscs_path); } if (input.test) { if (csscript.cscs_path == null) { csscript.cscs_path = csscript.default_cscs_path; } Test.All(); } else { if (input.listen) { SocketServer.Listen(input); } else { Output.WriteLine(SyntaxProvider.ProcessRequest(input)); } } }
public static void Listen(Args processArgs) { try { var serverSocket = new TcpListener(IPAddress.Loopback, processArgs.port); serverSocket.Start(); if (processArgs.client != 0) { connections[processArgs.client] = true; Output.WriteLine("Monitor client: " + processArgs.client); } Task.Run(() => MonitorConnections(processArgs.timeout, requestShutdown: serverSocket.Stop)); Output.WriteLine($" >> Server (v{Assembly.GetExecutingAssembly().GetName().Version}) Started (port={processArgs.port})"); new Engine().Preload(); Output.WriteLine($" >> Syntax engine loaded"); while (true) { Output.WriteLine(" >> Waiting for client request..."); TcpClient clientSocket = serverSocket.AcceptTcpClient(); Output.WriteLine(" >> Accepted client..."); lock (connections) { try { Output.WriteLine(" >> Reading request..."); string request = clientSocket.ReadAllText(); var args = new Args(request.GetLines()); if (args.exit) { clientSocket.WriteAllText("Bye"); break; } else { if (args.client != 0) { connections[args.client] = true; // Output.WriteLine("Monitor client: " + args.client); } } Output.WriteLine(" >> Processing client request"); string response = SyntaxProvider.ProcessRequest(args); if (response != null) { clientSocket.WriteAllText(response); } } catch (Exception e) { Output.WriteLine(e.Message); } } } serverSocket.Stop(); Output.WriteLine(" >> exit"); } catch (SocketException e) { if (e.ErrorCode == 10048) { Output.WriteLine(">" + e.Message); } else { Output.WriteLine(e.Message); } } catch (Exception e) { Output.WriteLine(e); } }