// Update is called once per frame void Update() { if (!_inServer.GetIsOpen()) { _inServer.Connect(_inSharedCommFile); } else { if (!isShowConnectOnce) { _outMessage.text = "连接成功!"; isShowConnectOnce = true; _image.gameObject.SetActive(true); } EventPacket ep = _inServer.GetMessage(); if (ep != null) { _outMessage.text = ep.eventValue; Debug.Log(ep.eventId + "," + ep.eventValue); } } if (!_outServer.GetIsOpen()) { _outServer.Connect(_outSharedCommFile); } }
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; }
//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; } }