From 78d9b4f8822d04b8ab950f40d1daed4cf57066f8 Mon Sep 17 00:00:00 2001 From: Marius Hamre nordrik Date: Wed, 10 May 2023 15:04:02 +0200 Subject: [PATCH] Fix connected checks --- .../RabbitMq/RabbitMqBrokerConnection.cs | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/CymaticLabs.Unity3D.Amqp/RabbitMq/RabbitMqBrokerConnection.cs b/src/CymaticLabs.Unity3D.Amqp/RabbitMq/RabbitMqBrokerConnection.cs index 54da5c6..aef34e0 100644 --- a/src/CymaticLabs.Unity3D.Amqp/RabbitMq/RabbitMqBrokerConnection.cs +++ b/src/CymaticLabs.Unity3D.Amqp/RabbitMq/RabbitMqBrokerConnection.cs @@ -258,7 +258,7 @@ public RabbitMqBrokerConnection(string server, int amqpPort, int webPort, string /// The connection password. /// The number of seconds to wait before connection retry attempts. /// The client/server heartbeat in seconds. - public RabbitMqBrokerConnection(string name, string server, int amqpPort, int webPort, string virtualHost, + public RabbitMqBrokerConnection(string name, string server, int amqpPort, int webPort, string virtualHost, string username, string password, short reconnectInterval = 5, ushort requestedHeartbeat = 30) { Name = name; @@ -290,7 +290,7 @@ private void EnsureConnection() // Ensure that the cliient is connected if (State == AmqpConnectionStates.Connected && (Connection == null || !Connection.IsOpen)) { - lock(stateLock) State = AmqpConnectionStates.Disconnected; + lock (stateLock) State = AmqpConnectionStates.Disconnected; Disconnected?.Invoke(this, EventArgs.Empty); } } @@ -311,7 +311,7 @@ public void Connect() } if (IsConnected) return; - lock(stateLock) State = AmqpConnectionStates.Connecting; + lock (stateLock) State = AmqpConnectionStates.Connecting; //StreamRuntime.Current.LogInfo("Connecting to {0}...", this); Console.WriteLine("Connecting to {0}...", this); @@ -369,7 +369,7 @@ public void Connect() connection.ConnectionUnblocked += Connection_ConnectionUnblocked; // Reset retries - lock(stateLock) connectionRetryCount = 0; + lock (stateLock) connectionRetryCount = 0; reconnect = false; bc.State = AmqpConnectionStates.Connected; @@ -399,7 +399,7 @@ public void Connect() else { // Update retry attempt - lock(stateLock) connectionRetryCount++; + lock (stateLock) connectionRetryCount++; Console.WriteLine("(retries:{0}) Error connecting to {1} => {2}", connectionRetryCount, this, ex.Message); ConnectionError?.Invoke(this, new ExceptionEventArgs(ex)); @@ -443,8 +443,8 @@ public void Disconnect() exchangeSubscriptions.Clear(); Connection.Close(); - lock(stateLock) - { + lock (stateLock) + { State = AmqpConnectionStates.Disconnected; } @@ -466,7 +466,7 @@ public void Disconnect() /// public void ResetConnection() { - lock(stateLock) + lock (stateLock) { // Reset state State = AmqpConnectionStates.Disconnected; @@ -610,7 +610,7 @@ public Exception Subscribe(AmqpExchangeSubscription subscription) Console.WriteLine("Subscribed to {0}{1} on {2}", subscription.ExchangeName, subscription.RoutingKey, subscription.Connection); //} - + exchangeSubscriptions.Add(subscription); // Notify @@ -766,7 +766,7 @@ public Exception Unsubscribe(AmqpQueueSubscription subscription) // Cancel the consumer try { - lock(Channel) + lock (Channel) { Channel.BasicCancel(subscription.ConsumerTag); Console.WriteLine("{0} has unsubscribed from queue: {1}", this, subscription.QueueName); @@ -906,7 +906,7 @@ public void Publish(string exchange, string routingKey, IAmqpMessageProperties p /// An Exception if one occurred during the operation, otherwise NULL. public Exception DeclareExchange(string name, AmqpExchangeTypes type, bool durable = true, bool autoDelete = false, IDictionary args = null) { - if (IsConnected) throw new InvalidOperationException("Exchanges cannot be declared when disconnected"); + if (!IsConnected) throw new InvalidOperationException("Exchanges cannot be declared when disconnected"); if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name"); try @@ -932,7 +932,7 @@ public Exception DeclareExchange(string name, AmqpExchangeTypes type, bool durab /// An Exception if one occurred during the operation, otherwise NULL. public Exception DeleteExchange(string name, bool ifUnused = false) { - if (IsConnected) throw new InvalidOperationException("Exchanges cannot be declared when disconnected"); + if (!IsConnected) throw new InvalidOperationException("Exchanges cannot be declared when disconnected"); if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name"); try