Esempio n. 1
0
        internal AmqpChannelFactory(AmqpTransportBindingElement bindingElement, BindingContext context)
            : base(context.Binding)
        {
            this.bindingElement    = bindingElement;
            this.bindingContext    = context;
            this.channelProperties = bindingElement.ChannelProperties.Clone();
            this.shared            = bindingElement.Shared;
            this.prefetchLimit     = bindingElement.PrefetchLimit;
            this.maxBufferPoolSize = bindingElement.MaxBufferPoolSize;
            Collection <MessageEncodingBindingElement> messageEncoderBindingElements
                = context.BindingParameters.FindAll <MessageEncodingBindingElement>();

            if (messageEncoderBindingElements.Count > 1)
            {
                throw new InvalidOperationException("More than one MessageEncodingBindingElement was found in the BindingParameters of the BindingContext");
            }
            else if (messageEncoderBindingElements.Count == 1)
            {
                this.messageEncoderFactory = messageEncoderBindingElements[0].CreateMessageEncoderFactory();
            }
            else
            {
                this.messageEncoderFactory = new TextMessageEncodingBindingElement().CreateMessageEncoderFactory();
            }

            openChannels = new List <AmqpTransportChannel>();
        }
Esempio n. 2
0
        internal AmqpChannelListener(AmqpTransportBindingElement bindingElement, BindingContext context)
            : base(context.Binding)
        {
            this.bindingElement    = bindingElement;
            this.channelProperties = bindingElement.ChannelProperties.Clone();
            this.bindingContext    = context;
            this.shared            = bindingElement.Shared;
            this.prefetchLimit     = bindingElement.PrefetchLimit;

            this.maxBufferPoolSize = bindingElement.MaxBufferPoolSize;

            // TODO: review this.  Should be unique hostname based
            this.uri = context.ListenUriBaseAddress;
            this.asyncOnAcceptCaller = new AsyncOnAcceptCaller(this.OnAcceptChannel);
            this.acceptWaitEvent     = new ManualResetEvent(false);

            Collection <MessageEncodingBindingElement> messageEncoderBindingElements
                = context.BindingParameters.FindAll <MessageEncodingBindingElement>();

            if (messageEncoderBindingElements.Count > 1)
            {
                throw new InvalidOperationException("More than one MessageEncodingBindingElement was found in the BindingParameters of the BindingContext");
            }
            else if (messageEncoderBindingElements.Count == 1)
            {
                this.messageEncoderFactory = messageEncoderBindingElements[0].CreateMessageEncoderFactory();
            }
            else
            {
                this.messageEncoderFactory = new TextMessageEncodingBindingElement().CreateMessageEncoderFactory();
            }
        }
Esempio n. 3
0
 private static ManagedConnection GetManagedConnection(AmqpChannelProperties channelProperties, bool connectionSharing)
 {
     if (connectionSharing)
     {
         string key = MakeKey(channelProperties);
         lock (connectionLock)
         {
             ManagedConnection mc = null;
             if (!sharedInstances.TryGetValue(key, out mc))
             {
                 mc = new ManagedConnection(true);
                 sharedInstances.Add(key, mc);
             }
             return mc;
         }
     }
     else
     {
         lock (connectionLock)
         {
             if (unsharedInstance == null)
             {
                 unsharedInstance = new ManagedConnection(false);
             }
             return unsharedInstance;
         }
     }
 }
Esempio n. 4
0
 private static ManagedConnection GetManagedConnection(AmqpChannelProperties channelProperties, bool connectionSharing)
 {
     if (connectionSharing)
     {
         string key = MakeKey(channelProperties);
         lock (connectionLock)
         {
             ManagedConnection mc = null;
             if (!sharedInstances.TryGetValue(key, out mc))
             {
                 mc = new ManagedConnection(true);
                 sharedInstances.Add(key, mc);
             }
             return(mc);
         }
     }
     else
     {
         lock (connectionLock)
         {
             if (unsharedInstance == null)
             {
                 unsharedInstance = new ManagedConnection(false);
             }
             return(unsharedInstance);
         }
     }
 }
