Skip to content

Commit

Permalink
Merge pull request #1 from mdickens/master
Browse files Browse the repository at this point in the history
mqtt-packet side of fix for MQTTjs issue 235
  • Loading branch information
mcollina committed Jan 18, 2015
2 parents 7e47307 + b826eaf commit 02593ab
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
3 changes: 3 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ module.exports.RETAIN_MASK = 0x01;
module.exports.LENGTH_MASK = 0x7F;
module.exports.LENGTH_FIN_MASK = 0x80;

/* Connack */
module.exports.SESSIONPRESENT_MASK = 0x01;

/* Connect */
module.exports.USERNAME_MASK = 0x80;
module.exports.PASSWORD_MASK = 0x40;
Expand Down
3 changes: 2 additions & 1 deletion generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ function connack(opts) {

buffer.writeUInt8(protocol.codes['connack'] << protocol.CMD_SHIFT, pos++);
pos += writeLength(buffer, pos, 2);
pos += writeNumber(buffer, pos, rc);
buffer.writeUInt8(opts.sessionPresent & protocol.SESSIONPRESENT_MASK, pos++);
buffer.writeUInt8(rc, pos++);

return buffer;
}
Expand Down
4 changes: 2 additions & 2 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ Parser.prototype._parseConnect = function () {

Parser.prototype._parseConnack = function () {
var packet = this.packet

packet.returnCode = this._parseNum()
packet.sessionPresent = this._list.readUInt8(this._pos++) & constants.SESSIONPRESENT_MASK
packet.returnCode = this._list.readUInt8(this._pos)
if(packet.returnCode === -1)
return this.emit('error', new Error('cannot parse return code'))
}
Expand Down
18 changes: 18 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,31 @@ testParseGenerate('connack with return code 0', {
, qos: 0
, dup: false
, length: 2
, sessionPresent: 0
, returnCode: 0
}, new Buffer([
32, 2, 0, 0
]))

testParseGenerate('connack with return code 0 session present bit set', {
cmd: 'connack'
, retain: false
, qos: 0
, dup: false
, length: 2
, sessionPresent: 1
, returnCode: 0
}, new Buffer([
32, 2, 1, 0
]))

testParseGenerate('connack with return code 5', {
cmd: 'connack'
, retain: false
, qos: 0
, dup: false
, length: 2
, sessionPresent: 0
, returnCode: 5
}, new Buffer([
32, 2, 0, 5
Expand Down Expand Up @@ -758,3 +772,7 @@ testGenerateError('Invalid password', {
, username: 'username'
, password: 42
})




0 comments on commit 02593ab

Please sign in to comment.