Exemplo n.º 1
0
        public AmqpSettings Clone()
        {
            AmqpSettings settings = new AmqpSettings();

            settings.DefaultLinkCredit        = this.DefaultLinkCredit;
            settings.transportProviders       = new List <TransportProvider>(this.TransportProviders);
            settings.RuntimeProvider          = this.RuntimeProvider;
            settings.RequireSecureTransport   = this.RequireSecureTransport;
            settings.AllowAnonymousConnection = this.AllowAnonymousConnection;
            return(settings);
        }
        /// <summary>
        /// Initializes a listener.
        /// </summary>
        /// <param name="addresses">The addresses to listen on.</param>
        /// <param name="settings">The protocol settings of the listener.</param>
        /// <param name="connectionSettings">The connection settings applied to accepted connections.</param>
        public AmqpConnectionListener(IEnumerable <string> addresses, AmqpSettings settings, AmqpConnectionSettings connectionSettings)
        {
            if (settings.RuntimeProvider == null)
            {
                throw new ArgumentNullException("IRuntimeProvider");
            }

            this.runtime            = settings.RuntimeProvider;
            this.connectionSettings = connectionSettings;
            this.connections        = new HashSet <AmqpConnection>();
            this.onConnectionClosed = this.OnConnectionClosed;
            this.listener           = CreateListener(addresses, settings);
        }
        static AmqpTransportListener CreateListener(IEnumerable <string> addresses, AmqpSettings settings)
        {
            List <TransportListener> listeners = new List <TransportListener>();

            foreach (string address in addresses)
            {
                Uri  uri     = new Uri(address);
                bool isAmqps = string.Equals(AmqpConstants.SchemeAmqps, uri.Scheme, StringComparison.OrdinalIgnoreCase);
                TcpTransportSettings tcpSettings = new TcpTransportSettings()
                {
                    Host = uri.Host,
                    Port = uri.Port < 0 ? (isAmqps ? AmqpConstants.DefaultSecurePort : AmqpConstants.DefaultPort) : uri.Port
                };

                TransportListener tpListener;
                if (isAmqps)
                {
                    var tlsProvider = settings.GetTransportProvider <TlsTransportProvider>();
                    if (tlsProvider == null)
                    {
                        throw new InvalidOperationException($"TlsTransportProvider must be in AmqpSettings.TransportProviders for '{address}'.");
                    }

                    if (tlsProvider.Settings.Certificate == null)
                    {
                        throw new InvalidOperationException($"A server certificate is required in TlsTransportProvider for '{address}'.");
                    }

                    TlsTransportSettings tlsSettings = new TlsTransportSettings(tcpSettings, false);
                    tlsSettings.Certificate = tlsProvider.Settings.Certificate;
                    tlsSettings.CertificateValidationCallback = tlsProvider.Settings.CertificateValidationCallback;
                    tlsSettings.CheckCertificateRevocation    = tlsProvider.Settings.CheckCertificateRevocation;

                    tpListener = tlsSettings.CreateListener();
                }
                else
                {
                    tpListener = tcpSettings.CreateListener();
                }

                listeners.Add(tpListener);
            }

            if (settings.GetTransportProvider <AmqpTransportProvider>() == null)
            {
                var amqpProvider = new AmqpTransportProvider(AmqpVersion.V100);
                settings.TransportProviders.Add(amqpProvider);
            }

            return(new AmqpTransportListener(listeners, settings));
        }
Exemplo n.º 4
0
        public AmqpConnection(TransportBase transport, ProtocolHeader protocolHeader, bool isInitiator, AmqpSettings amqpSettings, AmqpConnectionSettings connectionSettings) :
            base((isInitiator ? "out" : "in") + "-connection", transport, connectionSettings, isInitiator)
        {
            if (amqpSettings == null)
            {
                throw new ArgumentNullException("amqpSettings");
            }

            this.initialHeader          = protocolHeader;
            this.isInitiator            = isInitiator;
            this.amqpSettings           = amqpSettings;
            this.sessionsByLocalHandle  = new HandleTable <AmqpSession>(this.Settings.ChannelMax ?? AmqpConstants.DefaultMaxConcurrentChannels - 1);
            this.sessionsByRemoteHandle = new HandleTable <AmqpSession>(this.Settings.ChannelMax ?? AmqpConstants.DefaultMaxConcurrentChannels - 1);
            this.SessionFactory         = this;
            this.heartBeat = HeartBeat.None;
        }
