Example #1
0
        public void OpenConnection()
        {
            Cleanup();

            Exception connectException = null;

            try
            {
                Open o = new Open()
                {
                    ContainerId  = Endpoint.CFXHandle != null ? Endpoint.CFXHandle : Guid.NewGuid().ToString(),
                    HostName     = VirtualHostName,
                    MaxFrameSize = (uint)AmqpCFXEndpoint.MaxFrameSize.Value
                };

                ConnectionFactory factory = new ConnectionFactory();
#if !NETSTANDARD1_6
                if (this.NetworkUri.Scheme.ToUpper() == "AMQPS")
                {
                    factory.SSL.RemoteCertificateValidationCallback = ValidateServerCertificate;
                }
#endif
                if (string.IsNullOrWhiteSpace(this.NetworkUri.UserInfo))
                {
                    factory.SASL.Profile = SaslProfile.Anonymous;
                }

                Task <Connection> t = factory.CreateAsync(new Address(NetworkUri.ToString()), o, null);
                t.Wait(5000);
                if (t.Status != TaskStatus.RanToCompletion)
                {
                    throw new Exception("Timeout on CreateAsync");
                }
                connection = t.Result;

                connection.Closed += AmqpObject_Closed;
                session            = new Session(connection);
                session.Closed    += AmqpObject_Closed;
            }
            catch (Exception ex)
            {
                connectException = ex;
                Debug.WriteLine(ex.Message);
                Cleanup();
            }

            if (connectException != null)
            {
                Cleanup();
                throw connectException;
            }
        }
Example #2
0
        public void OpenConnection()
        {
            Cleanup();

            Exception connectException = null;

            try
            {
                Open o = new Open()
                {
                    ContainerId = Endpoint.CFXHandle != null ? Endpoint.CFXHandle : Guid.NewGuid().ToString(),
                    HostName    = VirtualHostName
                };

                o = null;

                ConnectionFactory factory = new ConnectionFactory();
#if NET462
                if (Certificate != null)
                {
                    factory.SSL.RemoteCertificateValidationCallback = ValidateServerCertificate;
                }
#endif
                Task <Connection> t = factory.CreateAsync(new Address(NetworkUri.ToString()), o, null);
                t.Wait(5000);
                if (t.Status != TaskStatus.RanToCompletion)
                {
                    throw new Exception("Timeout on CreateAsync");
                }
                connection = t.Result;

                connection.Closed += AmqpObject_Closed;
                session            = new Session(connection);
                session.Closed    += AmqpObject_Closed;
            }
            catch (Exception ex)
            {
                connectException = ex;
                Debug.WriteLine(ex.Message);
                Cleanup();
            }

            if (connectException != null)
            {
                Cleanup();
                throw connectException;
            }
        }
Example #3
0
        protected void EnsureConnection()
        {
            if (NetworkUri == null)
            {
                return;
            }

            bool      madeConnections  = false;
            bool      connected        = true;
            int       newLinkCount     = 0;
            Exception connectException = null;

            lock (this)
            {
                if (connecting)
                {
                    return;
                }
                connecting = true;
            }

            if (IsClosed)
            {
                connected = false;

                try
                {
                    AppLog.Info("Connecting to " + NetworkUri.ToString());
                    OpenConnection();
                    madeConnections = true;
                }
                catch (Exception ex)
                {
                    connectException = ex;
                    AppLog.Error(ex);
                }
            }

            if (!IsClosed)
            {
                connected = true;

                if (madeConnections)
                {
                    AppLog.Info("Connected to " + NetworkUri.ToString() + ".  Establishing Links...");
                }

                lock (this)
                {
                    foreach (AmqpLink link in links.Where(l => l.IsClosed))
                    {
                        try
                        {
                            link.CreateLink(session, this.OnMessageReceived);
                            madeConnections = true;
                            ++newLinkCount;
                        }
                        catch (Exception ex)
                        {
                            connectException = ex;
                            connected        = false;
                            AppLog.Error(ex);
                        }
                    }
                }

                if (connected && madeConnections)
                {
                    AppLog.Info($"{newLinkCount} Links established for {NetworkUri.ToString()}");
                }
            }

            lock (this)
            {
                connecting = false;
            }

            if (!connected)
            {
                AppLog.Error($"Connection Failed for {NetworkUri.ToString()}.  Will attempt again in {AmqpCFXEndpoint.ReconnectInterval?.TotalSeconds} seconds...");
                PostConnectionEvent(ConnectionEvent.ConnectionFailed, connectException);
                Task.Run(new Action(async() =>
                {
                    await Task.Delay(Convert.ToInt32(AmqpCFXEndpoint.ReconnectInterval?.TotalMilliseconds));
                    EnsureConnection();
                }));
            }
            else
            {
                if (madeConnections)
                {
                    PostConnectionEvent(ConnectionEvent.ConnectionEstablished);
                }
                if (AmqpCFXEndpoint.KeepAliveEnabled.Value)
                {
                    lock (keepAliveLock)
                    {
                        if (keepAliveTimer == null)
                        {
                            int interval = Convert.ToInt32(AmqpCFXEndpoint.KeepAliveInterval.Value.TotalMilliseconds);
                            keepAliveTimer = new Timer(new TimerCallback(KeepAliveTimer), null, 0, interval);
                        }
                    }
                }
            }
        }
