Beispiel #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));
        }
        public void Listen()
        {
            BackoffTimeoutHelper helper = new BackoffTimeoutHelper(TimeSpan.FromSeconds(1.0));

            lock (this.ThisLock)
            {
                if (this.listenSocket != null)
                {
                    this.listenSocket.Listen(this.settings.ListenBacklog);
                    this.isListening = true;
                }
                while (!this.isListening)
                {
                    try
                    {
                        this.listenSocket = new Socket(this.localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                        if ((this.localEndpoint.AddressFamily == AddressFamily.InterNetworkV6) && this.settings.TeredoEnabled)
                        {
                            this.listenSocket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPProtectionLevel, 10);
                        }
                        this.listenSocket.Bind(this.localEndpoint);
                        this.listenSocket.Listen(this.settings.ListenBacklog);
                        this.isListening = true;
                        continue;
                    }
                    catch (SocketException exception)
                    {
                        bool flag = false;
                        if ((exception.ErrorCode == 0x2740) && !helper.IsExpired())
                        {
                            helper.WaitAndBackoff();
                            flag = true;
                        }
                        if (!flag)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertListenException(exception, this.localEndpoint));
                        }
                        continue;
                    }
                }
                this.socketAsyncEventArgsPool = new SocketAsyncEventArgsPool(GetAcceptBufferSize(this.listenSocket));
            }
        }