Ejemplo n.º 1
0
        public void RunReadMessageLoop()
        {
            try
            {
                var messageStream = new MessageStream(InStream);

                while (true)
                {
                    var message = messageStream.ReadMessage();
                    if (message == MessageStream.EndOfStream)
                    {
                        break;
                    }

                    ThreadPool.QueueUserWorkItem(o => HandleMessage(message));
                }
            }
            catch (System.IO.IOException ex)
            {
                Log.Error("Reading failed.", ex);
            }
            catch (ObjectDisposedException)
            {
                // Can ignore this, means the other endpoint closed the pipe
            }
        }
        private static NamedPipeClientStream CreateOutputStream(NamedPipeServerStream srv)
        {
            var stream      = new MessageStream(srv);
            var outPipeName = stream.ReadMessage();
            var outStream   = new NamedPipeClientStream(".", outPipeName, PipeDirection.Out);

            outStream.Connect(2000);
            return(outStream);
        }
Ejemplo n.º 3
0
        private void Close()
        {
            this.outMessageStream = null;

            InStream?.Dispose();
            OutStream?.Dispose();

            InStream  = null;
            OutStream = null;
        }
Ejemplo n.º 4
0
        private NamedPipeServerStream CreateRecievingPipeServer(Stream client)
        {
            var receivingPipe = Guid.NewGuid().ToString();
            var messageStream = new MessageStream(client);

            messageStream.WriteMessage(receivingPipe);

            return(this.pipeSecurity != null
                ? new NamedPipeServerStream(receivingPipe, PipeDirection.In, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Message, PipeOptions.Asynchronous, 0, 0, this.pipeSecurity)
                : new NamedPipeServerStream(receivingPipe, PipeDirection.In, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Message, PipeOptions.Asynchronous));
        }
Ejemplo n.º 5
0
        private void Connect(int timeout = 500)
        {
            if (PipeMessengerState.Disconnected != Interlocked.CompareExchange(
                    ref this.connectionState,
                    PipeMessengerState.Connecting,
                    PipeMessengerState.Disconnected))
            {
                // we already have one connection going on
                return;
            }

            NamedPipeClientStream outStream = null;
            NamedPipeServerStream inStream  = null;

            try
            {
                outStream = new NamedPipeClientStream(".", this.outPipeName, PipeDirection.Out);
                outStream.Connect(timeout);

                inStream = CreateRecievingPipeServer(outStream);

                InStream              = inStream;
                OutStream             = outStream;
                this.outMessageStream = new MessageStream(outStream);

                inStream.BeginWaitForConnection(Connected, this);
            }
            catch (Exception)
            {
                outStream?.Dispose();
                inStream?.Dispose();
                Close();
                this.connectionState = PipeMessengerState.Disconnected;

                throw;
            }
        }
Ejemplo n.º 6
0
 public PipeMessenger(PipeStream inStream, PipeStream outStream)
 {
     InStream              = inStream;
     OutStream             = outStream;
     this.outMessageStream = new MessageStream(outStream);
 }