Esempio n. 1
0
            public override Task ConnectAsync(EndPoint remoteAddress, EndPoint localAddress)
            {
                var promise = new TaskCompletionSource();

                if (Volatile.Read(ref _channel.v_state) == State.Connected)
                {
                    var cause = new AlreadyConnectedException();
                    Util.SafeSetFailure(promise, cause, Logger);
                    _ = _channel.Pipeline.FireExceptionCaught(cause);
                    return(promise.Task);
                }

                if (Volatile.Read(ref _channel.v_connectPromise) is object)
                {
                    ThrowHelper.ThrowConnectionPendingException();
                }

                _ = Interlocked.Exchange(ref _channel.v_connectPromise, promise);

                if (Volatile.Read(ref _channel.v_state) != State.Bound)
                {
                    // Not bound yet and no localAddress specified - get one.
                    if (localAddress is null)
                    {
                        localAddress = new LocalAddress(_channel);
                    }
                }

                if (localAddress is object)
                {
                    try
                    {
                        _channel.DoBind(localAddress);
                    }
                    catch (Exception ex)
                    {
                        Util.SafeSetFailure(promise, ex, Logger);
                        _ = _channel.CloseAsync();
                        return(promise.Task);
                    }
                }

                IChannel boundChannel = LocalChannelRegistry.Get(remoteAddress);

                if (!(boundChannel is LocalServerChannel serverChannel))
                {
                    Exception cause = new ConnectException($"connection refused: {remoteAddress}", null);
                    Util.SafeSetFailure(promise, cause, Logger);
                    _ = _channel.CloseAsync();
                    return(promise.Task);
                }

                _ = Interlocked.Exchange(ref _channel.v_peer, serverChannel.Serve(_channel));
                return(promise.Task);
            }
            public override Task ConnectAsync(EndPoint remoteAddress, EndPoint localAddress)
            {
                var promise = new TaskCompletionSource();

                if (this.localChannel.state == State.Connected)
                {
                    var cause = new AlreadyConnectedException();
                    Util.SafeSetFailure(promise, cause, Logger);
                    this.localChannel.Pipeline.FireExceptionCaught(cause);
                    return(promise.Task);
                }

                if (this.localChannel.connectPromise != null)
                {
                    throw new ConnectionPendingException();
                }

                this.localChannel.connectPromise = promise;

                if (this.localChannel.state != State.Bound)
                {
                    // Not bound yet and no localAddress specified - get one.
                    if (localAddress == null)
                    {
                        localAddress = new LocalAddress(this.localChannel);
                    }
                }

                if (localAddress != null)
                {
                    try
                    {
                        this.localChannel.DoBind(localAddress);
                    }
                    catch (Exception ex)
                    {
                        Util.SafeSetFailure(promise, ex, Logger);
                        this.channel.CloseAsync();
                        return(promise.Task);
                    }
                }

                IChannel boundChannel = LocalChannelRegistry.Get(remoteAddress);

                if (!(boundChannel is LocalServerChannel))
                {
                    Exception cause = new ConnectException($"connection refused: {remoteAddress}", null);
                    Util.SafeSetFailure(promise, cause, Logger);
                    this.localChannel.CloseAsync();
                    return(promise.Task);
                }

                this.localChannel.peer = ((LocalServerChannel)boundChannel).Serve(this.localChannel);
                return(promise.Task);
            }
Esempio n. 3
0
        private static Exception HandleConnectException(Address remoteAddress, ConnectException cause, AggregateException e)
        {
            var socketException = cause?.InnerException as SocketException;

            if (socketException?.SocketErrorCode == SocketError.ConnectionRefused)
            {
                return(new InvalidAssociationException(socketException.Message + " " + remoteAddress));
            }

            return(new InvalidAssociationException("Failed to associate with " + remoteAddress, e ?? (Exception)cause));
        }
Esempio n. 4
0
        public virtual void TestWrapConnectException()
        {
            IOException e       = new ConnectException("failed");
            IOException wrapped = VerifyExceptionClass(e, typeof(ConnectException));

            AssertInException(wrapped, "failed");
            AssertWikified(wrapped);
            AssertInException(wrapped, "localhost");
            AssertRemoteDetailsIncluded(wrapped);
            AssertInException(wrapped, "/ConnectionRefused");
        }
Esempio n. 5
0
 /// <summary>Determine the proxy server (if any) needed to obtain a URL.</summary>
 /// <remarks>Determine the proxy server (if any) needed to obtain a URL.</remarks>
 /// <param name="proxySelector">proxy support for the caller.</param>
 /// <param name="u">location of the server caller wants to talk to.</param>
 /// <returns>proxy to communicate with the supplied URL.</returns>
 /// <exception cref="Sharpen.ConnectException">
 /// the proxy could not be computed as the supplied URL could not
 /// be read. This failure should never occur.
 /// </exception>
 public static Proxy ProxyFor(ProxySelector proxySelector, Uri u)
 {
     try
     {
         return(proxySelector.Select(u.ToURI())[0]);
     }
     catch (URISyntaxException e)
     {
         ConnectException err;
         err = new ConnectException(MessageFormat.Format(JGitText.Get().cannotDetermineProxyFor
                                                         , u));
         Sharpen.Extensions.InitCause(err, e);
         throw err;
     }
 }
        public async Task TestCall_secureCallerOnNonListeningServerAndNoPortSpecifiedAsync()
        {
            ConnectException expectedException = new ConnectException();

            Mock.Get(mockConnection).Setup(m => m.SendAsync(It.IsAny <HttpRequestMessage>()))
            .Throws(expectedException);     // server is not listening on 443

            try
            {
                await secureEndpointCaller.CallAsync().ConfigureAwait(false);

                Assert.Fail("Should not fall back to HTTP if not allowInsecureRegistries");
            }
            catch (ConnectException ex)
            {
                Assert.AreEqual(expectedException, ex);
            }

            Mock.Get(mockConnectionFactory).Verify(m => m(new Uri("https://apiroutebase/api")));
        }
        public async Task TestCall_insecureCallerOnNonListeningServerAndPortSpecifiedAsync()
        {
            ConnectException expectedException = new ConnectException();

            Mock.Get(mockConnection).Setup(m => m.SendAsync(It.IsAny <HttpRequestMessage>()))
            .Throws(expectedException);     // server is not listening on 5000

            RegistryEndpointCaller <string> insecureEndpointCaller =
                CreateRegistryEndpointCaller(true, 5000);

            try
            {
                await insecureEndpointCaller.CallAsync().ConfigureAwait(false);

                Assert.Fail("Should not fall back to HTTP if port was explicitly given and cannot connect");
            }
            catch (ConnectException ex)
            {
                Assert.AreEqual(expectedException, ex);
            }
        }
 public HttpHostConnectException(HttpHost host, ConnectException cause) : this(cause
                                                                               , host, null)
 {
 }