Esempio n. 5
0
 protected AmqpTransportBindingElement(AmqpTransportBindingElement other)
     : base(other)
 {
     this.channelProperties = other.channelProperties.Clone();
     this.shared            = other.shared;
     this.prefetchLimit     = other.prefetchLimit;
     this.bindingSecurity   = other.bindingSecurity;
 }
Esempio n. 6
0
        internal static void FindAuthenticationCredentials(AmqpChannelProperties channelProperties,
                                                           BindingContext bindingContext)
        {
            AmqpTransportSecurity tsec = channelProperties.AmqpTransportSecurity;

            if (tsec == null)
            {
                // no auth
                return;
            }

            if (tsec.CredentialType == AmqpCredentialType.Anonymous)
            {
                // no auth
                return;
            }

            // credentials search order: specific AmqpCredentials, specific
            // ClientCredentials (if applicable), binding's default credentials

            AmqpCredential amqpCred = bindingContext.BindingParameters.Find <AmqpCredential>();

            if (amqpCred != null)
            {
                channelProperties.AmqpCredential = amqpCred.Clone();
                return;
            }

            if (!tsec.IgnoreEndpointClientCredentials)
            {
                ClientCredentials cliCred = bindingContext.BindingParameters.Find <ClientCredentials>();
                if (cliCred != null)
                {
                    if (cliCred.UserName != null)
                    {
                        if (cliCred.UserName.UserName != null)
                        {
                            channelProperties.AmqpCredential = new AmqpCredential(cliCred.UserName.UserName,
                                                                                  cliCred.UserName.Password);
                            return;
                        }
                    }
                }
            }

            if (tsec.DefaultCredential != null)
            {
                channelProperties.AmqpCredential = tsec.DefaultCredential.Clone();
            }
        }
Esempio n. 7
0
        public AmqpChannelProperties Clone()
        {
            AmqpChannelProperties props = (AmqpChannelProperties)this.MemberwiseClone();

            if (this.defaultMessageProperties != null)
            {
                props.defaultMessageProperties = this.defaultMessageProperties.Clone();
            }

            if (this.amqpTransportSecurity != null)
            {
                props.amqpTransportSecurity = this.amqpTransportSecurity.Clone();
            }

            if (this.amqpCredential != null)
            {
                this.amqpCredential = this.amqpCredential.Clone();
            }

            return(props);
        }
Esempio n. 8
0
        private static string MakeKey(AmqpChannelProperties props)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(props.BrokerHost);
            sb.Append(':');
            sb.Append(props.BrokerPort);
            sb.Append(':');
            sb.Append(props.TransferMode);

            AmqpTransportSecurity sec = props.AmqpTransportSecurity;

            if (sec == null)
            {
                return(sb.ToString());
            }

            if (sec.UseSSL)
            {
                sb.Append(":SSL");
            }

            if (sec.CredentialType == AmqpCredentialType.Plain)
            {
                sb.Append(":saslP");
                AmqpCredential cred = props.AmqpCredential;
                if (cred != null)
                {
                    sb.Append(":NM:");
                    sb.Append(cred.UserName);
                    sb.Append(":PW:");
                    sb.Append(cred.Password);
                }
            }

            return(sb.ToString());
        }
Esempio n. 9
0
 public static OutputLink GetOutputLink(AmqpChannelProperties channelProperties, bool connectionSharing, bool sessionSharing, string qname)
 {
     ManagedConnection mc = GetManagedConnection(channelProperties, connectionSharing);
     return (OutputLink)mc.GetLink(channelProperties, sessionSharing, null, qname);
 }
