예제 #1
0
        private static async Task Loop(TcpConnectorSlave connector, AdapterBase module)
        {
            Process parentProcess = null;

            using (Request request = await connector.ReceiveRequest()) {
                if (request.Code != AdapterMsg.ID_ParentInfo)
                {
                    throw new Exception("Missing ParentInfo request");
                }
                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 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);
            }
        }
예제 #2
0
        public override async Task <Group[]> Initialize(Adapter adapter, AdapterCallback callback, DataItemInfo[] itemInfos)
        {
            this.callback    = callback;
            this.adapterName = adapter.Name;

            var config = new Config(adapter.Config);

            string cmd  = GetCommand(config);
            string args = GetArgs(config);

            const string portPlaceHolder = "{PORT}";

            if (!args.Contains(portPlaceHolder))
            {
                throw new Exception("Missing port placeholder in args parameter: {PORT}");
            }

            var server = TcpConnectorServer.ListenOnFreePort();
            int port   = server.Port;

            args = args.Replace(portPlaceHolder, port.ToString());

            try {
                var taskConnect = server.WaitForConnect(TimeSpan.FromSeconds(60));

                process = StartProcess(cmd, args);

                while (!process.HasExited && !taskConnect.IsCompleted)
                {
                    await Task.Delay(TimeSpan.FromMilliseconds(50));
                }

                if (process.HasExited)
                {
                    throw new Exception($"Failed to start command \"{cmd}\" with arguments \"{args}\"");
                }

                connection = await taskConnect;

                var parentInfo = new ParentInfoMsg()
                {
                    PID = Process.GetCurrentProcess().Id
                };
                Task ignored = SendVoidRequest(parentInfo);

                var initMsg = new InititializeMsg(adapter, itemInfos);

                Task <Group[]> tInit = SendRequest <Group[]>(initMsg);

                taskReceive = connection.ReceiveAndDistribute(onEvent);

                Task t = await Task.WhenAny(tInit, taskReceive);

                if (t != tInit)
                {
                    if (process.HasExited)
                    {
                        throw new Exception("Adapter process terminated during Init call.");
                    }
                    else
                    {
                        throw new Exception("TCP connection broken to Adapter process during Init call.");
                    }
                }

                Group[] res = await tInit;

                Task ignored2 = Supervise();

                return(res);
            }
            catch (Exception) {
                if (connection != null)
                {
                    connection.Close("Init failed.");
                }
                StopProcess(process);
                process = null;
                throw;
            }
            finally {
                server.StopListening();
            }
        }