Beispiel #1
0
        public ConnectionConfiguration()
        {
            // set default values
            Port                   = DefaultPort;
            VirtualHost            = "/";
            UserName               = "******";
            Password               = "******";
            RequestedHeartbeat     = 10;
            Timeout                = 10; // seconds
            PublisherConfirms      = false;
            PersistentMessages     = true;
            UseBackgroundThreads   = false;
            ConnectIntervalAttempt = TimeSpan.FromSeconds(5);
            DispatcherQueueSize    = 1024;

            // prefetchCount determines how many messages will be allowed in the local in-memory queue
            // setting to zero makes this infinite, but risks an out-of-memory exception.
            // set to 50 based on this blog post:
            // http://www.rabbitmq.com/blog/2012/04/25/rabbitmq-performance-measurements-part-2/
            PrefetchCount  = 50;
            AuthMechanisms = new IAuthMechanismFactory[] { new PlainMechanismFactory() };

            Hosts = new List <HostConfiguration>();

            Ssl = new SslOption();
            ClientProperties = new Dictionary <string, object>();
        }
        /// <summary>
        /// Given a list of mechanism names supported by the server, select a preferred mechanism,
        ///  or null if we have none in common.
        /// </summary>
        public IAuthMechanismFactory AuthMechanismFactory(IList <string> mechanismNames)
        {
            // Our list is in order of preference, the server one is not.
            for (int index = 0; index < AuthMechanisms.Count; index++)
            {
                IAuthMechanismFactory factory = AuthMechanisms[index];
                string factoryName            = factory.Name;
                if (mechanismNames.Any <string>(x => string.Equals(x, factoryName, StringComparison.OrdinalIgnoreCase)))
                {
                    return(factory);
                }
            }

            return(null);
        }
Beispiel #3
0
        private void StartAndTune()
        {
            var connectionStartCell = new BlockingCell <ConnectionStartDetails>();

            _model0.m_connectionStartCell        = connectionStartCell;
            _model0.HandshakeContinuationTimeout = _config.HandshakeContinuationTimeout;
            _frameHandler.ReadTimeout            = _config.HandshakeContinuationTimeout;
            _frameHandler.SendHeader();

            ConnectionStartDetails connectionStart = connectionStartCell.WaitForValue();

            if (connectionStart is null)
            {
                throw new IOException("connection.start was never received, likely due to a network timeout");
            }

            ServerProperties = connectionStart.m_serverProperties;

            var serverVersion = new AmqpVersion(connectionStart.m_versionMajor, connectionStart.m_versionMinor);

            if (!serverVersion.Equals(Protocol.Version))
            {
                TerminateMainloop();
                FinishClose();
                throw new ProtocolVersionMismatchException(Protocol.MajorVersion, Protocol.MinorVersion, serverVersion.Major, serverVersion.Minor);
            }

            // FIXME: parse out locales properly!
            ConnectionTuneDetails connectionTune = default;
            bool tuned = false;

            try
            {
                string mechanismsString = Encoding.UTF8.GetString(connectionStart.m_mechanisms);
                IAuthMechanismFactory mechanismFactory = GetAuthMechanismFactory(mechanismsString);
                IAuthMechanism        mechanism        = mechanismFactory.GetInstance();
                byte[]? challenge = null;
                do
                {
                    byte[] response = mechanism.handleChallenge(challenge, _config);
                    ConnectionSecureOrTune res;
                    if (challenge is null)
                    {
                        res = _model0.ConnectionStartOk(ClientProperties,
                                                        mechanismFactory.Name,
                                                        response,
                                                        "en_US");
                    }
                    else
                    {
                        res = _model0.ConnectionSecureOk(response);
                    }

                    if (res.m_challenge is null)
                    {
                        connectionTune = res.m_tuneDetails;
                        tuned          = true;
                    }
                    else
                    {
                        challenge = res.m_challenge;
                    }
                }while (!tuned);
            }
            catch (OperationInterruptedException e)
            {
                if (e.ShutdownReason != null && e.ShutdownReason.ReplyCode == Constants.AccessRefused)
                {
                    throw new AuthenticationFailureException(e.ShutdownReason.ReplyText);
                }
                throw new PossibleAuthenticationFailureException(
                          "Possibly caused by authentication failure", e);
            }

            ushort channelMax = (ushort)NegotiatedMaxValue(_config.MaxChannelCount, connectionTune.m_channelMax);

            _sessionManager = new SessionManager(this, channelMax);

            uint frameMax = NegotiatedMaxValue(_config.MaxFrameSize, connectionTune.m_frameMax);

            FrameMax       = frameMax;
            MaxPayloadSize = frameMax == 0 ? int.MaxValue : (int)frameMax - Client.Impl.Framing.BaseFrameSize;

            uint heartbeatInSeconds = NegotiatedMaxValue((uint)_config.HeartbeatInterval.TotalSeconds, (uint)connectionTune.m_heartbeatInSeconds);

            Heartbeat = TimeSpan.FromSeconds(heartbeatInSeconds);

            _model0.ConnectionTuneOk(channelMax, frameMax, (ushort)Heartbeat.TotalSeconds);

            // now we can start heartbeat timers
            MaybeStartHeartbeatTimers();
        }
