static void Main(string[] args) { Console.WriteLine("Waiting for connection on named pipe mypipe"); while (true) { System.IO.Pipes.NamedPipeServerStream namedPipeServerStream = new System.IO.Pipes.NamedPipeServerStream("mypipe"); namedPipeServerStream.WaitForConnection(); byte[] buffer = new byte[255]; namedPipeServerStream.Read(buffer, 0, 255); string request = ASCIIEncoding.ASCII.GetString(buffer); Console.WriteLine(request); request = request.Trim('\0'); if (request.ToLower() == "close") break; namedPipeServerStream.Close(); } }
public static string NamedPipeServerClose(object pipeRaw) { object[] pipe = (object[])pipeRaw; System.IO.Pipes.NamedPipeServerStream namedPipe = (System.IO.Pipes.NamedPipeServerStream)pipe[0]; namedPipe.Close(); return(null); }
static async Task StartServerActivatorAsync(string npname) { await Task.Delay(1); while (!ApplicationCTS.IsCancellationRequested) { bool everConnected = false; try { using (var nps = new System.IO.Pipes.NamedPipeServerStream(npname, System.IO.Pipes.PipeDirection.InOut , 1, System.IO.Pipes.PipeTransmissionMode.Byte , System.IO.Pipes.PipeOptions.Asynchronous | System.IO.Pipes.PipeOptions.WriteThrough, 65536, 65536)) { await nps.WaitForConnectionAsync(ApplicationCTS.Token); everConnected = true; byte[] msg = System.Text.Encoding.ASCII.GetBytes(CurrentProcess.Id.ToString()); await nps.WriteAsync(msg); await nps.FlushAsync(); //nps.Disconnect(); //do not disconnect nps.Close(); } } catch (Exception x) { if (ApplicationCTS.IsCancellationRequested) return; WriteDebugLine(x); if (!everConnected) { await Task.Delay(1000); continue; } } PostToAppThread(delegate { WriteDebugLine("ServerActivator"); var mf = MainBrowser?.FindForm(); if (mf?.Visible == true) { ActivateForm(mf); return; } var forms = WF.Application.OpenForms; for (var i = 0; i < forms.Count; i++) { var form = forms[i]; if (!form.Visible) continue; //WindowsInterop.SetForegroundWindow(form.Handle); ActivateForm(form); break; } }); } }
public void openConnection() { string message = ""; Boolean loop = true; while (loop) { //set up the reciever pipe - this will listen for compute messages from the java plugin. using (System.IO.Pipes.NamedPipeServerStream _pipeServer = new System.IO.Pipes.NamedPipeServerStream(_recieverPipeName, System.IO.Pipes.PipeDirection.InOut, 5)) { Console.WriteLine("Named Pipe " + _recieverPipeName + " Started"); Console.WriteLine("Waiting for Client To Connect to " + _recieverPipeName); //set up the broadcast pipe - this will send a message to the java plugin (preferably when the requested compute is complete) using (System.IO.Pipes.NamedPipeServerStream _senderPipeServer = new System.IO.Pipes.NamedPipeServerStream(_senderPipeName, System.IO.Pipes.PipeDirection.InOut, 5)) { //establish valid links for the recieving and sending pipes. try { Console.WriteLine("Named Pipe " + _senderPipeName + " Started"); Console.WriteLine("Waiting for Client To Connect to " + _senderPipeName); _pipeServer.WaitForConnection(); Console.WriteLine("Client Connected to " + _recieverPipeName); _senderPipeServer.WaitForConnection(); Console.WriteLine("Client Connected to " + _senderPipeName); //now that there are valid connections, establish stream readers and writers accordingly StreamReader sr = new StreamReader(_pipeServer); StreamWriter sw = new StreamWriter(_senderPipeServer); sw.AutoFlush = true; //this sr.ReadLine() will wait until a message has been sent from the java plugin. message = sr.ReadLine(); //process the message if (!Implementations.ParserFactory.ParserFactory.Instance.parse(message)) { if (Implementations.ParserFactory.ParserFactory.Instance.isValid) { sw.WriteLine("Compute Successful!"); } else { sw.WriteLine("Compute Failed! " + Implementations.ParserFactory.ParserFactory.Instance.message); } } //ensure that the response sw.WriteLine("Message Recieved: " + message); } catch (Exception ex) { try { _senderPipeServer.Close(); } catch (Exception e1) { } try { _pipeServer.Close(); } catch (Exception e2) { } Console.WriteLine(ex.InnerException); } } } if (message == "Exit") { loop = false; } } }