Changed decodable message method to force loading the complete size of a message, before starting the real decode phase

This commit is contained in:
andrea
2012-08-26 13:38:11 +02:00
parent b5e673d209
commit 758301149a
4 changed files with 27 additions and 6 deletions
@@ -37,7 +37,7 @@ public class Utils {
* Decode the variable remaining lenght as defined in MQTT v3.1 specification * Decode the variable remaining lenght as defined in MQTT v3.1 specification
* (section 2.1). * (section 2.1).
* *
* @return the decoded length or -1 if needed more data to decode. * @return the decoded length or -1 if needed more data to decode the length field.
*/ */
static int decodeRemainingLenght(IoBuffer in) { static int decodeRemainingLenght(IoBuffer in) {
int multiplier = 1; int multiplier = 1;
@@ -87,6 +87,17 @@ public class Utils {
} }
byte h1 = in.get(); byte h1 = in.get();
byte messageType = (byte) ((h1 & 0x00F0) >> 4); byte messageType = (byte) ((h1 & 0x00F0) >> 4);
int remainingLength = Utils.decodeRemainingLenght(in);
if (remainingLength == -1) {
return MessageDecoderResult.NEED_DATA;
}
//check remaining length
if (in.remaining() < remainingLength) {
return MessageDecoderResult.NEED_DATA;
}
return messageType == type ? MessageDecoderResult.OK : MessageDecoderResult.NOT_OK; return messageType == type ? MessageDecoderResult.OK : MessageDecoderResult.NOT_OK;
} }
@@ -27,13 +27,17 @@ public class ConnAckDecoderTest {
@Test @Test
public void testDecodable_OK() { public void testDecodable_OK() {
m_buff.put((byte)(AbstractMessage.CONNACK << 4)).flip(); m_buff.put((byte)(AbstractMessage.CONNACK << 4))
.put((byte)0) //0 length
.flip();
assertEquals(MessageDecoder.OK , m_msgdec.decodable(null, m_buff)); assertEquals(MessageDecoder.OK , m_msgdec.decodable(null, m_buff));
} }
@Test @Test
public void testDecodable_NOT_OK() { public void testDecodable_NOT_OK() {
m_buff.put((byte)(AbstractMessage.CONNECT << 4)).flip(); m_buff.put((byte)(AbstractMessage.CONNECT << 4))
.put((byte)0) //0 length
.flip();
assertEquals(MessageDecoder.NOT_OK , m_msgdec.decodable(null, m_buff)); assertEquals(MessageDecoder.NOT_OK , m_msgdec.decodable(null, m_buff));
} }
@@ -26,13 +26,17 @@ public class ConnectDecoderTest {
@Test @Test
public void testDecodable_OK() { public void testDecodable_OK() {
m_buff.put((byte)(AbstractMessage.CONNECT << 4)).flip(); m_buff.put((byte)(AbstractMessage.CONNECT << 4))
.put((byte)0) //0 length
.flip();
assertEquals(MessageDecoder.OK , m_msgdec.decodable(null, m_buff)); assertEquals(MessageDecoder.OK , m_msgdec.decodable(null, m_buff));
} }
@Test @Test
public void testDecodable_NOT_OK() { public void testDecodable_NOT_OK() {
m_buff.put((byte)(AbstractMessage.CONNACK << 4)).flip(); m_buff.put((byte)(AbstractMessage.CONNACK << 4))
.put((byte)0) //0 length
.flip();
assertEquals(MessageDecoder.NOT_OK , m_msgdec.decodable(null, m_buff)); assertEquals(MessageDecoder.NOT_OK , m_msgdec.decodable(null, m_buff));
} }
@@ -29,7 +29,9 @@ public class PublishDecoderTest {
@Test @Test
public void testDecodable_OK() { public void testDecodable_OK() {
m_buff.put((byte) (AbstractMessage.PUBLISH << 4)).flip(); m_buff.put((byte) (AbstractMessage.PUBLISH << 4))
.put((byte)0) //0 length
.flip();
assertEquals(MessageDecoder.OK, m_msgdec.decodable(null, m_buff)); assertEquals(MessageDecoder.OK, m_msgdec.decodable(null, m_buff));
} }