Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize handle function #297

Merged
merged 5 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions lib/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,16 @@ var handlePubrec = require('./pubrec')
var handlePing = require('./ping')

function handle (client, packet, done) {
if (
// [MQTT-3.1.0-1]
(packet.cmd !== 'connect' && !client.connected) ||
if (packet.cmd === 'connect') {
// [MQTT-3.1.0-2]
(packet.cmd === 'connect' && client.connected)
) {
client.conn.destroy()
return
return client.connected ? client.conn.destroy() : handleConnect(client, packet, done)
}

if (packet.cmd !== 'connect' && client._keepaliveInterval > 0) {
client._keepaliveTimer.reschedule(client._keepaliveInterval)
if (!client.connected) {
// [MQTT-3.1.0-1]
return client.conn.destroy()
}

switch (packet.cmd) {
case 'connect':
handleConnect(client, packet, done)
break
case 'publish':
handlePublish(client, packet, done)
break
Expand All @@ -53,9 +45,14 @@ function handle (client, packet, done) {
case 'disconnect':
client.disconnected = true
client.conn.end()
break
return
default:
client.conn.destroy()
return
}

if (client._keepaliveInterval > 0) {
client._keepaliveTimer.reschedule(client._keepaliveInterval)
}
}

Expand Down
8 changes: 6 additions & 2 deletions test/handlers/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var test = require('tape').test
var EE = require('events').EventEmitter
var handleConnect = require('../../lib/handlers/connect')
var handle = require('../../lib/handlers/index')

test('reject clients with no clientId running on MQTT 3.1', function (t) {
t.plan(2)
Expand All @@ -13,12 +13,16 @@ test('reject clients with no clientId running on MQTT 3.1', function (t) {
}

client.broker = broker
client.conn = {
destroy: function () {}
gnought marked this conversation as resolved.
Show resolved Hide resolved
}

client.on('error', function (err) {
t.equal(err.message, 'Empty clientIds are supported only on MQTT 3.1.1', 'error message')
})

handleConnect(client, {
handle(client, {
cmd: 'connect',
protocolVersion: 3,
protocolId: 'MQIsdp'
}, function (err) {
Expand Down