public static void ConnectAndRunModule(string host, int port, ModuleBase module) { var connector = new TcpConnectorSlave(); connector.Connect(host, port); try { SingleThreadedAsync.Run(() => Loop(connector, module)); } catch (Exception exp) { Console.Error.WriteLine("EXCEPTION: " + exp.Message); } }
private static async Task Loop(TcpConnectorSlave connector, ModuleBase module) { Process parentProcess = null; using (Request request = await connector.ReceiveRequest()) { if (request.Code != ModuleHelper.ID_ParentInfo) { throw new Exception("Missing ParentInfo request (update MediatorLib.dll)"); } ParentInfoMsg info = StdJson.ObjectFromUtf8Stream <ParentInfoMsg>(request.Payload); parentProcess = Process.GetProcessById(info.PID); connector.SendResponseSuccess(request.RequestID, s => { }); } Thread t = new Thread(() => { ParentAliveChecker(parentProcess); }); t.IsBackground = true; t.Start(); var helper = new ModuleHelper(module, connector); bool run = true; while (run) { using (Request request = await connector.ReceiveRequest()) { helper.ExecuteModuleRequestAsync(request); bool shutdown = request.Code == ModuleHelper.ID_Shutdown; bool initAbort = request.Code == ModuleHelper.ID_InitAbort; run = !shutdown && !initAbort; } } // Wait until parent process kills us (after Run method completed) while (true) { await Task.Delay(1000); } }
public ModuleHelper(ModuleBase module, TcpConnectorSlave connector) { this.module = module; this.connector = connector; }