コード例 #1
0
        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;
                }
            })));
        }
コード例 #2
0
        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));
            }
        }
コード例 #3
0
        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);
            }
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
 public CustomNamedPipeServer(string pipeName, IpcCommands command, Func <object> function)
 {
     RunSendingServer(pipeName, command, function);
 }
コード例 #8
0
 private IntPtr SendCommand(IpcCommands command, int value)
 {
     return(NativeMethods.SendMessage(this.process.MainWindowHandle, WM_WA_IPC, value, (int)command));
 }
コード例 #9
0
 private IntPtr SendCommand(IpcCommands command)
 {
     return(SendCommand(command, 0));
 }