private void Disconnect() { if (this.state.CompareAndSet(ConnectionState.CONNECTED, ConnectionState.CLOSING) && this.impl != null) { Tracer.InfoFormat("Sending Close Request On Connection {0}.", ClientId); try { if (!this.impl.IsClosed) { this.impl.Close(TimeSpan.FromMilliseconds(connInfo.closeTimeout), null); } } catch (AmqpException amqpEx) { throw ExceptionSupport.Wrap(amqpEx, "Error Closing Amqp Connection " + ClientId); } catch (TimeoutException tmoutEx) { throw ExceptionSupport.GetTimeoutException(this.impl, "Timeout waiting for Amqp Connection {0} Close response. Message : {1}", ClientId, tmoutEx.Message); } finally { if (this.state.CompareAndSet(ConnectionState.CLOSING, ConnectionState.CLOSED)) { // connection cleanup. MessageFactory <ConnectionInfo> .Unregister(this); this.impl = null; } } } }
private void OpenResponse(Amqp.IConnection conn, Open openResp) { Tracer.InfoFormat("Connection {0}, Open {0}", conn.ToString(), openResp.ToString()); Tracer.DebugFormat("Open Response : \n Hostname = {0},\n ContainerId = {1},\n MaxChannel = {2},\n MaxFrame = {3}\n", openResp.HostName, openResp.ContainerId, openResp.ChannelMax, openResp.MaxFrameSize); Tracer.DebugFormat("Open Response Descriptor : \n Descriptor Name = {0},\n Descriptor Code = {1}\n", openResp.Descriptor.Name, openResp.Descriptor.Code); ProcessCapabilities(openResp); ProcessRemoteConnectionProperties(openResp); if (SymbolUtil.CheckAndCompareFields(openResp.Properties, SymbolUtil.CONNECTION_ESTABLISH_FAILED, SymbolUtil.BOOLEAN_TRUE)) { Tracer.InfoFormat("Open response contains {0} property the connection {1} will soon be closed.", SymbolUtil.CONNECTION_ESTABLISH_FAILED, this.ClientId); } else { object value = SymbolUtil.GetFromFields(openResp.Properties, SymbolUtil.CONNECTION_PROPERTY_TOPIC_PREFIX); if (value != null && value is string) { this.connInfo.TopicPrefix = value as string; } value = SymbolUtil.GetFromFields(openResp.Properties, SymbolUtil.CONNECTION_PROPERTY_QUEUE_PREFIX); if (value != null && value is string) { this.connInfo.QueuePrefix = value as string; } this.latch?.countDown(); } }
private void OnOpened(Amqp.IConnection connection, Open open) { if (connection != null) { _tcs.TrySetResult(true); } }
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); } } } }