/// <summary> /// Returns information about data in the pipe /// </summary> public int GetBytesAvailable() { // check object state if (_disposed) { throw new ObjectDisposedException(GetType().FullName); } if (_instance == null) { throw new InvalidOperationException("Pipe is not connected"); } // check pipe buffer int bytesRead; int totalBytes; int bytesLeftThisMessage; bool error = PipeNative.PeekNamedPipe( _instance.Handle, IntPtr.Zero, 0, out bytesRead, out totalBytes, out bytesLeftThisMessage); if (!error) { // error occured throw new PipeIOException(Marshal.GetLastWin32Error(), "Could not read data from the pipe: " + _instance.Name); } return(totalBytes); }
/// <summary> /// Copies data into a buffer without removing it from the pipe /// </summary> public int Peek(byte[] buffer, int offset, int size) { // check object state if (_disposed) { throw new ObjectDisposedException(GetType().FullName); } if (_instance == null) { throw new InvalidOperationException("Pipe is not connected"); } // parameters validation if (buffer == null) { throw new ArgumentNullException("buffer"); } if (offset < 0 || offset > buffer.Length) { throw new ArgumentOutOfRangeException("offset"); } if (size < 0 || size > buffer.Length - offset) { throw new ArgumentOutOfRangeException("size"); } // read data from the pipe GCHandle gcBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned); int bytesRead; int totalBytes; int bytesLeftThisMessage; bool error = PipeNative.PeekNamedPipe( _instance.Handle, Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset), size, out bytesRead, out totalBytes, out bytesLeftThisMessage); gcBuffer.Free(); if (!error) { // error occured throw new PipeIOException(Marshal.GetLastWin32Error(), "Could not read data from the pipe: " + _instance.Name); } return(bytesRead); }