Esempio n. 10
0
            public object GetLink(AmqpChannelProperties channelProperties, bool sessionSharing, string inputQueue, string outputQueue)
            {
                AmqpConnection connection = null;
                AmqpSession session = null;
                Object link = null;
                bool newConnection = false;
                //bool newSession = false;
                bool success = false;

                // when called in the non-shared case, only stack variables should be used for holding connections/sessions/links

                if (this.shared)
                {
                    Monitor.Enter(this); // lock
                }

                try
                {
                    if (this.shared)
                    {
                        // TODO: check shared connection not closed (i.e. network drop) and refresh this instance if needed
                        if (sessionSharing)
                        {
                            throw new NotImplementedException("shared session");
                            /* * ... once we have a defined shared session config parameter:

                            // lazilly create
                            if (this.sharedSessions == null)
                            {
                                this.sharedSessions = new Dictionary<string, AmqpSession>();
                            }

                            alreadydeclaredstring sessionKey = channelProperties.name_of_key_goes_here;
                            this.sharedSessions.TryGetValue(sessionKey, out session);

                            * */
                        }

                        if (this.sharedConnection != null)
                        {
                            connection = this.sharedConnection;
                        }
                    }

                    if (connection == null)
                    {
                        if (channelProperties.AmqpSecurityMode != AmqpSecurityMode.None)
                        {
                            string user = null;
                            string passwd = null;
                            bool ssl = false;
                            bool saslPlain = false;

                            AmqpTransportSecurity tsec = channelProperties.AmqpTransportSecurity;
                            if (tsec.UseSSL)
                            {
                                ssl = true;
                            }

                            if (tsec.CredentialType == AmqpCredentialType.Plain)
                            {
                                saslPlain = true;
                                AmqpCredential plainCred = channelProperties.AmqpCredential;
                                if (plainCred != null)
                                {
                                    user = plainCred.UserName;
                                    passwd = plainCred.Password;
                                }
                            }

                            connection = new AmqpConnection(channelProperties.BrokerHost, channelProperties.BrokerPort,
                                ssl, saslPlain, user, passwd);
                        }
                        else
                        {
                            connection = new AmqpConnection(channelProperties.BrokerHost, channelProperties.BrokerPort);
                        }

                        newConnection = true;
                        if (this.shared)
                        {
                            connection.OnConnectionIdle += new ConnectionIdleEventHandler(this.IdleConnectionHandler);
                        }
                        else
                        {
                            connection.OnConnectionIdle += new ConnectionIdleEventHandler(UnsharedIdleConnectionHandler);
                        }
                    }

                    if (session == null)
                    {
                        session = connection.CreateSession();
                        //newSession = true;
                    }

                    if (inputQueue != null)
                    {
                        link = session.CreateInputLink(inputQueue);
                    }
                    else
                    {
                        link = session.CreateOutputLink(outputQueue);
                    }

                    if (this.shared)
                    {
                        if (newConnection)
                        {
                            this.sharedConnection = connection;
                        }
                        /*
                        if (newSession)
                        {
                            sharedSessions.Add(foo, session);
                        }
                         * */
                    }

                    success = true;
                }
                finally
                {
                    if (this.shared)
                    {
                        Monitor.Exit(this);
                    }
                    if (!success)
                    {
                        /*
                        if (newSession)
                        {
                            session.Close();
                        }
                         */
                        if (newConnection)
                        {
                            connection.Close();
                        }
                    }
                }

                return link;
            }
Esempio n. 11
0
        private static string MakeKey(AmqpChannelProperties props)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(props.BrokerHost);
            sb.Append(':');
            sb.Append(props.BrokerPort);
            sb.Append(':');
            sb.Append(props.TransferMode);

            AmqpTransportSecurity sec = props.AmqpTransportSecurity;
            if (sec == null)
            {
                return sb.ToString();
            }

            if (sec.UseSSL)
            {
                sb.Append(":SSL");
            }

            if (sec.CredentialType == AmqpCredentialType.Plain)
            {
                sb.Append(":saslP");
                AmqpCredential cred = props.AmqpCredential;
                if (cred != null)
                {
                    sb.Append(":NM:");
                    sb.Append(cred.UserName);
                    sb.Append(":PW:");
                    sb.Append(cred.Password);
                }
            }

            return sb.ToString();
        }
