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);
        }
            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));
            }
Exemple #3
0
 private static Exception CreatePipeNameCannotBeAccessedException(int error, Uri pipeUri)
 {
     return(new AddressAccessDeniedException(System.ServiceModel.SR.GetString("PipeNameCanNotBeAccessed2", new object[] { pipeUri.AbsoluteUri }), new PipeException(System.ServiceModel.SR.GetString("PipeNameCanNotBeAccessed", new object[] { PipeError.GetErrorString(error) }), error)));
 }
Exemple #4
0
        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));
        }
Exemple #5
0
        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);
        }