Ejemplo n.º 1
0
        private void btnConnect_Click(object sender, EventArgs e)
        {
            // Grey out form
            this.Enabled = false;
            btnCon.Text  = "Connecting...";
            Application.DoEvents();

            // Try and handle connect attempt
            try
            {
                _controller.Connect();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connection failure:\r\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            // Restore
            this.Enabled = true;
            btnCon.Text  = "Connect";
        }
        protected bool StartAndConnectServer()
        {
            // First connection attempt
            try
            {
                cef.Connect();
                return(true);
            }
            catch (Exception e)
            {
                UnityEngine.Debug.Log("[CEF] Proxy server not responding, attempting to start server executable. Connection error details: " + e.Message);
            }

            // Determine path to CEF Unity Server
            //string cefPath = Application.dataPath;
            string cefPath     = System.IO.Path.Combine(Application.streamingAssetsPath, "Cef");
            string cefPathExec = cefPath + "/CefUnityServer.exe";

            // Start the process, hide it, and listen to its output
            var processInfo = new System.Diagnostics.ProcessStartInfo();

            processInfo.Arguments              = string.Format("{0} {1} {2}", cef.PipeName, "google.com", width + "x" + height);
            processInfo.CreateNoWindow         = true;
            processInfo.FileName               = cefPathExec;
            processInfo.WorkingDirectory       = cefPath;
            processInfo.UseShellExecute        = false;
            processInfo.RedirectStandardInput  = true;
            processInfo.RedirectStandardOutput = true;
            processInfo.WindowStyle            = System.Diagnostics.ProcessWindowStyle.Hidden;

            process = System.Diagnostics.Process.Start(processInfo);
            process.ErrorDataReceived  += Process_ErrorDataReceived;
            process.OutputDataReceived += Process_OutputDataReceived;

            // Basic wait time to let the server start (usually takes a quarter second or so on a reasonable machine)
            Thread.Sleep(250);

            // Wait for the app to start - as long as it doesn't fail and we don't exceed a certain timeout
            int       attemptsRemaining = 10;
            Exception lastEx            = null;

            do
            {
                try
                {
                    // Connect - if okay, break out and proceed
                    cef.Connect();
                    return(true);
                }
                catch (Exception ex)
                {
                    // Connect failed, wait a bit and try again
                    UnityEngine.Debug.Log("[CEF] Proxy server not responding. {0} attempt(s) remaining. Connection error details: " + ex.Message);

                    attemptsRemaining--;
                    lastEx = ex;

                    if (attemptsRemaining <= 0)
                    {
                        break;
                    }
                    else
                    {
                        Thread.Sleep(100);
                    }
                }
            }while (true);

            UnityEngine.Debug.Log("[CEF] Proxy server failed to start! (Hard failure)");
            throw lastEx;
        }