Beispiel #1
0
        public static void foo()
        {
            Process loopProcess = null;

            try
            {
                loopProcess = new Process
                {
                    StartInfo =
                    {
                        FileName               = "ChromeNativeMessagingHost.exe",
                        UseShellExecute        = false,
                        RedirectStandardInput  = true,
                        RedirectStandardOutput = true
                    }
                };
                loopProcess.Start();

                using (var inStream = loopProcess.StandardInput)
                    using (var outStream = loopProcess.StandardOutput)
                    {
                        var testGetJsonString = GenerateXmlHttpRequestJsonObjectForNonExistentDatabase();
                        var xmlHttpRequest    = JsonConvert.DeserializeObject <XmlHttpRequest>(testGetJsonString);
                        ChromeNativeHostUtilities.SendMessage(xmlHttpRequest, inStream.BaseStream);
                        inStream.BaseStream.Flush();

                        var stopWatch = new Stopwatch();
                        stopWatch.Start();

                        var xmlHttpResponse =
                            ChromeNativeHostUtilities.ReadNextMessage <XmlHttpResponse>(outStream.BaseStream);
                        stopWatch.Stop();
                        Debug.WriteLine("Time for message on wire to thali device hub: " + stopWatch.ElapsedMilliseconds);
                        Debug.Flush();
                        //Assert.AreEqual(404, xmlHttpResponse.status);



                        ChromeNativeHostUtilities.SendMessage(xmlHttpRequest, inStream.BaseStream);
                        inStream.BaseStream.Flush();

                        stopWatch.Restart();

                        ChromeNativeHostUtilities.ReadNextMessage <XmlHttpResponse>(outStream.BaseStream);
                        stopWatch.Stop();
                        Debug.WriteLine("Time for message on wire to thali device hub: " + stopWatch.ElapsedMilliseconds);
                        Debug.Flush();

                        ChromeNativeHostUtilities.SendMessage(xmlHttpRequest, inStream.BaseStream);
                        inStream.BaseStream.Flush();

                        stopWatch.Restart();

                        ChromeNativeHostUtilities.ReadNextMessage <XmlHttpResponse>(outStream.BaseStream);
                        stopWatch.Stop();
                        Debug.WriteLine("Time for message on wire to thali device hub: " + stopWatch.ElapsedMilliseconds);
                        Debug.Flush();
                        ChromeNativeHostUtilities.SendMessage(xmlHttpRequest, inStream.BaseStream);
                        inStream.BaseStream.Flush();

                        stopWatch.Restart();

                        ChromeNativeHostUtilities.ReadNextMessage <XmlHttpResponse>(outStream.BaseStream);
                        stopWatch.Stop();
                        Debug.WriteLine("Time for message on wire to thali device hub: " + stopWatch.ElapsedMilliseconds);
                        Debug.Flush();
                        ChromeNativeHostUtilities.SendMessage(xmlHttpRequest, inStream.BaseStream);
                        inStream.BaseStream.Flush();

                        stopWatch.Restart();

                        ChromeNativeHostUtilities.ReadNextMessage <XmlHttpResponse>(outStream.BaseStream);
                        stopWatch.Stop();
                        Debug.WriteLine("Time for message on wire to thali device hub: " + stopWatch.ElapsedMilliseconds);
                        Debug.Flush();
                        ChromeNativeHostUtilities.SendMessage(xmlHttpRequest, inStream.BaseStream);
                        inStream.BaseStream.Flush();

                        stopWatch.Restart();

                        ChromeNativeHostUtilities.ReadNextMessage <XmlHttpResponse>(outStream.BaseStream);
                        stopWatch.Stop();
                        Debug.WriteLine("Time for message on wire to thali device hub: " + stopWatch.ElapsedMilliseconds);
                        Debug.Flush();
                    }
            }
            finally
            {
                if (loopProcess != null)
                {
                    loopProcess.Kill();
                }
            }
        }
Beispiel #2
0
        public static void MainLoop(bool synchronous)
        {
            Stream outStream = null;

            // TODO: If one of the console methods throws an exception then will it be caught by the outer catch? And if
            // there is an exception inside the inner try then don't I have to assume that the using will close the streams
            // before the outter catch can send a message? I'm not sure about the answers and don't have time to explore
            // at the moment so I'm using a double wrapping of try's to take care of it.
            try
            {
                using (var inStream = Console.OpenStandardInput())
                using (outStream = Console.OpenStandardOutput())
                {
                    try
                    {
                        ServicePointManager.DefaultConnectionLimit = 100;

                        var appdataDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                        var homeDirectory = string.Concat(appdataDirectory, @"\Google\Chrome\User Data\Thali");
                        if (!Directory.Exists(homeDirectory))
                        {
                            Directory.CreateDirectory(homeDirectory);
                        }

                        DirectoryInfo workingDirectory = new DirectoryInfo(homeDirectory);
                        
                        var clientCert = ThaliClientToDeviceHubUtilities.GetLocalClientCertificate(workingDirectory);
                        if (synchronous)
                        {
                            ChromeNativeHostUtilities.SynchronousRequestResponseMessageEngine<XmlHttpRequest>(
                                inStream,
                                outStream,
                                inMessage => ProcessMessage(inMessage, clientCert),
                                ProcessHostError);
                        }
                        else
                        {
                            ChromeNativeHostUtilities.AsynchronousRequestResponseMessageEngine<XmlHttpRequest>(
                                inStream,
                                outStream,
                                inMessage => ProcessMessage(inMessage, clientCert),
                                ProcessHostError);
                        }
                    }
                    catch (Exception e)
                    {
                        if (outStream != null)
                        {
                            ChromeNativeHostUtilities.ParallelSendMessage(ProcessHostError("oops! " + e.Message, null), outStream);
                        }                        
                    }
                }
            }
            catch (Exception e)
            {
                if (outStream != null)
                {
                    ChromeNativeHostUtilities.SendMessage(ProcessHostError("oops! " + e.Message, null), outStream);   
                }
            }
        }