Skip to content

Commit

Permalink
Set DisconnectSource to SERVER when ConnAck contained an error code
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Jan 24, 2020
1 parent 2e91fc0 commit a2a6c4c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.hivemq.client.internal.netty.DefaultChannelOutboundHandler;
import com.hivemq.client.internal.util.Checks;
import com.hivemq.client.mqtt.exceptions.ConnectionFailedException;
import com.hivemq.client.mqtt.lifecycle.MqttDisconnectSource;
import com.hivemq.client.mqtt.mqtt5.Mqtt5ClientConfig;
import com.hivemq.client.mqtt.mqtt5.auth.Mqtt5EnhancedAuthMechanism;
import com.hivemq.client.mqtt.mqtt5.exceptions.Mqtt5AuthException;
Expand Down Expand Up @@ -132,8 +133,9 @@ private void readConnAckError(final @NotNull ChannelHandlerContext ctx, final @N
callMechanism(() -> authMechanism.onAuthRejected(clientConfig, connAck));
state = MqttAuthState.NONE;

MqttDisconnectUtil.close(ctx.channel(),
new Mqtt5ConnAckException(connAck, "Connection failed. CONNACK contained Error Code."));
MqttDisconnectUtil.fireDisconnectEvent(ctx.channel(), new Mqtt5ConnAckException(connAck,
"CONNECT failed as CONNACK contained an Error Code: " + connAck.getReasonCode() + "."),
MqttDisconnectSource.SERVER);
}

private void readConnAckSuccess(final @NotNull ChannelHandlerContext ctx, final @NotNull MqttConnAck connAck) {
Expand Down Expand Up @@ -198,8 +200,8 @@ void readAuthSuccess(final @NotNull ChannelHandlerContext ctx, final @NotNull Mq
*/
@Override
void readReAuth(final @NotNull ChannelHandlerContext ctx, final @NotNull MqttAuth auth) {
MqttDisconnectUtil.disconnect(ctx.channel(), Mqtt5DisconnectReasonCode.PROTOCOL_ERROR, new Mqtt5AuthException(
auth,
MqttDisconnectUtil.disconnect(ctx.channel(), Mqtt5DisconnectReasonCode.PROTOCOL_ERROR,
new Mqtt5AuthException(auth,
"Must not receive AUTH with reason code REAUTHENTICATE during connect auth."));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.hivemq.client.mqtt.MqttVersion;
import com.hivemq.client.mqtt.lifecycle.MqttClientConnectedContext;
import com.hivemq.client.mqtt.lifecycle.MqttClientConnectedListener;
import com.hivemq.client.mqtt.lifecycle.MqttDisconnectSource;
import com.hivemq.client.mqtt.mqtt5.exceptions.Mqtt5ConnAckException;
import com.hivemq.client.mqtt.mqtt5.message.disconnect.Mqtt5DisconnectReasonCode;
import io.netty.channel.Channel;
Expand Down Expand Up @@ -157,8 +158,9 @@ public void channelRead(final @NotNull ChannelHandlerContext ctx, final @NotNull
*/
private void readConnAck(final @NotNull MqttConnAck connAck, final @NotNull Channel channel) {
if (connAck.getReasonCode().isError()) {
MqttDisconnectUtil.close(channel, new Mqtt5ConnAckException(connAck,
"CONNECT failed as CONNACK contained an Error Code: " + connAck.getReasonCode() + "."));
MqttDisconnectUtil.fireDisconnectEvent(channel, new Mqtt5ConnAckException(connAck,
"CONNECT failed as CONNACK contained an Error Code: " + connAck.getReasonCode() + "."),
MqttDisconnectSource.SERVER);

} else if (validateClientIdentifier(connAck, channel)) {
final MqttClientConnectionConfig connectionConfig = addConnectionConfig(connAck, channel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ public final class MqttDisconnectUtil {
* Closes the channel from the client side without sending a DISCONNECT message.
*
* @param channel the channel to close.
* @param cause the cause why the channel is closed.
* @param reason the reason why the channel is closed.
*/
public static void close(final @NotNull Channel channel, final @NotNull Throwable cause) {
fireDisconnectEvent(channel, cause, MqttDisconnectSource.CLIENT);
public static void close(final @NotNull Channel channel, final @NotNull String reason) {
fireDisconnectEvent(channel, new ConnectionClosedException(reason), MqttDisconnectSource.CLIENT);
}

/**
* Closes the channel from the client side without sending a DISCONNECT message.
*
* @param channel the channel to close.
* @param reason the reason why the channel is closed.
* @param cause the cause why the channel is closed.
*/
public static void close(final @NotNull Channel channel, final @NotNull String reason) {
fireDisconnectEvent(channel, new ConnectionClosedException(reason), MqttDisconnectSource.CLIENT);
public static void close(final @NotNull Channel channel, final @NotNull Throwable cause) {
fireDisconnectEvent(channel, cause, MqttDisconnectSource.CLIENT);
}

/**
Expand Down Expand Up @@ -87,7 +87,7 @@ public static void disconnect(
fireDisconnectEvent(channel, new Mqtt5DisconnectException(disconnect, cause), MqttDisconnectSource.CLIENT);
}

static void fireDisconnectEvent(
public static void fireDisconnectEvent(
final @NotNull Channel channel, final @NotNull Throwable cause,
final @NotNull MqttDisconnectSource source) {

Expand Down

0 comments on commit a2a6c4c

Please sign in to comment.