示例#1
0
        /// <summary>
        /// Reads the data sent from the server named pipe endpoint as a stream
        /// </summary>
        /// <returns>The stream object containing the data read from the server</returns>
        public System.IO.Stream ReadStream()
        {
            try
            {
                System.Diagnostics.Trace.WriteLine(
                    "ReadStream() called",
                    "TransMock.Communication.NamedPipes.StreamingNamedPipeClient");

                byte[]       inBuffer  = new byte[256];
                MemoryStream msgStream = new MemoryStream(4096);

                int  byteCountRead = 0;
                bool eofReached    = false;

                while (!eofReached)
                {
                    byteCountRead = this.pipeClient.Read(inBuffer, 0, inBuffer.Length);

                    eofReached = NamedPipeMessageUtils.IsEndOfMessage(
                        inBuffer,
                        byteCountRead);

                    if (eofReached && byteCountRead > 0)
                    {
                        byteCountRead -= NamedPipeMessageUtils.EndOfMessage.Length;
                        // We take tha contents without the EndOfMessage sequence
                        //inBuffer = inBuffer
                        //    .Take(byteCountRead)
                        //    .ToArray();
                    }

                    msgStream.Write(
                        inBuffer,
                        0,
                        byteCountRead);
                }

                System.Diagnostics.Trace.WriteLine(
                    "ReadStream() succeeded",
                    "TransMock.Communication.NamedPipes.StreamingNamedPipeClient");

                // Rewind the message stream to the beginning
                msgStream.Seek(0, SeekOrigin.Begin);

                return(msgStream);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(
                    "ReadStream() threw exception: " + ex.Message,
                    "TransMock.Communication.NamedPipes.StreamingNamedPipeClient");

                throw;
            }
        }
示例#2
0
        /// <summary>
        /// Reads the data sent from the server named pipe endpoint as a stream
        /// </summary>
        /// <returns>The stream object containing the data read from the server</returns>
        public async Task <System.IO.Stream> ReadStreamAsync()
        {
            try
            {
                System.Diagnostics.Trace.WriteLine(
                    "ReadStreamAsync() called",
                    "TransMock.Communication.NamedPipes.StreamingNamedPipeClientAsync");

                byte[]       inBuffer  = new byte[256];
                MemoryStream msgStream = new MemoryStream(4096);

                int  byteCountRead = 0;
                bool eofReached    = false;

                while (!eofReached)
                {
                    byteCountRead = await this.pipeClient
                                    .ReadAsync(inBuffer, 0, inBuffer.Length);

                    eofReached = NamedPipeMessageUtils.IsEndOfMessage(
                        inBuffer,
                        byteCountRead);

                    if (eofReached && byteCountRead > 0)
                    {
                        byteCountRead -= NamedPipeMessageUtils.EndOfMessage.Length;
                    }

                    msgStream.Write(
                        inBuffer,
                        0,
                        byteCountRead);
                }

                System.Diagnostics.Trace.WriteLine(
                    "ReadStreamAsync() succeeded",
                    "TransMock.Communication.NamedPipes.StreamingNamedPipeClientAsync");

                // Rewind the message stream to the beginning
                msgStream.Seek(0, SeekOrigin.Begin);

                return(msgStream);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(
                    "ReadStreamAsync() threw exception: " + ex.Message,
                    "TransMock.Communication.NamedPipes.StreamingNamedPipeClientAsync");

                throw;
            }
        }
