提交 c883282f 编写于 作者: 浅梦2013's avatar 浅梦2013

优化解码异常处理

上级 cde9c6af
......@@ -80,7 +80,7 @@ public final class MqttDecoder {
Result<?> decodedVariableHeader = decodeVariableHeader(ctx, buffer, mqttFixedHeader, bytesRemainingInVariablePart);
variableHeader = decodedVariableHeader.value;
if (bytesRemainingInVariablePart > maxBytesInMessage) {
throw new DecoderException("too large message: " + bytesRemainingInVariablePart + " bytes");
throw new DecoderException("too large message: " + bytesRemainingInVariablePart + " bytes but maxBytesInMessage is " + maxBytesInMessage);
}
bytesRemainingInVariablePart -= decodedVariableHeader.numberOfBytesConsumed;
} catch (Exception cause) {
......
......@@ -20,6 +20,8 @@ import net.dreamlu.iot.mqtt.codec.*;
import org.tio.client.intf.ClientAioHandler;
import org.tio.core.ChannelContext;
import org.tio.core.TioConfig;
import org.tio.core.exception.AioDecodeException;
import org.tio.core.exception.TioDecodeException;
import org.tio.core.intf.Packet;
import java.nio.ByteBuffer;
......@@ -49,8 +51,16 @@ public class MqttClientAioHandler implements ClientAioHandler {
}
@Override
public Packet decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext) {
return mqttDecoder.decode(channelContext, buffer, limit, position, readableLength);
public Packet decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext context) throws TioDecodeException {
MqttMessage message = mqttDecoder.decode(context, buffer, limit, position, readableLength);
if (message == null) {
return null;
}
DecoderResult decoderResult = message.decoderResult();
if (decoderResult.isFailure() && decoderResult.getCause() instanceof DecoderException) {
throw new AioDecodeException(decoderResult.getCause());
}
return message;
}
@Override
......
......@@ -22,6 +22,8 @@ import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.core.TioConfig;
import org.tio.core.exception.AioDecodeException;
import org.tio.core.exception.TioDecodeException;
import org.tio.core.intf.Packet;
import org.tio.server.AcceptCompletionHandler;
import org.tio.server.intf.ServerAioHandler;
......@@ -58,8 +60,16 @@ public class MqttServerAioHandler implements ServerAioHandler {
* @return Packet
*/
@Override
public Packet decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext context) {
return mqttDecoder.decode(context, buffer, limit, position, readableLength);
public Packet decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext context) throws TioDecodeException {
MqttMessage message = mqttDecoder.decode(context, buffer, limit, position, readableLength);
if (message == null) {
return null;
}
DecoderResult decoderResult = message.decoderResult();
if (decoderResult.isFailure() && decoderResult.getCause() instanceof DecoderException) {
throw new AioDecodeException(decoderResult.getCause());
}
return message;
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册