private Exception ConvertPipeException(PipeException pipeException, TransferOperation transferOperation) { return this.ConvertPipeException(pipeException.Message, pipeException, transferOperation); }
private Exception ConvertPipeException(string exceptionMessage, PipeException pipeException, TransferOperation transferOperation) { if (this.timeoutErrorString != null) { if (transferOperation == this.timeoutErrorTransferOperation) { return new TimeoutException(this.timeoutErrorString, pipeException); } return new CommunicationException(this.timeoutErrorString, pipeException); } if (this.aborted) { return new CommunicationObjectAbortedException(exceptionMessage, pipeException); } return new CommunicationException(exceptionMessage, pipeException); }
CommunicationException CreatePipeDuplicationFailedException(int win32Error) { Exception innerException = new PipeException(SR.GetString(SR.PipeDuplicationFailed), win32Error); return new CommunicationException(innerException.Message, innerException); }
public unsafe IAsyncResult BeginWrite(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, AsyncCallback callback, object state) { IAsyncResult result2; TimeoutHelper helper = new TimeoutHelper(timeout); this.FinishPendingWrite(timeout); ConnectionUtilities.ValidateBufferBounds(buffer, offset, size); if (this.autoBindToCompletionPort && !this.isBoundToCompletionPort) { lock (this.readLock) { lock (this.writeLock) { this.ValidateEnterWritingState(true); this.EnsureBoundToCompletionPort(); } } } lock (this.writeLock) { try { this.ValidateEnterWritingState(true); if (this.isWriteOutstanding) { throw Fx.AssertAndThrow("Write I/O already pending when BeginWrite called."); } WriteAsyncResult result = new WriteAsyncResult(callback, state, size); try { this.writeTimeout = timeout; this.WriteTimer.Set(helper.RemainingTime()); this.writeAsyncResult = result; this.isWriteOutstanding = true; this.writeOverlapped.StartAsyncOperation(buffer, this.onAsyncWriteComplete, this.isBoundToCompletionPort); if (UnsafeNativeMethods.WriteFile(this.pipe.DangerousGetHandle(), this.writeOverlapped.BufferPtr + offset, size, IntPtr.Zero, this.writeOverlapped.NativeOverlapped) == 0) { int error = Marshal.GetLastWin32Error(); if (error != 0x3e5) { this.isWriteOutstanding = false; throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Exceptions.CreateWriteException(error)); } } } finally { if (!this.isWriteOutstanding) { this.writeOverlapped.CancelAsyncOperation(); this.writeAsyncResult = null; this.WriteTimer.Cancel(); } } if (!this.isWriteOutstanding) { int num2; Exception exception = Exceptions.GetOverlappedWriteException(this.pipe, this.writeOverlapped.NativeOverlapped, out num2); if ((exception == null) && (num2 != size)) { exception = new PipeException(System.ServiceModel.SR.GetString("PipeWriteIncomplete")); } if (exception != null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception); } result.Complete(true); } else { this.EnterWritingState(); } result2 = result; } catch (PipeException exception2) { throw DiagnosticUtility.ExceptionUtility.ThrowHelper(this.ConvertPipeException(exception2, TransferOperation.Write), this.ExceptionEventType); } } return result2; }
public static unsafe bool TryCreate(List<SecurityIdentifier> allowedSids, Uri pipeUri, string sharedMemoryName, out PipeSharedMemory result) { byte[] buffer; SafeFileMappingHandle handle; int num; bool flag2; Guid guid = Guid.NewGuid(); string pipeName = BuildPipeName(guid); try { buffer = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, -2147483648); } catch (Win32Exception exception) { Exception innerException = new PipeException(exception.Message, exception); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException)); } result = null; fixed (byte* numRef = buffer) { UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES { lpSecurityDescriptor = numRef }; handle = UnsafeNativeMethods.CreateFileMapping((IntPtr) (-1), securityAttributes, 4, 0, sizeof(SharedMemoryContents), sharedMemoryName); num = Marshal.GetLastWin32Error(); } if (handle.IsInvalid) { handle.SetHandleAsInvalid(); if (num == 5) { return false; } Exception exception3 = new PipeException(System.ServiceModel.SR.GetString("PipeNameCantBeReserved", new object[] { pipeUri.AbsoluteUri, PipeError.GetErrorString(num) }), num); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(exception3.Message, exception3)); } if (num == 0xb7) { handle.Close(); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreatePipeNameInUseException(num, pipeUri)); } PipeSharedMemory memory = new PipeSharedMemory(handle, pipeUri, pipeName); bool flag = true; try { memory.InitializeContents(guid); flag = false; result = memory; flag2 = true; } finally { if (flag) { memory.Dispose(); } } return flag2; }
private Exception CreateConnectFailedException(Uri remoteUri, PipeException innerException) { return new CommunicationException(System.ServiceModel.SR.GetString("PipeConnectFailed", new object[] { remoteUri.AbsoluteUri }), innerException); }
public unsafe static bool TryCreate(List<SecurityIdentifier> allowedSids, Uri pipeUri, string sharedMemoryName, out PipeSharedMemory result) { Guid pipeGuid = Guid.NewGuid(); string pipeName = BuildPipeName(pipeGuid.ToString()); byte[] binarySecurityDescriptor; try { binarySecurityDescriptor = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, UnsafeNativeMethods.GENERIC_READ); } catch (Win32Exception e) { // While Win32exceptions are not expected, if they do occur we need to obey the pipe/communication exception model. Exception innerException = new PipeException(e.Message, e); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException)); } SafeFileMappingHandle fileMapping; int error; result = null; fixed (byte* pinnedSecurityDescriptor = binarySecurityDescriptor) { UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES(); securityAttributes.lpSecurityDescriptor = (IntPtr)pinnedSecurityDescriptor; fileMapping = UnsafeNativeMethods.CreateFileMapping((IntPtr)(-1), securityAttributes, UnsafeNativeMethods.PAGE_READWRITE, 0, sizeof(SharedMemoryContents), sharedMemoryName); error = Marshal.GetLastWin32Error(); } if (fileMapping.IsInvalid) { fileMapping.SetHandleAsInvalid(); if (error == UnsafeNativeMethods.ERROR_ACCESS_DENIED) { return false; } else { Exception innerException = new PipeException(SR.GetString(SR.PipeNameCantBeReserved, pipeUri.AbsoluteUri, PipeError.GetErrorString(error)), error); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(innerException.Message, innerException)); } } // now we have a valid file mapping handle if (error == UnsafeNativeMethods.ERROR_ALREADY_EXISTS) { fileMapping.Close(); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreatePipeNameInUseException(error, pipeUri)); } PipeSharedMemory pipeSharedMemory = new PipeSharedMemory(fileMapping, pipeUri, pipeName); bool disposeSharedMemory = true; try { pipeSharedMemory.InitializeContents(pipeGuid); disposeSharedMemory = false; result = pipeSharedMemory; if (TD.PipeSharedMemoryCreatedIsEnabled()) { TD.PipeSharedMemoryCreated(sharedMemoryName); } return true; } finally { if (disposeSharedMemory) { pipeSharedMemory.Dispose(); } } }
private Exception CreatePipeAcceptFailedException(int errorCode) { Exception innerException = new PipeException(System.ServiceModel.SR.GetString("PipeAcceptFailed", new object[] { PipeError.GetErrorString(errorCode) }), errorCode); return new CommunicationException(innerException.Message, innerException); }
unsafe PipeHandle CreatePipe() { int openMode = UnsafeNativeMethods.PIPE_ACCESS_DUPLEX | UnsafeNativeMethods.FILE_FLAG_OVERLAPPED; if (!anyPipesCreated) { openMode |= UnsafeNativeMethods.FILE_FLAG_FIRST_PIPE_INSTANCE; } byte[] binarySecurityDescriptor; try { binarySecurityDescriptor = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, UnsafeNativeMethods.GENERIC_READ | UnsafeNativeMethods.GENERIC_WRITE); } catch (Win32Exception e) { // While Win32exceptions are not expected, if they do occur we need to obey the pipe/communication exception model. Exception innerException = new PipeException(e.Message, e); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException)); } PipeHandle pipeHandle; int error; string pipeName = null; fixed (byte* pinnedSecurityDescriptor = binarySecurityDescriptor) { UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES(); securityAttributes.lpSecurityDescriptor = (IntPtr)pinnedSecurityDescriptor; pipeName = this.sharedMemory.PipeName; pipeHandle = UnsafeNativeMethods.CreateNamedPipe( pipeName, openMode, UnsafeNativeMethods.PIPE_TYPE_MESSAGE | UnsafeNativeMethods.PIPE_READMODE_MESSAGE, maxInstances, bufferSize, bufferSize, 0, securityAttributes); error = Marshal.GetLastWin32Error(); } if (pipeHandle.IsInvalid) { pipeHandle.SetHandleAsInvalid(); Exception innerException = new PipeException(SR.GetString(SR.PipeListenFailed, pipeUri.AbsoluteUri, PipeError.GetErrorString(error)), error); if (error == UnsafeNativeMethods.ERROR_ACCESS_DENIED) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(innerException.Message, innerException)); } else if (error == UnsafeNativeMethods.ERROR_ALREADY_EXISTS) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAlreadyInUseException(innerException.Message, innerException)); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException)); } } else { if (TD.NamedPipeCreatedIsEnabled()) { TD.NamedPipeCreated(pipeName); } } bool closePipe = true; try { if (useCompletionPort) { ThreadPool.BindHandle(pipeHandle); } anyPipesCreated = true; closePipe = false; return pipeHandle; } finally { if (closePipe) { pipeHandle.Close(); } } }
Exception CreatePipeAcceptFailedException(int errorCode) { Exception innerException = new PipeException(SR.GetString(SR.PipeAcceptFailed, PipeError.GetErrorString(errorCode)), errorCode); return new CommunicationException(innerException.Message, innerException); }
IConnection TryConnect(Uri remoteUri, string resolvedAddress, BackoffTimeoutHelper backoffHelper) { const int access = UnsafeNativeMethods.GENERIC_READ | UnsafeNativeMethods.GENERIC_WRITE; bool lastAttempt = backoffHelper.IsExpired(); int flags = UnsafeNativeMethods.FILE_FLAG_OVERLAPPED; // By default Windows named pipe connection is created with impersonation, but we want // to create it with anonymous and let WCF take care of impersonation/identification. flags |= UnsafeNativeMethods.SECURITY_QOS_PRESENT | UnsafeNativeMethods.SECURITY_ANONYMOUS; PipeHandle pipeHandle = UnsafeNativeMethods.CreateFile(resolvedAddress, access, 0, IntPtr.Zero, UnsafeNativeMethods.OPEN_EXISTING, flags, IntPtr.Zero); int error = Marshal.GetLastWin32Error(); if (pipeHandle.IsInvalid) { pipeHandle.SetHandleAsInvalid(); } else { int mode = UnsafeNativeMethods.PIPE_READMODE_MESSAGE; if (UnsafeNativeMethods.SetNamedPipeHandleState(pipeHandle, ref mode, IntPtr.Zero, IntPtr.Zero) == 0) { error = Marshal.GetLastWin32Error(); pipeHandle.Close(); PipeException innerException = new PipeException(SR.GetString(SR.PipeModeChangeFailed, PipeError.GetErrorString(error)), error); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( CreateConnectFailedException(remoteUri, innerException)); } return new PipeConnection(pipeHandle, bufferSize, false, true); } if (error == UnsafeNativeMethods.ERROR_FILE_NOT_FOUND || error == UnsafeNativeMethods.ERROR_PIPE_BUSY) { if (lastAttempt) { Exception innerException = new PipeException(SR.GetString(SR.PipeConnectAddressFailed, resolvedAddress, PipeError.GetErrorString(error)), error); TimeoutException timeoutException; string endpoint = remoteUri.AbsoluteUri; if (error == UnsafeNativeMethods.ERROR_PIPE_BUSY) { timeoutException = new TimeoutException(SR.GetString(SR.PipeConnectTimedOutServerTooBusy, endpoint, backoffHelper.OriginalTimeout), innerException); } else { timeoutException = new TimeoutException(SR.GetString(SR.PipeConnectTimedOut, endpoint, backoffHelper.OriginalTimeout), innerException); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(timeoutException); } return null; } else { PipeException innerException = new PipeException(SR.GetString(SR.PipeConnectAddressFailed, resolvedAddress, PipeError.GetErrorString(error)), error); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( CreateConnectFailedException(remoteUri, innerException)); } }
Exception CreateConnectFailedException(Uri remoteUri, PipeException innerException) { return new CommunicationException( SR.GetString(SR.PipeConnectFailed, remoteUri.AbsoluteUri), innerException); }
unsafe void FinishSyncWrite(bool traceExceptionsAsErrors) { int bytesWritten; Exception writeException; if (this.closeState == CloseState.HandleClosed) { writeException = CreatePipeClosedException(TransferOperation.Write); } else { writeException = Exceptions.GetOverlappedWriteException(this.pipe, this.writeOverlapped.NativeOverlapped, out bytesWritten); if (writeException == null && bytesWritten != this.syncWriteSize) { writeException = new PipeException(SR.GetString(SR.PipeWriteIncomplete)); } } if (writeException != null) { TraceEventType traceEventType = TraceEventType.Information; if (traceExceptionsAsErrors) { traceEventType = TraceEventType.Error; } throw DiagnosticUtility.ExceptionUtility.ThrowHelper(writeException, traceEventType); } }
private CommunicationException CreatePipeDuplicationFailedException(int win32Error) { Exception innerException = new PipeException(System.ServiceModel.SR.GetString("PipeDuplicationFailed"), win32Error); return new CommunicationException(innerException.Message, innerException); }
public static Exception CreatePipeNameInUseException(int error, Uri pipeUri) { Exception innerException = new PipeException(SR.GetString(SR.PipeNameInUse, pipeUri.AbsoluteUri), error); return new AddressAlreadyInUseException(innerException.Message, innerException); }
private unsafe void FinishSyncWrite(bool traceExceptionsAsErrors) { Exception exception; if (this.closeState == CloseState.HandleClosed) { exception = this.CreatePipeClosedException(TransferOperation.Write); } else { int num; exception = Exceptions.GetOverlappedWriteException(this.pipe, this.writeOverlapped.NativeOverlapped, out num); if ((exception == null) && (num != this.syncWriteSize)) { exception = new PipeException(System.ServiceModel.SR.GetString("PipeWriteIncomplete")); } } if (exception != null) { TraceEventType information = TraceEventType.Information; if (traceExceptionsAsErrors) { information = TraceEventType.Error; } throw DiagnosticUtility.ExceptionUtility.ThrowHelper(exception, information); } }
static Exception CreatePipeNameCannotBeAccessedException(int error, Uri pipeUri) { Exception innerException = new PipeException(SR.GetString(SR.PipeNameCanNotBeAccessed, PipeError.GetErrorString(error)), error); return new AddressAccessDeniedException(SR.GetString(SR.PipeNameCanNotBeAccessed2, pipeUri.AbsoluteUri), innerException); }
private unsafe PipeHandle CreatePipe() { byte[] buffer; PipeHandle handle; int num2; PipeHandle handle2; int openMode = 0x40000003; if (!this.anyPipesCreated) { openMode |= 0x80000; } try { buffer = SecurityDescriptorHelper.FromSecurityIdentifiers(this.allowedSids, -1073741824); } catch (Win32Exception exception) { Exception innerException = new PipeException(exception.Message, exception); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException)); } fixed (byte* numRef = buffer) { UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES { lpSecurityDescriptor = numRef }; handle = UnsafeNativeMethods.CreateNamedPipe(this.sharedMemory.PipeName, openMode, 6, this.maxInstances, this.bufferSize, this.bufferSize, 0, securityAttributes); num2 = Marshal.GetLastWin32Error(); } if (handle.IsInvalid) { handle.SetHandleAsInvalid(); Exception exception3 = new PipeException(System.ServiceModel.SR.GetString("PipeListenFailed", new object[] { this.pipeUri.AbsoluteUri, PipeError.GetErrorString(num2) }), num2); switch (num2) { case 5: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(exception3.Message, exception3)); case 0xb7: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAlreadyInUseException(exception3.Message, exception3)); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(exception3.Message, exception3)); } bool flag = true; try { if (this.useCompletionPort) { ThreadPool.BindHandle(handle); } this.anyPipesCreated = true; flag = false; handle2 = handle; } finally { if (flag) { handle.Close(); } } return handle2; }
public unsafe AsyncCompletionResult BeginWrite(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, WaitCallback callback, object state) { TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); FinishPendingWrite(timeout); ConnectionUtilities.ValidateBufferBounds(buffer, offset, size); if (autoBindToCompletionPort && !isBoundToCompletionPort) { // Locks must be both taken, and in this order. lock (readLock) { lock (writeLock) { ValidateEnterWritingState(true); EnsureBoundToCompletionPort(); } } } lock (writeLock) { try { ValidateEnterWritingState(true); if (this.isWriteOutstanding) { throw Fx.AssertAndThrow("Write I/O already pending when BeginWrite called."); } try { this.writeTimeout = timeout; this.WriteTimer.Set(timeoutHelper.RemainingTime()); this.asyncBytesToWrite = size; this.asyncWriteException = null; this.asyncWriteCallback = callback; this.asyncWriteCallbackState = state; this.isWriteOutstanding = true; this.writeOverlapped.StartAsyncOperation(buffer, this.onAsyncWriteComplete, this.isBoundToCompletionPort); if (UnsafeNativeMethods.WriteFile(this.pipe.DangerousGetHandle(), this.writeOverlapped.BufferPtr + offset, size, IntPtr.Zero, this.writeOverlapped.NativeOverlapped) == 0) { int error = Marshal.GetLastWin32Error(); if (error != UnsafeNativeMethods.ERROR_IO_PENDING) { this.isWriteOutstanding = false; throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Exceptions.CreateWriteException(error)); } } } finally { if (!this.isWriteOutstanding) { // Unbind the buffer. this.writeOverlapped.CancelAsyncOperation(); this.ResetWriteState(); this.WriteTimer.Cancel(); } } if (!this.isWriteOutstanding) { int bytesWritten; Exception writeException = Exceptions.GetOverlappedWriteException(this.pipe, this.writeOverlapped.NativeOverlapped, out bytesWritten); if (writeException == null && bytesWritten != size) { writeException = new PipeException(SR.GetString(SR.PipeWriteIncomplete)); } if (writeException != null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(writeException); } } else { EnterWritingState(); } return this.isWriteOutstanding ? AsyncCompletionResult.Queued : AsyncCompletionResult.Completed; } catch (PipeException e) { throw DiagnosticUtility.ExceptionUtility.ThrowHelper(ConvertPipeException(e, TransferOperation.Write), ExceptionEventType); } } }
public static Exception CreatePipeNameInUseException(int error, Uri pipeUri) { Exception innerException = new PipeException(System.ServiceModel.SR.GetString("PipeNameInUse", new object[] { pipeUri.AbsoluteUri }), error); return new AddressAlreadyInUseException(innerException.Message, innerException); }
private IConnection TryConnect(Uri remoteUri, string resolvedAddress, BackoffTimeoutHelper backoffHelper) { bool flag = backoffHelper.IsExpired(); int dwFlagsAndAttributes = 0x40000000; if (this.includeSecurityIdentity) { dwFlagsAndAttributes |= 0x110000; } PipeHandle handle = UnsafeNativeMethods.CreateFile(resolvedAddress, -1073741824, 0, IntPtr.Zero, 3, dwFlagsAndAttributes, IntPtr.Zero); int errorCode = Marshal.GetLastWin32Error(); if (handle.IsInvalid) { handle.SetHandleAsInvalid(); } else { int mode = 2; if (UnsafeNativeMethods.SetNamedPipeHandleState(handle, ref mode, IntPtr.Zero, IntPtr.Zero) == 0) { errorCode = Marshal.GetLastWin32Error(); handle.Close(); PipeException exception = new PipeException(System.ServiceModel.SR.GetString("PipeModeChangeFailed", new object[] { PipeError.GetErrorString(errorCode) }), errorCode); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.CreateConnectFailedException(remoteUri, exception)); } return new PipeConnection(handle, this.bufferSize, false, true); } if ((errorCode == 2) || (errorCode == 0xe7)) { TimeoutException exception3; if (!flag) { return null; } Exception exception2 = new PipeException(System.ServiceModel.SR.GetString("PipeConnectAddressFailed", new object[] { resolvedAddress, PipeError.GetErrorString(errorCode) }), errorCode); string absoluteUri = remoteUri.AbsoluteUri; if (errorCode == 0xe7) { exception3 = new TimeoutException(System.ServiceModel.SR.GetString("PipeConnectTimedOutServerTooBusy", new object[] { absoluteUri, backoffHelper.OriginalTimeout }), exception2); } else { exception3 = new TimeoutException(System.ServiceModel.SR.GetString("PipeConnectTimedOut", new object[] { absoluteUri, backoffHelper.OriginalTimeout }), exception2); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception3); } PipeException innerException = new PipeException(System.ServiceModel.SR.GetString("PipeConnectAddressFailed", new object[] { resolvedAddress, PipeError.GetErrorString(errorCode) }), errorCode); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.CreateConnectFailedException(remoteUri, innerException)); }