Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: connection checks #2

Merged
merged 1 commit into from
May 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/CymaticLabs.Unity3D.Amqp/RabbitMq/RabbitMqBrokerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public RabbitMqBrokerConnection(string server, int amqpPort, int webPort, string
/// <param name="password">The connection password.</param>
/// <param name="reconnectInterval">The number of seconds to wait before connection retry attempts.</param>
/// <param name="requestedHeartbeat">The client/server heartbeat in seconds.</param>
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;
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -443,8 +443,8 @@ public void Disconnect()
exchangeSubscriptions.Clear();
Connection.Close();

lock(stateLock)
{
lock (stateLock)
{
State = AmqpConnectionStates.Disconnected;
}

Expand All @@ -466,7 +466,7 @@ public void Disconnect()
/// </summary>
public void ResetConnection()
{
lock(stateLock)
lock (stateLock)
{
// Reset state
State = AmqpConnectionStates.Disconnected;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -906,7 +906,7 @@ public void Publish(string exchange, string routingKey, IAmqpMessageProperties p
/// <returns>An Exception if one occurred during the operation, otherwise NULL.</returns>
public Exception DeclareExchange(string name, AmqpExchangeTypes type, bool durable = true, bool autoDelete = false, IDictionary<string, object> 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
Expand All @@ -932,7 +932,7 @@ public Exception DeclareExchange(string name, AmqpExchangeTypes type, bool durab
/// <returns>An Exception if one occurred during the operation, otherwise NULL.</returns>
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
Expand Down