mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 11:45:43 +08:00
Convert MqttClient._connectState to enum
This commit is contained in:
@@ -39,10 +39,15 @@ namespace MQTTnet.Client
|
|||||||
IMqttChannelAdapter _adapter;
|
IMqttChannelAdapter _adapter;
|
||||||
bool _cleanDisconnectInitiated;
|
bool _cleanDisconnectInitiated;
|
||||||
volatile int _connectState;
|
volatile int _connectState;
|
||||||
const int CONNECT_STATED_DISCONNECTED = 0;
|
|
||||||
const int CONNECT_STATED_DISCONNECTING = 1;
|
enum ConnectState
|
||||||
const int CONNECT_STATED_CONNECTED = 2;
|
{
|
||||||
const int CONNECT_STATED_CONNECTING = 3;
|
Disconnected = 0,
|
||||||
|
Disconnecting,
|
||||||
|
Connected,
|
||||||
|
Connecting
|
||||||
|
}
|
||||||
|
|
||||||
MqttClientDisconnectReason _disconnectReason;
|
MqttClientDisconnectReason _disconnectReason;
|
||||||
|
|
||||||
DateTime _lastPacketSentTimestamp;
|
DateTime _lastPacketSentTimestamp;
|
||||||
@@ -61,7 +66,7 @@ namespace MQTTnet.Client
|
|||||||
|
|
||||||
public IMqttApplicationMessageReceivedHandler ApplicationMessageReceivedHandler { get; set; }
|
public IMqttApplicationMessageReceivedHandler ApplicationMessageReceivedHandler { get; set; }
|
||||||
|
|
||||||
public bool IsConnected => _connectState == CONNECT_STATED_CONNECTED;
|
public bool IsConnected => (ConnectState)_connectState == ConnectState.Connected;
|
||||||
|
|
||||||
public IMqttClientOptions Options { get; private set; }
|
public IMqttClientOptions Options { get; private set; }
|
||||||
|
|
||||||
@@ -74,7 +79,7 @@ namespace MQTTnet.Client
|
|||||||
|
|
||||||
ThrowIfDisposed();
|
ThrowIfDisposed();
|
||||||
|
|
||||||
if (Interlocked.CompareExchange(ref _connectState, CONNECT_STATED_CONNECTING, CONNECT_STATED_DISCONNECTED) != CONNECT_STATED_DISCONNECTED)
|
if (CompareExchangeConnectState(ConnectState.Connecting, ConnectState.Disconnected) != ConnectState.Disconnected)
|
||||||
throw new InvalidOperationException("Not allowed to connect while connect/disconnect is pending.");
|
throw new InvalidOperationException("Not allowed to connect while connect/disconnect is pending.");
|
||||||
|
|
||||||
MqttClientAuthenticateResult authenticateResult = null;
|
MqttClientAuthenticateResult authenticateResult = null;
|
||||||
@@ -113,7 +118,7 @@ namespace MQTTnet.Client
|
|||||||
_keepAlivePacketsSenderTask = Task.Run(() => TrySendKeepAliveMessagesAsync(backgroundCancellationToken), backgroundCancellationToken);
|
_keepAlivePacketsSenderTask = Task.Run(() => TrySendKeepAliveMessagesAsync(backgroundCancellationToken), backgroundCancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
Interlocked.CompareExchange(ref _connectState, CONNECT_STATED_CONNECTED, CONNECT_STATED_CONNECTING);
|
CompareExchangeConnectState(ConnectState.Connected, ConnectState.Connecting);
|
||||||
|
|
||||||
_logger.Info("Connected.");
|
_logger.Info("Connected.");
|
||||||
|
|
||||||
@@ -365,7 +370,7 @@ namespace MQTTnet.Client
|
|||||||
{
|
{
|
||||||
Cleanup();
|
Cleanup();
|
||||||
_cleanDisconnectInitiated = false;
|
_cleanDisconnectInitiated = false;
|
||||||
Interlocked.CompareExchange(ref _connectState, CONNECT_STATED_DISCONNECTED, CONNECT_STATED_DISCONNECTING);
|
CompareExchangeConnectState(ConnectState.Disconnected, ConnectState.Disconnecting);
|
||||||
|
|
||||||
_logger.Info("Disconnected.");
|
_logger.Info("Disconnected.");
|
||||||
|
|
||||||
@@ -800,20 +805,20 @@ namespace MQTTnet.Client
|
|||||||
|
|
||||||
bool DisconnectIsPendingOrFinished()
|
bool DisconnectIsPendingOrFinished()
|
||||||
{
|
{
|
||||||
var connectState = _connectState;
|
var connectState = (ConnectState)_connectState;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
switch (connectState)
|
switch (connectState)
|
||||||
{
|
{
|
||||||
case CONNECT_STATED_DISCONNECTING:
|
case ConnectState.Disconnected:
|
||||||
case CONNECT_STATED_DISCONNECTED:
|
case ConnectState.Disconnecting:
|
||||||
return true;
|
return true;
|
||||||
case CONNECT_STATED_CONNECTING:
|
case ConnectState.Connected:
|
||||||
case CONNECT_STATED_CONNECTED:
|
case ConnectState.Connecting:
|
||||||
// This will compare the _connectState to old value and set it to "CONNECT_STATED_DISCONNECTING" afterwards.
|
// This will compare the _connectState to old value and set it to "CONNECT_STATED_DISCONNECTING" afterwards.
|
||||||
// So the first caller will get a "false" and all subsequent ones will get "true".
|
// So the first caller will get a "false" and all subsequent ones will get "true".
|
||||||
var newState = Interlocked.CompareExchange(ref _connectState, CONNECT_STATED_DISCONNECTING, connectState);
|
var newState = CompareExchangeConnectState(ConnectState.Disconnecting, connectState);
|
||||||
if (newState != connectState)
|
if (newState == connectState)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -822,5 +827,10 @@ namespace MQTTnet.Client
|
|||||||
}
|
}
|
||||||
} while (true);
|
} while (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ConnectState CompareExchangeConnectState(ConnectState value, ConnectState comparand)
|
||||||
|
{
|
||||||
|
return (ConnectState)Interlocked.CompareExchange(ref _connectState, (int)value, (int)comparand);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user