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