示例#1
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));
        }
示例#2
0
 void ReportBroken(PipeException exception = null)
 {
     try
     {
         if (null != OnBroken)
         {
             OnBroken(this, new PipeEventArgs()
             {
                 Pipe = this
             });
         }
     }
     catch (Exception ex)
     {
         _logger?.LogError(ex, "DefaultPipe ReportBroken error.");
     }
 }
示例#3
0
 void ReportBroken(PipeBrokenCause cause, PipeException exception = null)
 {
     try
     {
         if (null != OnBroken)
         {
             OnBroken(this, new PipeBrokenEventArgs()
             {
                 Pipe      = this,
                 Cause     = cause,
                 Exception = exception
             });
         }
     }
     catch (Exception ex)
     {
         _logger?.LogError(ex, "Pipe ReportBroken error.");
     }
 }
        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));
            }
示例#6
0
 /// <summary>
 /// Creates a new failure pipe response.
 /// </summary>
 /// <param name="callId">The ID of the call.</param>
 /// <param name="message">The failure message.</param>
 /// <returns>The failure pipe response.</returns>
 public static PipeResponse Failure(long callId, string message, Exception ex = null)
 {
     return(new PipeResponse {
         Succeeded = false, CallId = callId, Error = message, Exception = PipeException.Failure(ex.InnerException)
     });
 }
示例#7
0
 private Exception CreateConnectFailedException(Uri remoteUri, PipeException innerException)
 {
     return(new CommunicationException(System.ServiceModel.SR.GetString("PipeConnectFailed", new object[] { remoteUri.AbsoluteUri }), innerException));
 }
示例#8
0
        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));
        }
示例#9
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);
        }