Esempio n. 12
0
        internal AmqpTransportChannel(ChannelManagerBase factory, AmqpChannelProperties channelProperties, EndpointAddress remoteAddress, MessageEncoder msgEncoder, long maxBufferPoolSize, bool sharedConnection, int prefetchLimit)
            : base(factory)
        {
            this.isInputChannel = (factory is ChannelListenerBase) || (factory is AmqpChannelFactory <IInputChannel>);

            if (remoteAddress == null)
            {
                throw new ArgumentException("Null Endpoint Address");
            }

            this.factoryChannelProperties = channelProperties;
            this.shared        = sharedConnection;
            this.prefetchLimit = prefetchLimit;
            this.remoteAddress = remoteAddress;

            // pull out host, port, queue, and connection arguments
            string qpidAddress = this.UriToQpidAddress(remoteAddress.Uri, out subject);

            this.encoder = msgEncoder;
            string ct = String.Empty;

            if (this.encoder != null)
            {
                ct = this.encoder.ContentType;
                if (ct != null)
                {
                    int pos = ct.IndexOf(';');
                    if (pos != -1)
                    {
                        ct = ct.Substring(0, pos).Trim();
                    }
                }
                else
                {
                    ct = "application/octet-stream";
                }
            }

            this.encoderContentType = ct;

            if (this.factoryChannelProperties.TransferMode == TransferMode.Streamed)
            {
                this.streamed = true;
            }
            else
            {
                if (!(this.factoryChannelProperties.TransferMode == TransferMode.Buffered))
                {
                    throw new ArgumentException("TransferMode mode must be \"Streamed\" or \"Buffered\"");
                }

                this.streamed = false;
            }

            this.bufferManager = BufferManager.CreateBufferManager(maxBufferPoolSize, int.MaxValue);

            this.asyncOpenCaller  = new AsyncTimeSpanCaller(this.OnOpen);
            this.asyncCloseCaller = new AsyncTimeSpanCaller(this.OnClose);

            if (this.isInputChannel)
            {
                this.inputLink = ConnectionManager.GetInputLink(this.factoryChannelProperties, shared, false, qpidAddress);
                this.inputLink.PrefetchLimit = this.prefetchLimit;
            }
            else
            {
                this.outputLink  = ConnectionManager.GetOutputLink(this.factoryChannelProperties, shared, false, qpidAddress);
                this.subject     = this.outputLink.DefaultSubject;
                this.qpidSubject = this.outputLink.QpidSubject;
            }
        }
