private void ListenForRequests() { if (appWrapperProcess == null) { return; } SimpleThreadMessenger.HandleMessageFromQtAppWrapperDelegate handleMessageFromQtAppWrapperDelegate; handleMessageFromQtAppWrapperDelegate = new SimpleThreadMessenger.HandleMessageFromQtAppWrapperDelegate(simpleThreadMessenger.HandleMessageFromQtAppWrapper); bool firstIteration = true; while (!terminateEditorThread) { try { if (!firstIteration) { if (appWrapperProcess.HasExited) { appWrapperProcess.Close(); } } else { firstIteration = false; } } catch { } appWrapperProcess.Start(); client = new TcpClient(); int connectionAttempts = 0; int appwrapperPort = 12015; while (!client.Connected && !terminateEditorThread && connectionAttempts < 10) { try { client.Connect(IPAddress.Loopback, appwrapperPort); if (!client.Connected) { ++connectionAttempts; System.Threading.Thread.Sleep(1000); } } catch { ++connectionAttempts; System.Threading.Thread.Sleep(1000); } } if (connectionAttempts >= 10) { Messages.DisplayErrorMessage(SR.GetString("CouldNotConnectToAppwrapper", appwrapperPort)); terminateEditorThread = true; } if (terminateEditorThread) { TerminateClient(); return; } NetworkStream clientStream = client.GetStream(); // say hello to qtappwrapper clientStream.Write(qtAppWrapperHelloMessage, 0, qtAppWrapperHelloMessage.Length); clientStream.Flush(); byte[] message = new byte[4096]; int bytesRead; while (!terminateEditorThread) { try { bytesRead = 0; try { //blocks until a client sends a message bytesRead = clientStream.Read(message, 0, 4096); } catch { // A socket error has occured, probably because // the QtAppWrapper has been terminated. // Break and then try to restart the QtAppWrapper. break; } if (bytesRead == 0) { //the client has disconnected from the server break; } //message has successfully been received UnicodeEncoding encoder = new UnicodeEncoding(); string fullMessageString = encoder.GetString(message, 0, bytesRead); string[] messages = fullMessageString.Split(new Char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (string messageString in messages) { int index = messageString.IndexOf(' '); int requestedPid = Convert.ToInt32(messageString.Substring(0, index)); int currentPid = System.Diagnostics.Process.GetCurrentProcess().Id; if (requestedPid == currentPid) { // Actual file opening is done in the main thread. string file = messageString.Substring(index + 1); simpleThreadMessenger.Invoke(handleMessageFromQtAppWrapperDelegate, new object[] { file }); } } } catch (System.Threading.ThreadAbortException) { break; } catch { } } TerminateClient(); } }
private void ListenForRequests() { if (appWrapperProcess == null) return; SimpleThreadMessenger.HandleMessageFromQtAppWrapperDelegate handleMessageFromQtAppWrapperDelegate; handleMessageFromQtAppWrapperDelegate = new SimpleThreadMessenger.HandleMessageFromQtAppWrapperDelegate(simpleThreadMessenger.HandleMessageFromQtAppWrapper); bool firstIteration = true; while (!terminateEditorThread) { try { if (!firstIteration) { if (appWrapperProcess.HasExited) appWrapperProcess.Close(); } else { firstIteration = false; } } catch { } appWrapperProcess.Start(); client = new TcpClient(); int connectionAttempts = 0; int appwrapperPort = 12005; while (!client.Connected && !terminateEditorThread && connectionAttempts < 10) { try { client.Connect(IPAddress.Loopback, appwrapperPort); if (!client.Connected) { ++connectionAttempts; System.Threading.Thread.Sleep(1000); } } catch { ++connectionAttempts; System.Threading.Thread.Sleep(1000); } } if (connectionAttempts >= 10) { Messages.DisplayErrorMessage(SR.GetString("CouldNotConnectToAppwrapper", appwrapperPort)); terminateEditorThread = true; } if (terminateEditorThread) { TerminateClient(); return; } NetworkStream clientStream = client.GetStream(); // say hello to qtappwrapper clientStream.Write(qtAppWrapperHelloMessage, 0, qtAppWrapperHelloMessage.Length); clientStream.Flush(); byte[] message = new byte[4096]; int bytesRead; while (!terminateEditorThread) { try { bytesRead = 0; try { //blocks until a client sends a message bytesRead = clientStream.Read(message, 0, 4096); } catch { // A socket error has occured, probably because // the QtAppWrapper has been terminated. // Break and then try to restart the QtAppWrapper. break; } if (bytesRead == 0) { //the client has disconnected from the server break; } //message has successfully been received UnicodeEncoding encoder = new UnicodeEncoding(); string fullMessageString = encoder.GetString(message, 0, bytesRead); string[] messages = fullMessageString.Split(new Char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (string messageString in messages) { int index = messageString.IndexOf(' '); int requestedPid = Convert.ToInt32(messageString.Substring(0, index)); int currentPid = System.Diagnostics.Process.GetCurrentProcess().Id; if (requestedPid == currentPid) { // Actual file opening is done in the main thread. string file = messageString.Substring(index + 1); simpleThreadMessenger.Invoke(handleMessageFromQtAppWrapperDelegate, new object[] { file }); } } } catch (System.Threading.ThreadAbortException) { break; } catch { } } TerminateClient(); } }