Example #4
0
 private void AmqpObject_Closed(IAmqpObject sender, Error error)
 {
     if (error != null)
     {
         PostConnectionEvent(ConnectionEvent.ConnectionInterrupted, new Exception(error.ToString()));
         AppLog.Error(string.Format("Connection LOST to endpoint {0}.\r\n\r\n{1}", NetworkUri.ToString(), error.ToString()));
         Close();
         EnsureConnection();
     }
     else
     {
         PostConnectionEvent(ConnectionEvent.ConnectionClosed);
     }
 }
Example #5
0
        private void KeepAliveTimer(object o)
        {
            AppLog.Info(string.Format("Keep Alive Timer Firing for endpoint {0} ...", NetworkUri.ToString()));

            lock (this)
            {
                foreach (AmqpReceiverLink link in links.OfType <AmqpReceiverLink>())
                {
                    link.CloseLink();
                }
            }

            EnsureConnection();
        }
Example #6
0
        public void OpenConnection()
        {
            Cleanup();

            Exception connectException = null;

            Task.Run(() =>
            {
                try
                {
                    bool anonymous = false;
                    if (AuthenticationMode == AuthenticationMode.Anonymous)
                    {
                        anonymous = true;
                    }
                    else if (AuthenticationMode == AuthenticationMode.Auto)
                    {
                        if (!NetworkUri.ToString().Contains("@"))
                        {
                            anonymous = true;
                        }
                    }

                    Open o = new Open()
                    {
                        ContainerId = Endpoint.CFXHandle != null ? Endpoint.CFXHandle : Guid.NewGuid().ToString(),
                        HostName    = TargetHostName
                    };

                    if (!anonymous || Certificate != null)
                    {
                        isAsync = true;

                        ConnectionFactory factory = new ConnectionFactory();
                        if (anonymous)
                        {
                            factory.SASL.Profile = SaslProfile.External;
                        }
                        else
                        {
                            factory.SASL.Profile = SaslProfile.Anonymous;
                        }
                        //if (Certificate != null) factory.SSL.RemoteCertificateValidationCallback = ValidateServerCertificate;

                        Task <Connection> t = factory.CreateAsync(new Address(NetworkUri.ToString()), o, null);
                        t.Wait(5000);
                        if (t.IsCanceled)
                        {
                            throw new Exception("Timeout on CreateAsync");
                        }

                        connection = t.Result;
                    }
                    else
                    {
                        isAsync    = false;
                        connection = new Connection(new Address(NetworkUri.ToString()), SaslProfile.Anonymous, o, null);
                    }

                    connection.Closed += AmqpObject_Closed;
                    session            = new Session(connection);
                    session.Closed    += AmqpObject_Closed;
                }
                catch (Exception ex)
                {
                    connectException = ex;
                    Debug.WriteLine(ex.Message);
                    Cleanup();
                }
            }).Wait();

            if (connectException != null)
            {
                Cleanup();
                throw connectException;
            }
        }
Example #7
0
 private void AmqpObject_Closed(IAmqpObject sender, Error error)
 {
     if (error != null)
     {
         AppLog.Error(string.Format("Connection LOST to endpoint {0}.\r\n\r\n{1}", NetworkUri.ToString(), error.ToString()));
         EnsureConnection();
     }
 }