private void InitComm() { //Read _inServer = new SharedCommServer(false); //Write _outServer = new SharedCommServer(true); _inServer.InitComm(inCommFileSize, _inSharedCommFile); _outServer.InitComm(outCommFileSize, _outSharedCommFile); //开启读取线程 Action serverRead = () => { EventPacket ep; while (true) { ep = _inServer.GetMessage(); if (ep != null) { //把获取到的信息直接发送到浏览器中 Console.WriteLine("Message From Unity:" + ep.eventValue); _browser.ExecuteScriptAsync("DisplayValue", ep.eventValue); } //Console.WriteLine("serverRead!!"); //没那么频繁、可以调节 Thread.Sleep(200); } }; // ThreadPool.QueueUserWorkItem((o) => { serverWrite(); }); ThreadPool.QueueUserWorkItem((o) => { serverRead(); }); }
/// <summary> /// App constructor /// </summary> /// <param name="worker">Main CEF worker</param> /// <param name="memServer">Shared memory file</param> /// <param name="commServer">TCP server</param> // public App(CefWorker worker, SharedMemServer memServer, SocketServer commServer,bool enableWebRtc) public App(CefWorker worker, SharedMemServer memServer, SharedCommServer inServer, SharedCommServer outServer, bool enableWebRtc) { // _renderProcessHandler = new WorkerCefRenderProcessHandler(); _enableWebRtc = enableWebRtc; _memServer = memServer; _mainWorker = worker; //init SharedMem comms _inCommServer = inServer; _outCommServer = outServer; _mainWorker.SetMemServer(_memServer); //attach dialogs and queries _mainWorker.OnJSDialog += _mainWorker_OnJSDialog; _mainWorker.OnBrowserJSQuery += _mainWorker_OnBrowserJSQuery; //attach page events _mainWorker.OnPageLoaded += _mainWorker_OnPageLoaded; IsRunning = true; _exitTimer = new Timer(); _exitTimer.Interval = 10000; _exitTimer.Tick += _exitTimer_Tick; _exitTimer.Start(); }
void Start() { _inServer = new SharedCommServer(false); _outServer = new SharedCommServer(true); _button.onClick.AddListener(() => { if (_outServer != null && _outServer.GetIsOpen()) { _outServer.WriteMessage(new EventPacket() { eventId = 1, eventValue = "UnityMessage" }); } }); }
public void Init() { //string args = "--enable-media-stream"; string args = ""; #if USE_ARGS args = args + pictureBox1.Width.ToString() + " " + pictureBox1.Height.ToString() + " "; args = args + "http://test.webrtc.org" + " "; Guid memid = Guid.NewGuid(); memfile = memid.ToString(); args = args + memfile + " "; Guid inID = Guid.NewGuid(); outMemFile = inID.ToString(); args = args + outMemFile + " "; Guid outID = Guid.NewGuid(); inMemFile = outID.ToString(); args = args + inMemFile + " "; args = args + "1" + " "; //webrtc args = args + "1"; //gpu #endif bool connected = false; _inCommServer = new SharedCommServer(false); _outCommServer = new SharedCommServer(true); while (!connected) { Process pluginProcess = new Process() { StartInfo = new ProcessStartInfo() { WorkingDirectory = // @"D:\work\unity\StandaloneConnector_1\SimpleUnityBrowser\SharedPluginServer\bin\x86\Debug", @"D:\work\unity\StandaloneConnector_1\SimpleUnityBrowser\SharedPluginServer\bin\x64\Debug", FileName = // @"D:\work\unity\StandaloneConnector_1\SimpleUnityBrowser\SharedPluginServer\bin\x86\Debug\SharedPluginServer.exe", @"D:\work\unity\StandaloneConnector_1\SimpleUnityBrowser\SharedPluginServer\bin\x64\Debug\SharedPluginServer.exe", Arguments = args } }; pluginProcess.Start(); //Thread.Sleep(10000); // while (!Process.GetProcesses().Any(p => p.Name == myName)) { Thread.Sleep(100); } bool found_proc = false; while (!found_proc) { foreach (Process clsProcess in Process.GetProcesses()) { if (clsProcess.ProcessName == pluginProcess.ProcessName) { found_proc = true; } } Thread.Sleep(1000); } _inCommServer.Connect(inMemFile); bool b1 = _inCommServer.GetIsOpen(); _outCommServer.Connect(outMemFile); bool b2 = _outCommServer.GetIsOpen(); connected = b1 && b2; } arr = new SharedArray <byte>(memfile); //clientSocket.Connect(new IPEndPoint(ip, port)); #if USE_ARGS _texture = new Bitmap(pictureBox1.Width, pictureBox1.Width); #else int defWidth = 1280; int defHeight = 720; _texture = new Bitmap(defWidth, defHeight); #endif Application.Idle += Application_Idle; }
static int Main(string[] args) { log.Info("===============START================"); //////// CEF RUNTIME try { CefRuntime.Load(); } catch (DllNotFoundException ex) { log.ErrorFormat("{0} error", ex.Message); } catch (CefRuntimeException ex) { log.ErrorFormat("{0} error", ex.Message); } catch (Exception ex) { log.ErrorFormat("{0} error", ex.Message); } int defWidth = 1280; int defHeight = 720; string defUrl = "http://test.webrtc.org"; string defFileName = "MainSharedMem"; string defInFileName = "InSharedMem"; string defOutFileName = "OutSharedMem"; bool useWebRTC = false; bool EnableGPU = false; if (args.Length > 0 && args[0] != "--type=renderer") { if (args.Length > 1) { defWidth = Int32.Parse(args[0]); defHeight = Int32.Parse(args[1]); } if (args.Length > 2) { defUrl = args[2]; } if (args.Length > 3) { defFileName = args[3]; } if (args.Length > 4) { defInFileName = args[4]; } if (args.Length > 5) { defOutFileName = args[5]; } if (args.Length > 6) { if (args[6] == "1") { useWebRTC = true; } } if (args.Length > 7) { if (args[7] == "1") { EnableGPU = true; } } } log.InfoFormat("Starting plugin, settings:width:{0},height:{1},url:{2},memfile:{3},inMem:{4},outMem:{5}, WebRtc:{6},Enable GPU:{7}", defWidth, defHeight, defUrl, defFileName, defInFileName, defOutFileName, useWebRTC, EnableGPU); try { CefMainArgs cefMainArgs; cefMainArgs = new CefMainArgs(args); var cefApp = new WorkerCefApp(useWebRTC, EnableGPU); int exit_code = CefRuntime.ExecuteProcess(cefMainArgs, cefApp, IntPtr.Zero); if (exit_code >= 0) { log.ErrorFormat("CefRuntime return " + exit_code); return(exit_code); } var cefSettings = new CefSettings { SingleProcess = false, MultiThreadedMessageLoop = true, WindowlessRenderingEnabled = true, LogSeverity = CefLogSeverity.Info, }; try { CefRuntime.Initialize(cefMainArgs, cefSettings, cefApp, IntPtr.Zero); } catch (CefRuntimeException ex) { log.ErrorFormat("{0} error", ex.Message); } ///////////// } catch (Exception ex) { log.Info("EXCEPTION ON CEF INITIALIZATION:" + ex.Message + "\n" + ex.StackTrace); throw; } CefWorker worker = new CefWorker(); worker.Init(defWidth, defHeight, defUrl); SharedMemServer server = new SharedMemServer(); server.Init(defWidth * defHeight * 4, defFileName); SharedCommServer inSrv = new SharedCommServer(false); //TODO: the sizes may vary, but 10k should be enough? inSrv.InitComm(10000, defInFileName); SharedCommServer outSrv = new SharedCommServer(true); outSrv.InitComm(10000, defOutFileName); var app = new App(worker, server, inSrv, outSrv, false); while (app.IsRunning) { Application.DoEvents(); //check incoming messages and push outcoming app.CheckMessage(); } CefRuntime.Shutdown(); return(0); }
//A really hackish way to avoid thread error. Should be better way /* public bool ConnectTcp(out TcpClient tcp) * { * TcpClient ret = null; * try * { * ret = new TcpClient("127.0.0.1", _port); * } * catch (Exception ex) * { * tcp = null; * return false; * } * * tcp = ret; * return true; * * }*/ public IEnumerator InitPlugin(int width, int height, string sharedfilename, string initialURL, bool enableWebRTC, bool enableGPU) { //Initialization (for now) requires a predefined path to PluginServer, //so change this section if you move the folder //Also change the path in deployment script. #if UNITY_EDITOR_64 string PluginServerPath = Application.dataPath + @"\SimpleWebBrowser\PluginServer\x64"; #else #if UNITY_EDITOR_32 string PluginServerPath = Application.dataPath + @"\SimpleWebBrowser\PluginServer\x86"; #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); while (!_connected) { try { _pluginProcess = new Process() { StartInfo = new ProcessStartInfo() { WorkingDirectory = PluginServerPath, FileName = PluginServerPath + @"\SharedPluginServer.exe", Arguments = args } }; _pluginProcess.Start(); Initialized = false; } catch (Exception ex) { //log the file Debug.Log("FAILED TO START SERVER FROM:" + PluginServerPath + @"\SharedPluginServer.exe"); throw; } yield return(new WaitForSeconds(1.0f)); //connected = ConnectTcp(out _clientSocket); _inCommServer.Connect(_inCommFile); bool b1 = _inCommServer.GetIsOpen(); _outCommServer.Connect(_outCommFile); bool b2 = _outCommServer.GetIsOpen(); _connected = b1 && b2; } }