示例#3
0
        /// <summary>
        /// Asynchronous callback for a read operation from the pipe
        /// </summary>
        /// <param name="ar">The async result instanced passed to the method</param>
        private void PipeReadAsync(IAsyncResult ar)
        {
            try
            {
                System.Diagnostics.Debug.WriteLine(
                    $"Beginning reading from the pipe. Thread id: {Thread.CurrentThread.ManagedThreadId}.",
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServer");

                // Extracting the pipe connection from which the data is being read
                var state = ar.AsyncState as AsyncReadState;
                NamedPipeServerStream pipeConnection;

                lock (this.pipeSyncLock)
                {
                    System.Diagnostics.Debug.WriteLine(
                        $@"Acquired lock for getting connection for client id: {state.ConnectionId}. 
                            Thread id: {Thread.CurrentThread.ManagedThreadId}.",
                        "TransMock.Communication.NamedPipe.StreamingNamedPipeServer");

                    pipeConnection = this.pipeServerConnections[state.ConnectionId];
                }

                int  bytesRead  = pipeConnection.EndRead(ar);
                bool eofReached = false;

                System.Diagnostics.Debug.WriteLine(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Read {0} bytes",
                        bytesRead),
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServer");

                eofReached = NamedPipeMessageUtils.IsEndOfMessage(
                    state.RawData,
                    bytesRead);

                System.Diagnostics.Debug.WriteLine(
                    $@"eofReached is: {eofReached}. 
                            Thread id: {Thread.CurrentThread.ManagedThreadId}.",
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServer");

                if (eofReached && bytesRead > 0)
                {
                    bytesRead -= NamedPipeMessageUtils.EndOfMessage.Length;
                    // We take tha contents without the EndOfMessage sequence
                    state.RawData = state.RawData
                                    .Take(bytesRead)
                                    .ToArray();
                }

                state.InStream.Write(
                    state.RawData,
                    0,
                    bytesRead);

                System.Diagnostics.Debug.WriteLine(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Written {0} bytes to the internal stream",
                        bytesRead),
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServer");

                if (!eofReached)
                {
                    // Not EOF, continue reading
                    pipeConnection.BeginRead(
                        state.RawData,
                        0,
                        state.RawData.Length,
                        cb => this.PipeReadAsync(cb),
                        state);
                }
                else
                {
                    // EOF reached, constructing the message
                    System.Diagnostics.Debug.WriteLine(
                        "Message was read from the pipe",
                        "TransMock.Communication.NamedPipe.StreamingNamedPipeServer");

                    // Rewind the stream
                    state.InStream.Seek(0, SeekOrigin.Begin);

                    lock (this.pipeSyncLock)
                    {
                        // Notify that the read was complete
                        this.OnReadCompleted(state.ConnectionId, state.InStream);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(
                    "PipeReadAsync threw an exception: " + ex.Message,
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServer");
            }
        }
        /// <summary>
        /// Asynchronous callback for a read operation from the pipe
        /// </summary>
        /// <param name="ar">The async result instanced passed to the method</param>
        private void PipeReadAsync(IAsyncResult ar)
        {
            try
            {
                System.Diagnostics.Debug.WriteLine(
                    "Beginning reading from the pipe",
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServer");

                // Extracting the pipe connection from which the data is being read
                var state = ar.AsyncState as AsyncReadState;
                NamedPipeServerStream pipeConnection;

                lock (this.pipeSyncLock)
                {
                    pipeConnection = this.pipeServerConnections[state.ConnectionId];
                }

                int  bytesRead  = pipeConnection.EndRead(ar);
                bool eofReached = false;

                System.Diagnostics.Debug.WriteLine(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Read {0} bytes",
                        bytesRead),
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServer");

                if (bytesRead > 0)
                {
                    eofReached = NamedPipeMessageUtils.IsEndOfMessage(
                        state.RawData.Length,
                        bytesRead);
                }
                else
                {
                    eofReached = true;
                }

                state.InStream.Write(
                    state.RawData,
                    0,
                    bytesRead);

                System.Diagnostics.Debug.WriteLine(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Written {0} bytes to the internal stream",
                        bytesRead),
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServer");

                if (!eofReached)
                {
                    // Not EOF, continue reading
                    pipeConnection.BeginRead(
                        state.RawData,
                        0,
                        state.RawData.Length,
                        cb => this.PipeReadAsync(cb),
                        state);
                }
                else
                {
                    // EOF reached, constructing the message
                    System.Diagnostics.Debug.WriteLine(
                        "Message was read from the pipe",
                        "TransMock.Communication.NamedPipe.StreamingNamedPipeServer");

                    // Rewind the stream
                    state.InStream.Seek(0, SeekOrigin.Begin);

                    lock (this.pipeSyncLock)
                    {
                        // Notify that the read was complete
                        this.OnReadCompleted(state.ConnectionId, state.InStream);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(
                    "PipeReadAsync threw an exception: " + ex.Message,
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServer");
            }
        }
        /// <summary>
        /// Asynchronous callback for a read operation from the pipe
        /// </summary>
        /// <param name="ar">The async result instanced passed to the method</param>
        private async Task PipeReadAsync(AsyncReadState state)
        {
            try
            {
                System.Diagnostics.Debug.WriteLine(
                    $"Beginning reading from the pipe. Thread id: {Thread.CurrentThread.ManagedThreadId}.",
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServerAsync");

                NamedPipeServerStream pipeConnection;

                System.Diagnostics.Debug.WriteLine(
                    $@"Getting connection for client id: {state.ConnectionId}. 
                        Thread id: {Thread.CurrentThread.ManagedThreadId}.",
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServerAsync");

                while (!this.pipeServerConnections
                       .TryGetValue(state.ConnectionId, out pipeConnection))
                {
                    System.Diagnostics.Debug.WriteLine(
                        $@"Did not manage to acwuire connection for client id: {state.ConnectionId}. 
                        Continuing to try...",
                        "TransMock.Communication.NamedPipe.StreamingNamedPipeServerAsync");
                }

                if (!pipeConnection.IsConnected)
                {
                    System.Diagnostics.Debug.WriteLine(
                        "Pipe has not been connected yet! Exiting",
                        "TransMock.Communication.NamedPipe.StreamingNamedPipeServerAsync");

                    return;
                }



                byte[] buffer    = new byte[4096];
                int    bytesRead = await pipeConnection.ReadAsync(buffer, 0, buffer.Length);

                bool eofReached = false;

                System.Diagnostics.Debug.WriteLine(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Read {0} bytes",
                        bytesRead),
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServerAsync");

                eofReached = NamedPipeMessageUtils.IsEndOfMessage(
                    buffer,
                    bytesRead);

                System.Diagnostics.Debug.WriteLine(
                    $@"eofReached is: {eofReached}. 
                            Thread id: {Thread.CurrentThread.ManagedThreadId}.",
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServerAsync");

                if (eofReached && bytesRead > 0)
                {
                    bytesRead -= NamedPipeMessageUtils.EndOfMessage.Length;
                    // We take tha contents without the EndOfMessage sequence
                    buffer = buffer
                             .Take(bytesRead)
                             .ToArray();
                }

                state.InStream.Write(
                    buffer,
                    0,
                    bytesRead);

                System.Diagnostics.Debug.WriteLine(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Written {0} bytes to the internal stream",
                        bytesRead),
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServerAsync");

                if (!eofReached)
                {
                    // Not EOF, continue reading
                    await PipeReadAsync(state);
                }
                else
                {
                    // EOF reached, constructing the message
                    System.Diagnostics.Debug.WriteLine(
                        "Message was read from the pipe",
                        "TransMock.Communication.NamedPipe.StreamingNamedPipeServerAsync");

                    // Rewind the stream
                    state.InStream.Seek(0, SeekOrigin.Begin);

                    // Notify that the read was complete
                    this.OnReadCompleted(state.ConnectionId, state.InStream);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(
                    "PipeReadAsync threw an exception: " + ex.Message,
                    "TransMock.Communication.NamedPipe.StreamingNamedPipeServerAsync");
            }
        }