Exemplo n.º 5
0
 public AmqpConnection(TransportBase transport, ProtocolHeader protocolHeader, AmqpSettings amqpSettings, AmqpConnectionSettings connectionSettings) :
     this(transport, protocolHeader, true, amqpSettings, connectionSettings)
 {
 }
Exemplo n.º 6
0
 public AmqpConnection(TransportBase transport, AmqpSettings amqpSettings, AmqpConnectionSettings connectionSettings) :
     this(transport, amqpSettings.GetDefaultHeader(), true, amqpSettings, connectionSettings)
 {
 }
Exemplo n.º 7
0
 public AmqpConnection(TransportBase transport, ProtocolHeader protocolHeader, bool isInitiator, AmqpSettings amqpSettings, AmqpConnectionSettings connectionSettings) :
     this((isInitiator ? "out" : "in") + "-connection", transport, protocolHeader, isInitiator, amqpSettings, connectionSettings)
 {
 }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes a factory with protocol settings.
 /// </summary>
 /// <param name="settings"></param>
 public AmqpConnectionFactory(AmqpSettings settings)
 {
     this.settings = settings;
 }
Exemplo n.º 9
0
        /// <summary>
        /// Opens a connection to the specified address.
        /// </summary>
        /// <param name="addressUri">The address Uri. User info is ignored.</param>
        /// <param name="saslHandler">The SASL handler which determines the SASL mechanism. Null means no SASL handshake.</param>
        /// <param name="timeout">The operation timeout.</param>
        /// <returns>An AMQP connection.</returns>
        public async Task <AmqpConnection> OpenConnectionAsync(Uri addressUri, SaslHandler saslHandler, TimeSpan timeout)
        {
            TransportSettings transportSettings;

            if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqp, StringComparison.OrdinalIgnoreCase))
            {
                transportSettings = new TcpTransportSettings()
                {
                    Host = addressUri.Host,
                    Port = addressUri.Port > -1 ? addressUri.Port : AmqpConstants.DefaultPort
                };
            }
            else if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqps, StringComparison.OrdinalIgnoreCase))
            {
                TcpTransportSettings tcpSettings = new TcpTransportSettings()
                {
                    Host = addressUri.Host,
                    Port = addressUri.Port > -1 ? addressUri.Port : AmqpConstants.DefaultSecurePort
                };

                var tls = new TlsTransportSettings(tcpSettings)
                {
                    TargetHost = addressUri.Host
                };
                TlsTransportProvider tlsProvider = this.settings.GetTransportProvider <TlsTransportProvider>();
                if (tlsProvider != null)
                {
                    tls.CertificateValidationCallback = tlsProvider.Settings.CertificateValidationCallback;
                    tls.CheckCertificateRevocation    = tlsProvider.Settings.CheckCertificateRevocation;
                    tls.Certificate = tlsProvider.Settings.Certificate;
                    tls.Protocols   = tlsProvider.Settings.Protocols;
                }

                transportSettings = tls;
            }
            else if (addressUri.Scheme.Equals(WebSocketTransportSettings.WebSockets, StringComparison.OrdinalIgnoreCase) ||
                     addressUri.Scheme.Equals(WebSocketTransportSettings.SecureWebSockets, StringComparison.OrdinalIgnoreCase))
            {
                transportSettings = new WebSocketTransportSettings()
                {
                    Uri = addressUri
                };
            }
            else
            {
                throw new NotSupportedException(addressUri.Scheme);
            }

            AmqpSettings settings = this.settings.Clone();

            settings.TransportProviders.Clear();

            if (saslHandler != null)
            {
                // Provider for "AMQP3100"
                SaslTransportProvider saslProvider = new SaslTransportProvider(AmqpVersion.V100);
                saslProvider.AddHandler(saslHandler);
                settings.TransportProviders.Add(saslProvider);
            }

            // Provider for "AMQP0100"
            AmqpTransportProvider amqpProvider = new AmqpTransportProvider(AmqpVersion.V100);

            settings.TransportProviders.Add(amqpProvider);

            AmqpTransportInitiator initiator = new AmqpTransportInitiator(settings, transportSettings);
            TransportBase          transport = await Task.Factory.FromAsync(
                (c, s) => initiator.BeginConnect(timeout, c, s),
                (r) => initiator.EndConnect(r),
                null).ConfigureAwait(false);

            try
            {
                AmqpConnectionSettings connectionSettings = new AmqpConnectionSettings()
                {
                    ContainerId = Guid.NewGuid().ToString(),
                    HostName    = addressUri.Host
                };

                AmqpConnection connection = new AmqpConnection(transport, settings, connectionSettings);
                await connection.OpenAsync(timeout).ConfigureAwait(false);

                return(connection);
            }
            catch
            {
                transport.Abort();
                throw;
            }
        }
