示例#1
0
        /// <summary>
        /// 关闭浏览器
        /// </summary>
        public void Shutdown()
        {
            SendShutdownEvent();

            Initialized = false;
            _connected  = false;

            _mainTexArray  = null;
            _inCommServer  = null;
            _outCommServer = null;
            _pluginProcess = null;
            sPixelLock     = null;
        }
示例#2
0
        public IEnumerator InitPlugin(int width, int height, string sharedfilename, string initialURL, bool enableWebRTC, bool enableGPU)
        {
            ///浏览器exe位置,目前没分32/64位
#if UNITY_EDITOR_64
            string PluginServerPath = Application.dataPath + @"\WebBrowser\PluginServer\x64";
#else
#if UNITY_EDITOR_32
            string indexStr         = "Game";
            string PluginServerPath = string.Empty;
            if (Application.isEditor)
            {
                PluginServerPath = Application.dataPath.Remove(Application.dataPath.LastIndexOf(indexStr)) + @"Bin/PluginServer";
            }
            else
            {
                PluginServerPath = Application.streamingAssetsPath;
                PluginServerPath = PluginServerPath.Replace("/StreamingAssets", string.Empty);
                string[] temp = PluginServerPath.Split('/');
                PluginServerPath  = PluginServerPath.Replace(temp[temp.Length - 1], string.Empty);
                PluginServerPath  = PluginServerPath.Remove(PluginServerPath.Length - 1, 1);
                PluginServerPath += "/PluginServer";
            }
#else
            //HACK
            string AssemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            //log this for error handling
            Debug.Log("Assembly path:" + AssemblyPath);

            AssemblyPath = Path.GetDirectoryName(AssemblyPath);         //Managed

            AssemblyPath = Directory.GetParent(AssemblyPath).FullName;  //<project>_Data
            AssemblyPath = Directory.GetParent(AssemblyPath).FullName;  //required

            string PluginServerPath = AssemblyPath + @"\PluginServer";
#endif
#endif

            Debug.Log("Starting server from:" + PluginServerPath);

            kWidth  = width;
            kHeight = height;



            _sharedFileName = sharedfilename;

            //randoms
            Guid inID = Guid.NewGuid();
            _outCommFile = inID.ToString();

            Guid outID = Guid.NewGuid();
            _inCommFile = outID.ToString();

            _initialURL   = initialURL;
            _enableWebRTC = enableWebRTC;
            _enableGPU    = enableGPU;

            if (BrowserTexture == null)
            {
                BrowserTexture = new Texture2D(kWidth, kHeight, TextureFormat.BGRA32, false, true);
            }

            sPixelLock = new object();


            string args = BuildParamsString();

            _connected = false;

            _inCommServer  = new SharedCommServer(false);
            _outCommServer = new SharedCommServer(true);


            //尝试启动浏览器次数,超过则关闭
            int tryOpenProcessCount = 10;
            int curOpenProcessCount = 0;
            while (!_connected)
            {
                try
                {
                    if (_pluginProcess != null)
                    {
                        _pluginProcess.CloseMainWindow();
                    }

                    _pluginProcess = new Process()
                    {
                        StartInfo = new ProcessStartInfo()
                        {
                            WorkingDirectory = PluginServerPath,
                            FileName         = PluginServerPath + @"\SharedPluginServer.exe",
                            Arguments        = args
                        }
                    };

                    _pluginProcess.Start();
                    Initialized = false;
                    curOpenProcessCount++;
                }
                catch (Exception e)
                {
                    //log the file
                    Debug.Log("FAILED TO START SERVER FROM:" + PluginServerPath + @"\SharedPluginServer.exe" + ",errorCode:" + e.Message);
                    //throw;
                }
                yield return(new WaitForSeconds(2.0f));

                //connected = ConnectTcp(out _clientSocket);

                _inCommServer.Connect(_inCommFile);
                bool b1 = _inCommServer.GetIsOpen();
                _outCommServer.Connect(_outCommFile);
                bool b2 = _outCommServer.GetIsOpen();
                _connected = b1 && b2;

                if (!_connected && curOpenProcessCount > tryOpenProcessCount)
                {
                    if (_pluginProcess != null)
                    {
                        _pluginProcess.CloseMainWindow();
                    }

                    Debug.LogWarning("启动Web服务器失败。");
                    break;
                }
            }
        }