Ejemplo n.º 1
0
        public static void ConnectAndRunAdapter(string host, int port, AdapterBase adapter)
        {
            var connector = new TcpConnectorSlave();

            connector.Connect(host, port);

            try {
                SingleThreadedAsync.Run(() => Loop(connector, adapter));
            }
            catch (Exception exp) {
                Console.Error.WriteLine("EXCEPTION: " + exp.Message);
            }
        }
Ejemplo n.º 2
0
        private static async Task Loop(TcpConnectorSlave connector, AdapterBase 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);
            }
        }
Ejemplo n.º 3
0
 public AdapterHelper(AdapterBase module, TcpConnectorSlave connector)
 {
     this.adapter   = module;
     this.connector = connector;
 }