private static DaemonConfiguration LoadConfiguration() { var serializer = new XmlSerializer(typeof(DaemonConfiguration)); if (!File.Exists(ConfigurationPath)) { Console.WriteLine("[WARN/config] No configuration found, a default will be generated."); Console.WriteLine(); var config = new DaemonConfiguration() { ApiEndpoint = "http://API.NOT.SET/api/v1/data.php", ServerName = "SRV1" }; using (var stream = File.OpenWrite(ConfigurationPath)) { serializer.Serialize(stream, config); } return(config); } using (var stream = File.OpenRead(ConfigurationPath)) { return((DaemonConfiguration)serializer.Deserialize(stream)); } }
static void Main(string[] args) { Config = LoadConfiguration(); Console.WriteLine("[INFO/] Server Information Manager: Server Monitor Daemon"); Console.WriteLine("[INFO/] Currently monitoring with: PIPE/simext."); // MEMORY/data-log, FILE/net-rpt Console.WriteLine("[INFO/] Alpha v0.0 - Created by Verox for UnitedOperations.net"); Console.WriteLine(); while (true) { if (pipeServer != null) { Console.WriteLine(" disposing disconnected pipe."); pipeServer.Dispose(); } Console.Write("[INFO/PIPE/simext] Initializing new pipe..."); try { pipeServer = new NamedPipeServerStream(UniquePipeName, PipeDirection.InOut); Console.WriteLine(" success."); } catch (Exception ex) // We're rethrowing this. { // Let the user know something went wrong. Console.WriteLine("\n[FATAL/PIPE/simext] Unable to initalize new pipe. This is unrecoverable."); Console.WriteLine("[FATAL/PIPE/simext] " + ex.Message); Console.WriteLine("[INFO] This program is about to crash."); var throwaway = Console.ReadLine(); // Aaand rethrow. This kills the program. throw; } #if DEBUG Console.WriteLine("[DEBUG/PIPE/simext] Initialzing new stream!"); // Debug output. #endif StreamReader sr = new StreamReader(pipeServer); // blocking-ly wait for a new connection to appear on the pipe. Console.WriteLine("[INFO/PIPE/simext] Waiting for connection on pipe."); pipeServer.WaitForConnection(); Console.WriteLine("[INFO/PIPE/simext] Connection established on pipe!"); #if !DEBUG // Hide the window, we don't need it show it anymore unless something breaks. ShowWindow(false); #endif // While we have a connection while (pipeServer.IsConnected) { #if DEBUG Console.WriteLine("[DEBUG/PIPE/simext] TICK"); // Debug output. #endif string output = sr.ReadLine(); // Take data from the pipe... // And if there's actually something to send, send it to the server. if (output != null) { SendHTTP(output); } //#if DEBUG //Console.WriteLine("[DEBUG/PIPE/simext] " + output); // Debug output. //#endif } #if !DEBUG // Pipe disconnected, show the window. ShowWindow(true); #endif Console.Write("[WARN/PIPE/simext] Pipe disconnected... "); // ACH, AIT ALL GON T**S UP, ERRR! } }