Esempio n. 13
0
            public object GetLink(AmqpChannelProperties channelProperties, bool sessionSharing, string inputQueue, string outputQueue)
            {
                AmqpConnection connection    = null;
                AmqpSession    session       = null;
                Object         link          = null;
                bool           newConnection = false;
                //bool newSession = false;
                bool success = false;

                // when called in the non-shared case, only stack variables should be used for holding connections/sessions/links

                if (this.shared)
                {
                    Monitor.Enter(this); // lock
                }

                try
                {
                    if (this.shared)
                    {
                        // TODO: check shared connection not closed (i.e. network drop) and refresh this instance if needed
                        if (sessionSharing)
                        {
                            throw new NotImplementedException("shared session");

                            /* * ... once we have a defined shared session config parameter:
                             *
                             * // lazilly create
                             * if (this.sharedSessions == null)
                             * {
                             *  this.sharedSessions = new Dictionary<string, AmqpSession>();
                             * }
                             *
                             * alreadydeclaredstring sessionKey = channelProperties.name_of_key_goes_here;
                             * this.sharedSessions.TryGetValue(sessionKey, out session);
                             *
                             * */
                        }

                        if (this.sharedConnection != null)
                        {
                            connection = this.sharedConnection;
                        }
                    }

                    if (connection == null)
                    {
                        if (channelProperties.AmqpSecurityMode != AmqpSecurityMode.None)
                        {
                            string user      = null;
                            string passwd    = null;
                            bool   ssl       = false;
                            bool   saslPlain = false;

                            AmqpTransportSecurity tsec = channelProperties.AmqpTransportSecurity;
                            if (tsec.UseSSL)
                            {
                                ssl = true;
                            }

                            if (tsec.CredentialType == AmqpCredentialType.Plain)
                            {
                                saslPlain = true;
                                AmqpCredential plainCred = channelProperties.AmqpCredential;
                                if (plainCred != null)
                                {
                                    user   = plainCred.UserName;
                                    passwd = plainCred.Password;
                                }
                            }

                            connection = new AmqpConnection(channelProperties.BrokerHost, channelProperties.BrokerPort,
                                                            ssl, saslPlain, user, passwd);
                        }
                        else
                        {
                            connection = new AmqpConnection(channelProperties.BrokerHost, channelProperties.BrokerPort);
                        }

                        newConnection = true;
                        if (this.shared)
                        {
                            connection.OnConnectionIdle += new ConnectionIdleEventHandler(this.IdleConnectionHandler);
                        }
                        else
                        {
                            connection.OnConnectionIdle += new ConnectionIdleEventHandler(UnsharedIdleConnectionHandler);
                        }
                    }

                    if (session == null)
                    {
                        session = connection.CreateSession();
                        //newSession = true;
                    }

                    if (inputQueue != null)
                    {
                        link = session.CreateInputLink(inputQueue);
                    }
                    else
                    {
                        link = session.CreateOutputLink(outputQueue);
                    }

                    if (this.shared)
                    {
                        if (newConnection)
                        {
                            this.sharedConnection = connection;
                        }

                        /*
                         * if (newSession)
                         * {
                         *  sharedSessions.Add(foo, session);
                         * }
                         * */
                    }

                    success = true;
                }
                finally
                {
                    if (this.shared)
                    {
                        Monitor.Exit(this);
                    }
                    if (!success)
                    {
                        /*
                         * if (newSession)
                         * {
                         *  session.Close();
                         * }
                         */
                        if (newConnection)
                        {
                            connection.Close();
                        }
                    }
                }

                return(link);
            }
Esempio n. 14
0
        public static InputLink GetInputLink(AmqpChannelProperties channelProperties, bool connectionSharing, bool sessionSharing, string qname)
        {
            ManagedConnection mc = GetManagedConnection(channelProperties, connectionSharing);

            return((InputLink)mc.GetLink(channelProperties, sessionSharing, qname, null));
        }
Esempio n. 15
0
        internal static void FindAuthenticationCredentials(AmqpChannelProperties channelProperties,
            BindingContext bindingContext)
        {
            AmqpTransportSecurity tsec = channelProperties.AmqpTransportSecurity;
            if (tsec == null)
            {
                // no auth
                return;
            }

            if (tsec.CredentialType == AmqpCredentialType.Anonymous)
            {
                // no auth
                return;
            }

            // credentials search order: specific AmqpCredentials, specific
            // ClientCredentials (if applicable), binding's default credentials

            AmqpCredential amqpCred = bindingContext.BindingParameters.Find<AmqpCredential>();
            if (amqpCred != null)
            {
                channelProperties.AmqpCredential = amqpCred.Clone();
                return;
            }

            if (!tsec.IgnoreEndpointClientCredentials)
            {
                ClientCredentials cliCred = bindingContext.BindingParameters.Find<ClientCredentials>();
                if (cliCred != null)
                {
                    if (cliCred.UserName != null)
                    {
                        if (cliCred.UserName.UserName != null)
                        {
                            channelProperties.AmqpCredential = new AmqpCredential(cliCred.UserName.UserName,
                                cliCred.UserName.Password);
                            return;
                        }
                    }
                }
            }

            if (tsec.DefaultCredential != null)
            {
                channelProperties.AmqpCredential = tsec.DefaultCredential.Clone();
            }
        }
Esempio n. 16
0
 public AmqpTransportBindingElement()
 {
     // start with default properties
     channelProperties = new AmqpChannelProperties();
 }