private void onEvent(Event evt) { switch (evt.Code) { case ExternalModuleHost.ModuleHelper.ID_Event_VariableValuesChanged: var values = BinSeri.VariableValue_Serializer.Deserialize(evt.Payload); notifier?.Notify_VariableValuesChanged(values); break; case ExternalModuleHost.ModuleHelper.ID_Event_ConfigChanged: var objects = StdJson.ObjectFromUtf8Stream <List <ObjectRef> >(evt.Payload); if (objects != null) { notifier?.Notify_ConfigChanged(objects); } break; case ExternalModuleHost.ModuleHelper.ID_Event_AlarmOrEvent: var ae = StdJson.ObjectFromUtf8Stream <AlarmOrEventInfo>(evt.Payload); if (ae != null) { notifier?.Notify_AlarmOrEvent(ae); } break; default: logger.Error("Unknown event code: " + evt.Code); break; } }
private async Task <RequestBase> GetRequestObject(HttpRequest request, ReqDef def) { const string mediaBinary = "application/octet-stream"; using (var memoryStream = MemoryManager.GetMemoryStream("HandleClientRequest")) { using (var body = request.Body) { await body.CopyToAsync(memoryStream).ConfigureAwait(false); } memoryStream.Seek(0, SeekOrigin.Begin); RequestBase ret; bool binaryRequest = request.ContentType == mediaBinary; if (binaryRequest) { ret = def.MakeRequestObject(); using (var reader = new BinaryReader(memoryStream, Encoding.UTF8, leaveOpen: false)) { BinSerializable x = (BinSerializable)ret; x.BinDeserialize(reader); } } else { ret = (RequestBase)(StdJson.ObjectFromUtf8Stream(memoryStream, def.ReqType) ?? throw new Exception("Request via JSON is null")); } var accept = request.Headers["Accept"]; bool binaryResponse = accept.Any(a => a.Contains(mediaBinary)); ret.ReturnBinaryResponse = binaryResponse; return(ret); } }
private async Task <T> SendRequest <T>(ModuleMsg requestMsg) { using (Response res = await connection.SendRequest(requestMsg.GetMessageCode(), stream => StdJson.ObjectToStream(requestMsg, stream))) { if (res.Success) { return(StdJson.ObjectFromUtf8Stream <T>(res.SuccessPayload)); } else { throw new Exception(res.ErrorMsg); } } }
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); } }
private void onEvent(Event evt) { switch (evt.Code) { case ExternalModuleHost.ModuleHelper.ID_Event_VariableValuesChanged: var values = StdJson.ObjectFromUtf8Stream <VariableValue[]>(evt.Payload); notifier.Notify_VariableValuesChanged(values); break; case ExternalModuleHost.ModuleHelper.ID_Event_ConfigChanged: var objects = StdJson.ObjectFromUtf8Stream <ObjectRef[]>(evt.Payload); notifier.Notify_ConfigChanged(objects); break; case ExternalModuleHost.ModuleHelper.ID_Event_AlarmOrEvent: var ae = StdJson.ObjectFromUtf8Stream <AlarmOrEventInfo>(evt.Payload); notifier.Notify_AlarmOrEvent(ae); break; default: logger.Error("Unknown event code: " + evt.Code); break; } }
private static T Deserialize <T>(MemoryStream stream) => StdJson.ObjectFromUtf8Stream <T>(stream);
private static T Deserialize<T>(MemoryStream stream) => StdJson.ObjectFromUtf8Stream<T>(stream) ?? throw new Exception("Unexpected null value");