Exemplo n.º 10
0
        public async Task <AmqpConnection> OpenConnectionAsync(Uri addressUri, SaslHandler saslHandler, TimeSpan timeout)
        {
            TransportSettings transportSettings;

            if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqp, StringComparison.OrdinalIgnoreCase))
            {
                transportSettings = new TcpTransportSettings()
                {
                    Host = addressUri.Host,
                    Port = addressUri.Port > -1 ? addressUri.Port : AmqpConstants.DefaultPort
                };
            }
            else if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqps, StringComparison.OrdinalIgnoreCase))
            {
                TcpTransportSettings tcpSettings = new TcpTransportSettings()
                {
                    Host = addressUri.Host,
                    Port = addressUri.Port > -1 ? addressUri.Port : AmqpConstants.DefaultSecurePort
                };

                transportSettings = new TlsTransportSettings(tcpSettings)
                {
                    TargetHost = addressUri.Host
                };
            }
#if NET45
            else if (addressUri.Scheme.Equals(WebSocketTransport.WebSockets, StringComparison.OrdinalIgnoreCase) ||
                     addressUri.Scheme.Equals(WebSocketTransport.SecureWebSockets, StringComparison.OrdinalIgnoreCase))
            {
                transportSettings = new WebSocketTransportSettings()
                {
                    Uri = addressUri
                };
            }
#endif
            else
            {
                throw new NotSupportedException(addressUri.Scheme);
            }

            AmqpSettings settings = new AmqpSettings();

            if (saslHandler != null)
            {
                // Provider for "AMQP3100"
                SaslTransportProvider saslProvider = new SaslTransportProvider();
                saslProvider.Versions.Add(new AmqpVersion(1, 0, 0));
                saslProvider.AddHandler(saslHandler);
                settings.TransportProviders.Add(saslProvider);
            }

            // Provider for "AMQP0100"
            AmqpTransportProvider amqpProvider = new AmqpTransportProvider();
            amqpProvider.Versions.Add(new AmqpVersion(new Version(1, 0, 0, 0)));
            settings.TransportProviders.Add(amqpProvider);

            AmqpTransportInitiator initiator = new AmqpTransportInitiator(settings, transportSettings);
            TransportBase          transport = await Task.Factory.FromAsync(
                (c, s) => initiator.BeginConnect(timeout, c, s),
                (r) => initiator.EndConnect(r),
                null);

            try
            {
                AmqpConnectionSettings connectionSettings = new AmqpConnectionSettings()
                {
                    ContainerId = Guid.NewGuid().ToString(),
                    HostName    = addressUri.Host
                };

                AmqpConnection connection = new AmqpConnection(transport, settings, connectionSettings);
                await connection.OpenAsync(timeout);

                return(connection);
            }
            catch
            {
                transport.Abort();
                throw;
            }
        }