public static Task <bool> SendMessageAsync(IpcCommands command, string pipeName, object message = null, bool wait = true) { string userPipeName = GetUserPipeName(pipeName); return(Task.Run <bool>(new Func <bool>(() => { try { using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", userPipeName, PipeDirection.Out, PipeOptions.None, TokenImpersonationLevel.None)) { using (MemoryStream ms = new MemoryStream()) { if (wait) { if (!WaitForNamedPipeConnection(userPipeName)) { return false; } } else if (NamedPipeDoesNotExist(userPipeName)) { return false; } pipeClient.Connect(10); ms.WriteByte((byte)command); if (message != null) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, message); } ms.Seek(0, SeekOrigin.Begin); ms.CopyTo(pipeClient); pipeClient.Flush(); pipeClient.WaitForPipeDrain(); } } return true; } catch (IOException) { return false; } catch (TimeoutException) { return false; } catch (Exception e) { Logging.LogException(e); return false; } }))); }
public static object ReadMessages(PipeStream pipe, out IpcCommands command) { using (MemoryStream memoryStream = new MemoryStream()) { BinaryFormatter binForm = new BinaryFormatter(); pipe.CopyTo(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); command = (IpcCommands)memoryStream.ReadByte(); return(memoryStream.Length == memoryStream.Position ? null : binForm.Deserialize(memoryStream)); } }
void IpcProcessConnection(IAsyncResult ar) { try { // connect and read command ipcServer.EndWaitForConnection(ar); int raw = ipcServer.ReadByte(); if (raw >= 0) { IpcCommands command = (IpcCommands)raw; // commands interact with the main form and must therefore be processed in the UI thread this.Dispatcher.Invoke(new Action(() => { try { if (command == IpcCommands.ShowMainWindow) { this.MainWindow.ShowWindow(); } else if (command == IpcCommands.Exit) { this.MainWindow.CloseWindow(); } } catch (Exception ex) { log.Error("Error processing Ipc Message: " + ex.Message + " (" + ex.GetType().Name + ")", ex); } })); } } catch (Exception ex) { log.Error("Error processing Ipc Message: " + ex.Message + " (" + ex.GetType().Name + ")", ex); } try { ipcServer.Disconnect(); ipcServer.BeginWaitForConnection(new AsyncCallback(IpcProcessConnection), null); } catch (Exception ex) { log.Error("Could not listen on Ipc Channel: " + ex.Message + " (" + ex.GetType().Name + ")", ex); } }
private void RunSendingServer(string pipeName, IpcCommands command, Func <object> function) { _namedPipeServer = new NamedPipeServerStream(NamedPipe.GetUserPipeName(pipeName), PipeDirection.Out, 2, PipeTransmissionMode.Message, PipeOptions.Asynchronous); AsyncCallback ac = null; ac = o => { NamedPipeServerStream s = (NamedPipeServerStream)o.AsyncState; try { if (disposedValue) { return; } s.EndWaitForConnection(o); object data = function.Invoke(); using (MemoryStream ms = new MemoryStream()) { ms.WriteByte((byte)command); if (data != null) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, data); } ms.Seek(0, SeekOrigin.Begin); ms.CopyTo(s); s.Flush(); s.WaitForPipeDrain(); } s.Disconnect(); s.BeginWaitForConnection(ac, s); } catch (Exception) { s.Dispose(); RunSendingServer(pipeName, command, function); } }; _namedPipeServer.BeginWaitForConnection(ac, _namedPipeServer); }
public bool ProcessMessages(IpcCommands command, object data) { _synchronizationContext.Post(state => { switch (command) { case IpcCommands.StartTeaching: PointCapture.Instance.Mode = CaptureMode.Training; break; case IpcCommands.StopTraining: if (PointCapture.Instance.Mode != CaptureMode.UserDisabled) { PointCapture.Instance.Mode = CaptureMode.Normal; } break; case IpcCommands.LoadApplications: ApplicationManager.Instance.LoadApplications().Wait(); break; case IpcCommands.LoadGestures: GestureManager.Instance.LoadGestures().Wait(); break; case IpcCommands.LoadConfiguration: AppConfig.Reload(); break; case IpcCommands.StartControlPanel: TrayManager.StartControlPanel(); break; } }, null); return(true); }
public bool ProcessMessages(IpcCommands command, object data) { try { Application.Current?.Dispatcher.InvokeAsync(() => { switch (command) { case IpcCommands.Exit: { Application.Current.Shutdown(); break; } case IpcCommands.GotGesture: { var newGesture = data as Point[][][]; if (newGesture == null) { return; } GotNewPattern?.Invoke(this, newGesture.Select(list => new PointPattern(list)).ToArray()); break; } } }, DispatcherPriority.Input); return(true); } catch (Exception e) { MessageBox.Show(e.Message); return(false); } }
public CustomNamedPipeServer(string pipeName, IpcCommands command, Func <object> function) { RunSendingServer(pipeName, command, function); }
private IntPtr SendCommand(IpcCommands command, int value) { return(NativeMethods.SendMessage(this.process.MainWindowHandle, WM_WA_IPC, value, (int)command)); }
private IntPtr SendCommand(IpcCommands command) { return(SendCommand(command, 0)); }