Ejemplo n.º 1
0
        /// <summary>
        /// Writes a message to the instantiated Windows pipe.
        /// </summary>
        /// <param name="message">IPC message to write.</param>
        /// <returns>True on success.</returns>
        public bool WriteToPipe(IPCMessage message)
        {
            try
            {
                var buffer      = Encoding.UTF8.GetBytes(message.ToString());
                var writeResult = Kernel32.WriteFile(this.writePipe, buffer, (uint)buffer.Length, out uint bytesWritten, IntPtr.Zero);
                if (!writeResult)
                {
                    var lastWindowsError = Marshal.GetLastWin32Error();
                    if (lastWindowsError == ErrorBrokenPipe)
                    {
                        ErrorHandling.ErrorHandler.Handle("Could not write to pipe, got ERROR_BROKEN_PIPE from Windows", ErrorHandling.LogLevel.Debug);
                        this.StopListenerThread();
                    }

                    return(false);
                }
            }
            catch (Exception e)
            {
                ErrorHandling.ErrorHandler.Handle(e, ErrorHandling.LogLevel.Error);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Writes a message to a named pipe.
        /// </summary>
        /// <param name="pipe">Named pipe instance to write to.</param>
        /// <param name="message">IPCMessage to send.</param>
        public static void WriteToPipe(NamedPipeClientStream pipe, IPCMessage message)
        {
            var bytes = Encoding.UTF8.GetBytes(message.ToString());

            pipe.Write(bytes, 0, bytes.Length);
            pipe.Flush();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Writes a message to the instantiated Windows pipe.
        /// </summary>
        /// <param name="message">IPC message to write.</param>
        /// <param name="promptRestartBrokerServiceOnFail">Prompt the user to restart a stopped broker service.</param>
        /// <returns>True on success.</returns>
        public bool WriteToPipe(IPCMessage message, bool promptRestartBrokerServiceOnFail = true)
        {
            try
            {
                var buffer = Encoding.UTF8.GetBytes(message.ToString());
                pipe.Write(buffer, 0, buffer.Length);
                pipe.Flush();
            }
            catch (Exception e)
            {
                if (pipe is NamedPipeClientStream && (e is InvalidOperationException || e is IOException))
                {
                    if (promptRestartBrokerServiceOnFail)
                    {
                        Broker.PromptRestartBrokerService();
                    }
                }

                ErrorHandling.ErrorHandler.Handle(e, ErrorHandling.LogLevel.Error);
                return(false);
            }

            return(true);
        }