Skip to content

Commit

Permalink
Issue87 (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
shamblett authored Feb 23, 2024
1 parent 05ed633 commit 79fa087
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions lib/src/connectionhandling/mqtt_connection_handler_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,9 @@ abstract class MqttConnectionHandlerBase implements MqttIConnectionHandler {
try {
final ackMsg = msg as MqttConnectAckMessage;
// Drop the connection if our connect request has been rejected.
if (MqttReasonCodeUtilities.isError(
mqttConnectReasonCode.asInt(ackMsg.variableHeader!.reasonCode)!)) {
if (!_reasonCodeOk(msg)) {
MqttLogger.log('MqttConnectionHandlerBase::_connectAckProcessor '
'connection rejected, reason code is ${mqttConnectReasonCode.asString(ackMsg.variableHeader!.reasonCode)}');
connectionStatus.reasonCode = ackMsg.variableHeader!.reasonCode;
connectionStatus.reasonString = ackMsg.variableHeader!.reasonString;
'- reason code check failed, disconnecting');
_performConnectionDisconnect();
} else {
// Initialize the keepalive to start the ping based keepalive process.
Expand Down Expand Up @@ -312,4 +309,41 @@ abstract class MqttConnectionHandlerBase implements MqttIConnectionHandler {
.on<MqttConnectAckMessageAvailable>()
.listen(connectAckReceived);
}

// Reason code check, returns true if the reason code is valid and not an error
bool _reasonCodeOk(MqttConnectAckMessage msg) {
if (msg.variableHeader != null) {
final reasonCode = msg.variableHeader?.reasonCode;
if (reasonCode != null) {
final reasonCodeInt = mqttConnectReasonCode.asInt(reasonCode);
if (reasonCodeInt != null) {
connectionStatus.reasonCode = reasonCode;
connectionStatus.reasonString = msg.variableHeader?.reasonString;
if (MqttReasonCodeUtilities.isError(reasonCodeInt)) {
MqttLogger.log(
'MqttConnectionHandlerBase::_reasonCodeOk - reason code is an error '
'${mqttConnectReasonCode.asString(reasonCode)}');
return false;
} else {
MqttLogger.log(
'MqttConnectionHandlerBase::_reasonCodeOk - reason code is ok '
'${mqttConnectReasonCode.asString(reasonCode)}');
return true;
}
} else {
MqttLogger.log(
'MqttConnectionHandlerBase::_reasonCodeOk - reason code has a null integer value');
return false;
}
} else {
MqttLogger.log(
'MqttConnectionHandlerBase::_reasonCodeOk - reason code is null');
return true;
}
} else {
MqttLogger.log(
'MqttConnectionHandlerBase::_reasonCodeOk - variable header is null');
return false;
}
}
}

0 comments on commit 79fa087

Please sign in to comment.