2 using Microsoft.Win32.SafeHandles;
16 private static readonly
bool _canUseAsync;
23 private bool m_canRead;
25 private bool m_canWrite;
27 private bool m_isAsync;
29 private bool m_isMessageComplete;
31 private bool m_isFromExistingHandle;
33 private bool m_isHandleExposed;
41 private int m_outBufferSize;
43 private PipeState m_state;
52 return State == PipeState.Connected;
56 m_state = (value ? PipeState.Connected : PipeState.Disconnected);
75 if (m_state == PipeState.WaitingToConnect)
79 if (m_state == PipeState.Disconnected)
87 if (m_state == PipeState.Closed)
91 if (m_handle.IsClosed)
99 return m_isMessageComplete;
114 if (m_isFromExistingHandle)
116 if (!Microsoft.Win32.UnsafeNativeMethods.GetNamedPipeInfo(m_handle, out
int lpFlags, Microsoft.Win32.UnsafeNativeMethods.NULL, Microsoft.Win32.UnsafeNativeMethods.NULL, Microsoft.Win32.UnsafeNativeMethods.NULL))
120 if ((lpFlags & 4) != 0)
126 return m_transmissionMode;
145 if (!Microsoft.Win32.UnsafeNativeMethods.GetNamedPipeInfo(m_handle, Microsoft.Win32.UnsafeNativeMethods.NULL, Microsoft.Win32.UnsafeNativeMethods.NULL, out
int lpInBufferSize, Microsoft.Win32.UnsafeNativeMethods.NULL))
149 return lpInBufferSize;
170 return m_outBufferSize;
172 if (!Microsoft.Win32.UnsafeNativeMethods.GetNamedPipeInfo(m_handle, Microsoft.Win32.UnsafeNativeMethods.NULL, out
int lpOutBufferSize, Microsoft.Win32.UnsafeNativeMethods.NULL, Microsoft.Win32.UnsafeNativeMethods.NULL))
176 return lpOutBufferSize;
206 int num = (int)value << 1;
207 if (!Microsoft.Win32.UnsafeNativeMethods.SetNamedPipeHandleState(m_handle, &num, Microsoft.Win32.UnsafeNativeMethods.NULL, Microsoft.Win32.UnsafeNativeMethods.NULL))
227 if (m_handle ==
null)
231 if (m_handle.IsClosed)
235 m_isHandleExposed =
true;
272 public override long Length 297 internal PipeState State
310 static unsafe PipeStream()
312 _canUseAsync = (Environment.OSVersion.Platform ==
PlatformID.Win32NT);
313 IOCallback = AsyncPSCallback;
324 if (direction < PipeDirection.In || direction >
PipeDirection.InOut)
345 if (direction < PipeDirection.In || direction >
PipeDirection.InOut)
349 if (transmissionMode < PipeTransmissionMode.Byte || transmissionMode >
PipeTransmissionMode.Message)
353 if (outBufferSize < 0)
357 Init(direction, transmissionMode, outBufferSize);
362 m_readMode = transmissionMode;
363 m_transmissionMode = transmissionMode;
364 m_pipeDirection = direction;
373 m_outBufferSize = outBufferSize;
374 m_isMessageComplete =
true;
375 m_state = PipeState.WaitingToConnect;
388 isAsync &= _canUseAsync;
408 m_isHandleExposed = isExposed;
409 m_isFromExistingHandle = isExposed;
429 public override int Read([
In] [
Out]
byte[] buffer,
int offset,
int count)
443 if (buffer.Length - offset < count)
452 return ReadCore(buffer, offset, count);
456 private int ReadCore(
byte[] buffer,
int offset,
int count)
460 IAsyncResult asyncResult = BeginReadCore(buffer, offset, count,
null,
null);
464 int num = ReadFileNative(m_handle, buffer, offset, count,
null, out hr);
467 if (hr == 109 || hr == 233)
469 State = PipeState.Broken;
477 m_isMessageComplete = (hr != 234);
500 [HostProtection(
SecurityAction.LinkDemand, ExternalThreading =
true)]
515 if (buffer.Length - offset < count)
526 if (m_state == PipeState.Broken)
528 PipeStreamAsyncResult pipeStreamAsyncResult =
new PipeStreamAsyncResult();
529 pipeStreamAsyncResult._handle = m_handle;
530 pipeStreamAsyncResult._userCallback = callback;
531 pipeStreamAsyncResult._userStateObject = state;
532 pipeStreamAsyncResult._isWrite =
false;
533 pipeStreamAsyncResult.CallUserCallback();
534 return pipeStreamAsyncResult;
536 return base.BeginRead(buffer, offset, count, callback, state);
538 return BeginReadCore(buffer, offset, count, callback, state);
542 private unsafe PipeStreamAsyncResult BeginReadCore(
byte[] buffer,
int offset,
int count,
AsyncCallback callback,
object state)
544 PipeStreamAsyncResult pipeStreamAsyncResult =
new PipeStreamAsyncResult();
545 pipeStreamAsyncResult._handle = m_handle;
546 pipeStreamAsyncResult._userCallback = callback;
547 pipeStreamAsyncResult._userStateObject = state;
548 pipeStreamAsyncResult._isWrite =
false;
549 if (buffer.Length == 0)
551 pipeStreamAsyncResult.CallUserCallback();
557 NativeOverlapped* ptr = pipeStreamAsyncResult._overlapped = overlapped.
Pack(IOCallback, buffer);
559 int num = ReadFileNative(m_handle, buffer, offset, count, ptr, out hr);
566 State = PipeState.Broken;
568 pipeStreamAsyncResult.CallUserCallback();
578 return pipeStreamAsyncResult;
592 if (asyncResult ==
null)
598 return base.EndRead(asyncResult);
600 PipeStreamAsyncResult pipeStreamAsyncResult = asyncResult as PipeStreamAsyncResult;
601 if (pipeStreamAsyncResult ==
null || pipeStreamAsyncResult._isWrite)
609 WaitHandle waitHandle = pipeStreamAsyncResult._waitHandle;
610 if (waitHandle !=
null)
622 if (overlapped !=
null)
626 if (pipeStreamAsyncResult._errorCode != 0)
628 WinIOError(pipeStreamAsyncResult._errorCode);
630 m_isMessageComplete = (m_state == PipeState.Broken || pipeStreamAsyncResult._isMessageComplete);
631 return pipeStreamAsyncResult._numBytes;
649 public override void Write(
byte[] buffer,
int offset,
int count)
663 if (buffer.Length - offset < count)
672 WriteCore(buffer, offset, count);
676 private void WriteCore(
byte[] buffer,
int offset,
int count)
680 IAsyncResult asyncResult = BeginWriteCore(buffer, offset, count,
null,
null);
685 int num = WriteFileNative(m_handle, buffer, offset, count,
null, out hr);
711 [HostProtection(
SecurityAction.LinkDemand, ExternalThreading =
true)]
726 if (buffer.Length - offset < count)
737 return base.BeginWrite(buffer, offset, count, callback, state);
739 return BeginWriteCore(buffer, offset, count, callback, state);
743 private unsafe PipeStreamAsyncResult BeginWriteCore(
byte[] buffer,
int offset,
int count,
AsyncCallback callback,
object state)
745 PipeStreamAsyncResult pipeStreamAsyncResult =
new PipeStreamAsyncResult();
746 pipeStreamAsyncResult._userCallback = callback;
747 pipeStreamAsyncResult._userStateObject = state;
748 pipeStreamAsyncResult._isWrite =
true;
749 pipeStreamAsyncResult._handle = m_handle;
750 if (buffer.Length == 0)
752 pipeStreamAsyncResult.CallUserCallback();
758 NativeOverlapped* ptr = pipeStreamAsyncResult._overlapped = overlapped.
Pack(IOCallback, buffer);
760 int num = WriteFileNative(m_handle, buffer, offset, count, ptr, out hr);
761 if (num == -1 && hr != 997)
770 return pipeStreamAsyncResult;
783 if (asyncResult ==
null)
789 base.EndWrite(asyncResult);
792 PipeStreamAsyncResult pipeStreamAsyncResult = asyncResult as PipeStreamAsyncResult;
793 if (pipeStreamAsyncResult ==
null || !pipeStreamAsyncResult._isWrite)
801 WaitHandle waitHandle = pipeStreamAsyncResult._waitHandle;
802 if (waitHandle !=
null)
814 if (overlapped !=
null)
818 if (pipeStreamAsyncResult._errorCode != 0)
820 WinIOError(pipeStreamAsyncResult._errorCode);
827 if (buffer.Length == 0)
833 int numBytesRead = 0;
834 fixed (
byte* ptr = buffer)
836 num = ((!m_isAsync) ? Microsoft.Win32.UnsafeNativeMethods.ReadFile(handle, ptr + offset, count, out numBytesRead,
IntPtr.
Zero) : Microsoft.Win32.UnsafeNativeMethods.ReadFile(handle, ptr + offset, count,
IntPtr.
Zero, overlapped));
854 if (buffer.Length == 0)
859 int numBytesWritten = 0;
861 fixed (
byte* ptr = buffer)
863 num = ((!m_isAsync) ? Microsoft.Win32.UnsafeNativeMethods.WriteFile(handle, ptr + offset, count, out numBytesWritten, IntPtr.Zero) : Microsoft.Win32.UnsafeNativeMethods.WriteFile(handle, ptr + offset, count, IntPtr.Zero, overlapped));
871 return numBytesWritten;
888 byte[] array =
new byte[1];
889 if (ReadCore(array, 0, 1) == 0)
910 WriteCore(
new byte[1]
942 if (!Microsoft.Win32.UnsafeNativeMethods.FlushFileBuffers(m_handle))
952 protected override void Dispose(
bool disposing)
956 if (m_handle !=
null && !m_handle.IsClosed)
963 base.Dispose(disposing);
965 m_state = PipeState.Closed;
969 private void UpdateReadMode()
971 if (!Microsoft.Win32.UnsafeNativeMethods.GetNamedPipeHandleState(
SafePipeHandle, out
int lpState, Microsoft.Win32.UnsafeNativeMethods.NULL, Microsoft.Win32.UnsafeNativeMethods.NULL, Microsoft.Win32.UnsafeNativeMethods.NULL, Microsoft.Win32.UnsafeNativeMethods.NULL, 0))
975 if ((lpState & 2) != 0)
994 if (m_state == PipeState.Closed)
998 if (m_handle ==
null)
1002 if (m_handle.IsClosed)
1020 if (pipeSecurity ==
null)
1025 pipeSecurity.
Persist(m_handle);
1049 if (m_handle ==
null)
1053 if (m_state == PipeState.Closed)
1057 if (m_handle.IsClosed)
1067 if (m_state == PipeState.WaitingToConnect)
1071 if (m_state == PipeState.Disconnected)
1075 if (m_handle ==
null)
1079 if (m_state == PipeState.Closed)
1083 if (m_handle.IsClosed)
1093 if (m_state == PipeState.WaitingToConnect)
1097 if (m_state == PipeState.Disconnected)
1101 if (m_handle ==
null)
1105 if (m_state == PipeState.Broken)
1109 if (m_state == PipeState.Closed)
1113 if (m_handle.IsClosed)
1120 internal void WinIOError(
int errorCode)
1127 m_state = PipeState.Broken;
1128 throw new IOException(
System.
SR.GetString(
"IO_IO_PipeBroken"), Microsoft.Win32.UnsafeNativeMethods.MakeHRFromErrorCode(errorCode));
1133 m_handle.SetHandleAsInvalid();
1134 m_state = PipeState.Broken;
1141 internal unsafe
static Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES GetSecAttrs(
HandleInheritability inheritability, PipeSecurity pipeSecurity, out
object pinningHandle)
1143 pinningHandle =
null;
1144 Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES sECURITY_ATTRIBUTES =
null;
1147 sECURITY_ATTRIBUTES =
new Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES();
1148 sECURITY_ATTRIBUTES.nLength =
Marshal.
SizeOf((
object)sECURITY_ATTRIBUTES);
1151 sECURITY_ATTRIBUTES.bInheritHandle = 1;
1153 if (pipeSecurity !=
null)
1155 byte[] securityDescriptorBinaryForm = pipeSecurity.GetSecurityDescriptorBinaryForm();
1157 byte[] array = securityDescriptorBinaryForm;
1158 fixed (
byte* pSecurityDescriptor = array)
1160 sECURITY_ATTRIBUTES.pSecurityDescriptor = pSecurityDescriptor;
1164 return sECURITY_ATTRIBUTES;
1168 internal static Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES GetSecAttrs(
HandleInheritability inheritability)
1170 Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES sECURITY_ATTRIBUTES =
null;
1173 sECURITY_ATTRIBUTES =
new Microsoft.Win32.UnsafeNativeMethods.SECURITY_ATTRIBUTES();
1174 sECURITY_ATTRIBUTES.nLength =
Marshal.
SizeOf((
object)sECURITY_ATTRIBUTES);
1175 sECURITY_ATTRIBUTES.bInheritHandle = 1;
1177 return sECURITY_ATTRIBUTES;
1181 private unsafe
static void AsyncPSCallback(uint errorCode, uint numBytes,
NativeOverlapped* pOverlapped)
1184 PipeStreamAsyncResult pipeStreamAsyncResult = (PipeStreamAsyncResult)overlapped.
AsyncResult;
1185 pipeStreamAsyncResult._numBytes = (
int)numBytes;
1186 if (!pipeStreamAsyncResult._isWrite && (errorCode == 109 || errorCode == 233 || errorCode == 232))
1191 if (errorCode == 234)
1194 pipeStreamAsyncResult._isMessageComplete =
false;
1198 pipeStreamAsyncResult._isMessageComplete =
true;
1200 pipeStreamAsyncResult._errorCode = (int)errorCode;
1201 pipeStreamAsyncResult._completedSynchronously =
false;
1202 pipeStreamAsyncResult._isComplete =
true;
1204 if (waitHandle !=
null && !waitHandle.
Set())
1208 pipeStreamAsyncResult._userCallback?.Invoke(pipeStreamAsyncResult);
override void WriteByte(byte value)
Writes a byte to the current stream.
PlatformID
Identifies the operating system, or platform, supported by an assembly.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
unsafe override int EndRead(IAsyncResult asyncResult)
Ends a pending asynchronous read request.
Describes a set of security permissions applied to code. This class cannot be inherited.
override bool CanRead
Gets a value indicating whether the current stream supports read operations.
override int Read([In] [Out] byte[] buffer, int offset, int count)
Reads a block of bytes from a stream and writes the data to a specified buffer.
void SetAccessControl(PipeSecurity pipeSecurity)
Applies the access control list (ACL) entries specified by a T:System.IO.Pipes.PipeSecurity object to...
Encapsulates operating system–specific objects that wait for exclusive access to shared resources.
unsafe NativeOverlapped * Pack(IOCompletionCallback iocb)
Packs the current instance into a T:System.Threading.NativeOverlapped structure, specifying the deleg...
PipeSecurity GetAccessControl()
Gets a T:System.IO.Pipes.PipeSecurity object that encapsulates the access control list (ACL) entries ...
virtual PipeTransmissionMode TransmissionMode
Gets the pipe transmission mode supported by the current pipe.
bool Set()
Sets the state of the event to signaled, allowing one or more waiting threads to proceed.
static unsafe Overlapped Unpack(NativeOverlapped *nativeOverlappedPtr)
Unpacks the specified unmanaged T:System.Threading.NativeOverlapped structure into a managed T:System...
IntPtr InternalLow
Specifies a system-dependent status. Reserved for operating system use.
override void Flush()
Clears the buffer for the current stream and causes any buffered data to be written to the underlying...
The exception that is thrown when the value of an argument is outside the allowable range of values a...
static int SizeOf(object structure)
Returns the unmanaged size of an object in bytes.
delegate void AsyncCallback(IAsyncResult ar)
References a method to be called when a corresponding asynchronous operation completes.
Provides a managed representation of a Win32 OVERLAPPED structure, including methods to transfer info...
override void Write(byte[] buffer, int offset, int count)
Writes a block of bytes to the current stream using data from a buffer.
bool IsHandleExposed
Gets a value indicating whether a handle to a T:System.IO.Pipes.PipeStream object is exposed.
void InitializeHandle(SafePipeHandle handle, bool isExposed, bool isAsync)
Initializes a T:System.IO.Pipes.PipeStream object from the specified T:Microsoft.Win32....
static unsafe void Free(NativeOverlapped *nativeOverlappedPtr)
Frees the unmanaged memory associated with a native overlapped structure allocated by the Overload:Sy...
HandleInheritability
Specifies whether the underlying handle is inheritable by child processes.
unsafe delegate void IOCompletionCallback(uint errorCode, uint numBytes, NativeOverlapped *pOVERLAP)
Receives the error code, number of bytes, and overlapped value type when an I/O operation completes o...
PipeTransmissionMode
Specifies the transmission mode of the pipe.
virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
Blocks the current thread until the current T:System.Threading.WaitHandle receives a signal,...
override long Seek(long offset, SeekOrigin origin)
Sets the current position of the current stream to the specified value.
override bool CanSeek
Gets a value indicating whether the current stream supports seek operations.
SeekOrigin
Specifies the position in a stream to use for seeking.
internal void Persist(SafeHandle handle)
Saves the specified sections of the security descriptor that is associated with the current T:System....
bool IsAsync
Gets a value indicating whether a T:System.IO.Pipes.PipeStream object was opened asynchronously or sy...
SecurityAction
Specifies the security actions that can be performed using declarative security.
internal void CheckWriteOperations()
Verifies that the pipe is in a connected state for write operations.
virtual int InBufferSize
Gets the size, in bytes, of the inbound buffer for a pipe.
Represents the status of an asynchronous operation.
Exposes a T:System.IO.Stream object around a pipe, which supports both anonymous and named pipes.
Represents a collection that can contain many different types of permissions.
Specifies that the pipe direction is out.
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
override long Position
Gets or sets the current position of the current stream.
PipeStream(PipeDirection direction, PipeTransmissionMode transmissionMode, int outBufferSize)
Initializes a new instance of the T:System.IO.Pipes.PipeStream class using the specified T:System....
unsafe override void EndWrite(IAsyncResult asyncResult)
Ends a pending asynchronous write request.
The exception that is thrown when an I/O error occurs.
override long Length
Gets the length of a stream, in bytes.
Defines the underlying structure of all code access permissions.
A platform-specific type that is used to represent a pointer or a handle.
Notifies one or more waiting threads that an event has occurred. This class cannot be inherited.
IAsyncResult AsyncResult
Gets or sets the object that provides status information on the I/O operation.
static GCHandle Alloc(object value)
Allocates a F:System.Runtime.InteropServices.GCHandleType.Normal handle for the specified object.
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
GCHandleType
Represents the types of handles the T:System.Runtime.InteropServices.GCHandle class can allocate.
internal void CheckReadOperations()
Verifies that the pipe is in a connected state for read operations.
Provides a way to access a managed object from unmanaged memory.
virtual unsafe PipeTransmissionMode ReadMode
Gets or sets the reading mode for a T:System.IO.Pipes.PipeStream object.
override void SetLength(long value)
Sets the length of the current stream to the specified value.
virtual void Close()
Releases all resources held by the current T:System.Threading.WaitHandle.
static void RevertAssert()
Causes any previous M:System.Security.CodeAccessPermission.Assert for the current frame to be removed...
PipeStream(PipeDirection direction, int bufferSize)
Initializes a new instance of the T:System.IO.Pipes.PipeStream class using the specified T:System....
The exception that is thrown when one of the arguments provided to a method is not valid.
override bool CanWrite
Gets a value indicating whether the current stream supports write operations.
Specifies that the pipe direction is in.
bool? IsConnected
Gets or sets a value indicating whether a T:System.IO.Pipes.PipeStream object is connected.
static bool BindHandle(IntPtr osHandle)
Binds an operating system handle to the T:System.Threading.ThreadPool.
override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
Begins an asynchronous read operation.
override int ReadByte()
Reads a byte from a pipe.
virtual internal void CheckPipePropertyOperations()
Verifies that the pipe is in a proper state for getting or setting properties.
Represents the access control and audit security for a pipe.
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
Begins an asynchronous write operation.
PipeDirection
Specifies the direction of the pipe.
The exception that is thrown when a method call is invalid for the object's current state.
bool IsMessageComplete
Gets a value indicating whether there is more data in the message returned from the most recent read ...
void WaitForPipeDrain()
Waits for the other end of the pipe to read all sent bytes.
void Assert()
Declares that the calling code can access the resource protected by a permission demand through the c...
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
SecurityPermissionFlag
Specifies access flags for the security permission object.
AccessControlSections
Specifies which sections of a security descriptor to save or load.
Provides atomic operations for variables that are shared by multiple threads.
override void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.IO.Pipes.PipeStream class and optionally releas...
SafePipeHandle SafePipeHandle
Gets the safe handle for the local end of the pipe that the current T:System.IO.Pipes....
virtual int OutBufferSize
Gets the size, in bytes, of the outbound buffer for a pipe.
Provides an explicit layout that is visible from unmanaged code and that will have the same layout as...
Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I...
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....