private void WrapCall<T>(Func<Task<T>> call, Action<T, Stream> serializer, int reqID) { try { Task<T> task = call(); var tnext = task.ContinueOnMainThread(t => { if (t.IsFaulted) { Console.Error.WriteLine(MakeStr(t.Exception)); Console.Error.Flush(); connector.SendResponseError(reqID, t.Exception.InnerMost()); } else { T res = t.Result; connector.SendResponseSuccess(reqID, stream => serializer(res, stream)); } }); } catch (Exception exp) { Console.Error.WriteLine(MakeStr(exp)); Console.Error.Flush(); connector.SendResponseError(reqID, exp.InnerMost()); } }
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); } }