Beispiel #4
0
        protected void StartAndTune()
        {
            var connectionStartCell = new BlockingCell();

            m_model0.m_connectionStartCell        = connectionStartCell;
            m_model0.HandshakeContinuationTimeout = m_factory.HandshakeContinuationTimeout;
            m_frameHandler.ReadTimeout            = (int)m_factory.HandshakeContinuationTimeout.TotalMilliseconds;
            m_frameHandler.SendHeader();

            var connectionStart = (ConnectionStartDetails)
                                  connectionStartCell.Value;

            if (connectionStart == null)
            {
                throw new IOException("connection.start was never received, likely due to a network timeout");
            }

            ServerProperties = connectionStart.m_serverProperties;

            var serverVersion = new AmqpVersion(connectionStart.m_versionMajor,
                                                connectionStart.m_versionMinor);

            if (!serverVersion.Equals(Protocol.Version))
            {
                TerminateMainloop();
                FinishClose();
                throw new ProtocolVersionMismatchException(Protocol.MajorVersion,
                                                           Protocol.MinorVersion,
                                                           serverVersion.Major,
                                                           serverVersion.Minor);
            }

            ClientProperties = new Dictionary <string, object>(m_factory.ClientProperties)
            {
                ["capabilities"]    = Protocol.Capabilities,
                ["connection_name"] = ClientProvidedName
            };

            // FIXME: parse out locales properly!
            ConnectionTuneDetails connectionTune = default;
            bool tuned = false;

            try
            {
                string   mechanismsString = Encoding.UTF8.GetString(connectionStart.m_mechanisms, 0, connectionStart.m_mechanisms.Length);
                string[] mechanisms       = mechanismsString.Split(' ');
                IAuthMechanismFactory mechanismFactory = m_factory.AuthMechanismFactory(mechanisms);
                if (mechanismFactory == null)
                {
                    throw new IOException("No compatible authentication mechanism found - " +
                                          "server offered [" + mechanismsString + "]");
                }
                IAuthMechanism mechanism = mechanismFactory.GetInstance();
                byte[]         challenge = null;
                do
                {
                    byte[] response = mechanism.HandleChallenge(challenge, m_factory);
                    ConnectionSecureOrTune res;
                    if (challenge == null)
                    {
                        res = m_model0.ConnectionStartOk(ClientProperties,
                                                         mechanismFactory.Name,
                                                         response,
                                                         "en_US");
                    }
                    else
                    {
                        res = m_model0.ConnectionSecureOk(response);
                    }

                    if (res.m_challenge == null)
                    {
                        connectionTune = res.m_tuneDetails;
                        tuned          = true;
                    }
                    else
                    {
                        challenge = res.m_challenge;
                    }
                }while (!tuned);
            }
            catch (OperationInterruptedException e)
            {
                if (e.ShutdownReason?.ReplyCode == Constants.AccessRefused)
                {
                    throw new AuthenticationFailureException(e.ShutdownReason.ReplyText);
                }
                throw new PossibleAuthenticationFailureException(
                          "Possibly caused by authentication failure", e);
            }

            var channelMax = (ushort)NegotiatedMaxValue(m_factory.RequestedChannelMax,
                                                        connectionTune.m_channelMax);

            m_sessionManager = new SessionManager(this, channelMax);

            uint frameMax = NegotiatedMaxValue(m_factory.RequestedFrameMax,
                                               connectionTune.m_frameMax);

            FrameMax = frameMax;

            var heartbeat = (ushort)NegotiatedMaxValue(m_factory.RequestedHeartbeat,
                                                       connectionTune.m_heartbeat);

            Heartbeat = heartbeat;

            m_model0.ConnectionTuneOk(channelMax,
                                      frameMax,
                                      heartbeat);

            // now we can start heartbeat timers
            MaybeStartHeartbeatTimers();
        }