internal async Task Start()
        {
            Address address = UriUtil.ToAddress(remoteUri, Info.username, Info.password);

            underlyingConnection = await transport.CreateAsync(address, CreateOpenFrame(Info), OnOpened);

            underlyingConnection.AddClosedCallback(Provider.OnInternalClosed);

            // Create a Session for this connection that is used for Temporary Destinations
            // and perhaps later on management and advisory monitoring.

            // TODO: change the way how connection session id is obtained
            SessionInfo sessionInfo = new SessionInfo(Info.Id);

            sessionInfo.AcknowledgementMode = AcknowledgementMode.AutoAcknowledge;

            connectionSession = new AmqpConnectionSession(this, sessionInfo);
            await connectionSession.Start();
        }
Example #2
0
        internal async Task Start()
        {
            Address address = UriUtil.ToAddress(remoteUri, Info.UserName, Info.Password);

            this.tsc             = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
            underlyingConnection = await transport.CreateAsync(address, new AmqpHandler(this)).ConfigureAwait(false);

            underlyingConnection.AddClosedCallback(OnClosed);

            // Wait for connection to be opened
            await this.tsc.Task.ConfigureAwait(false);

            // Create a Session for this connection that is used for Temporary Destinations
            // and perhaps later on management and advisory monitoring.
            NmsSessionInfo sessionInfo = new NmsSessionInfo(Info, -1);

            sessionInfo.AcknowledgementMode = AcknowledgementMode.AutoAcknowledge;

            connectionSession = new AmqpConnectionSession(this, sessionInfo);
            await connectionSession.Start().ConfigureAwait(false);
        }
Example #3
0
        internal void Connect()
        {
            if (this.state.CompareAndSet(ConnectionState.INITIAL, ConnectionState.CONNECTING))
            {
                Address addr = UriUtil.ToAddress(connInfo.remoteHost, connInfo.username, connInfo.password ?? string.Empty);
                Tracer.InfoFormat("Creating Address: {0}", addr.Host);
                if (this.clientIdCanSet.CompareAndSet(true, false))
                {
                    if (this.ClientId == null)
                    {
                        connInfo.ResourceId = this.clientIdGenerator.GenerateId();
                    }
                    else
                    {
                        connInfo.ResourceId = new Id(ClientId);
                    }
                    Tracer.InfoFormat("Staring Connection with Client Id : {0}", this.ClientId);
                }

                Open openFrame = CreateOpenFrame(this.connInfo);

                Task <Amqp.Connection> fconn = this.implCreate(addr, openFrame, this.OpenResponse);
                // wait until the Open request is sent
                this.impl = TaskUtil.Wait(fconn, connInfo.connectTimeout);
                if (fconn.Exception != null)
                {
                    // exceptions thrown from TaskUtil are typically System.AggregateException and are usually transport exceptions for secure transport.
                    // extract the innerException of interest and wrap it as an NMS exception
                    if (fconn.Exception is AggregateException)
                    {
                        throw ExceptionSupport.Wrap(fconn.Exception.InnerException,
                                                    "Failed to connect host {0}. Cause: {1}", openFrame.HostName, fconn.Exception.InnerException?.Message ?? fconn.Exception.Message);
                    }
                    else
                    {
                        throw ExceptionSupport.Wrap(fconn.Exception,
                                                    "Failed to connect host {0}. Cause: {1}", openFrame.HostName, fconn.Exception?.Message);
                    }
                }

                this.impl.Closed += OnInternalClosed;
                this.impl.AddClosedCallback(OnInternalClosed);
                this.latch = new CountDownLatch(1);

                ConnectionState finishedState = ConnectionState.UNKNOWN;
                // Wait for Open response
                try
                {
                    bool received = this.latch.await((this.Info.requestTimeout == 0) ? Timeout.InfiniteTimeSpan : this.RequestTimeout);
                    if (received && this.impl.Error == null && fconn.Exception == null)
                    {
                        Tracer.InfoFormat("Connection {0} has connected.", this.impl.ToString());
                        finishedState = ConnectionState.CONNECTED;
                        // register connection factory once client Id accepted.
                        MessageFactory <ConnectionInfo> .Register(this);
                    }
                    else
                    {
                        if (!received)
                        {
                            // Timeout occured waiting on response
                            Tracer.InfoFormat("Connection Response Timeout. Failed to receive response from {0} in {1}ms", addr.Host, this.Info.requestTimeout);
                        }
                        finishedState = ConnectionState.INITIAL;

                        if (fconn.Exception == null)
                        {
                            if (!received)
                            {
                                throw ExceptionSupport.GetTimeoutException(this.impl, "Connection {0} has failed to connect in {1}ms.", ClientId, connInfo.closeTimeout);
                            }
                            Tracer.ErrorFormat("Connection {0} has Failed to connect. Message: {1}", ClientId, (this.impl.Error == null ? "Unknown" : this.impl.Error.ToString()));

                            throw ExceptionSupport.GetException(this.impl, "Connection {0} has failed to connect.", ClientId);
                        }
                        else
                        {
                            throw ExceptionSupport.Wrap(fconn.Exception, "Connection {0} failed to connect.", ClientId);
                        }
                    }
                }
                finally
                {
                    this.latch = null;
                    this.state.GetAndSet(finishedState);
                    if (finishedState != ConnectionState.CONNECTED)
                    {
                        this.impl.Close(TimeSpan.FromMilliseconds(connInfo.closeTimeout), null);
                    }
                }
            }
        }