Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all 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
99 changes: 49 additions & 50 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ function Client (broker, conn) {
return
}

var buf = empty
var client = that

if (client._paused) {
Expand All @@ -62,6 +61,7 @@ function Client (broker, conn) {
that._parsingBatch--
if (that._parsingBatch <= 0) {
that._parsingBatch = 0
var buf = empty
buf = client.conn.read(null)

if (buf) {
Expand All @@ -82,22 +82,8 @@ function Client (broker, conn) {
conn.on('end', this.close.bind(this))
this._eos = eos(this.conn, this.close.bind(this))

function dedupe (packet) {
var duplicates = that.duplicates
var id = packet.brokerId
if (!id) {
return true
}
var counter = packet.brokerCounter
var result = (duplicates[id] || 0) < counter
if (result) {
duplicates[id] = counter
}
return result
}

this.deliver0 = function deliverQoS0 (_packet, cb) {
var toForward = dedupe(_packet) &&
var toForward = dedupe(that, _packet) &&
that.broker.authorizeForward(that, _packet)
if (toForward) {
var packet = new Packet(toForward, broker)
Expand All @@ -112,30 +98,30 @@ function Client (broker, conn) {
// downgrade to qos0 if requested by publish
if (_packet.qos === 0) {
that.deliver0(_packet, cb)
} else {
var toForward = dedupe(_packet) &&
that.broker.authorizeForward(that, _packet)
if (toForward) {
var packet = new QoSPacket(toForward, that)
// Downgrading to client subscription qos if needed
var clientSub = that.subscriptions[packet.topic]
if (clientSub && (clientSub.qos || 0) < packet.qos) {
packet.qos = clientSub.qos
}
packet.writeCallback = cb
if (that.clean || packet.retain) {
writeQoS(null, that, packet)
} else {
broker.persistence.outgoingUpdate(that, packet, writeQoS)
}
} else if (that.clean === false) {
that.broker.persistence.outgoingClearMessageId(that, _packet, nop)
// we consider this to be an error, since the packet is undefined
// so there's nothing to send
cb()
return
}
var toForward = dedupe(that, _packet) &&
that.broker.authorizeForward(that, _packet)
if (toForward) {
var packet = new QoSPacket(toForward, that)
// Downgrading to client subscription qos if needed
var clientSub = that.subscriptions[packet.topic]
if (clientSub && (clientSub.qos || 0) < packet.qos) {
packet.qos = clientSub.qos
}
packet.writeCallback = cb
if (that.clean || packet.retain) {
writeQoS(null, that, packet)
} else {
cb()
broker.persistence.outgoingUpdate(that, packet, writeQoS)
}
} else if (that.clean === false) {
that.broker.persistence.outgoingClearMessageId(that, _packet, nop)
// we consider this to be an error, since the packet is undefined
// so there's nothing to send
cb()
} else {
cb()
}
}

Expand All @@ -147,6 +133,20 @@ function Client (broker, conn) {
}, broker.connectTimeout)
}

function dedupe (client, packet) {
var id = packet.brokerId
if (!id) {
return true
}
var duplicates = client.duplicates
var counter = packet.brokerCounter
var result = (duplicates[id] || 0) < counter
if (result) {
duplicates[id] = counter
}
return result
}

function writeQoS (err, client, packet) {
if (err) {
// is this right, or we should ignore thins?
Expand All @@ -160,11 +160,7 @@ function onError (err) {
this.errored = true
this.conn.removeAllListeners('error')
this.conn.on('error', nop)
if (this.id) {
this.broker.emit('clientError', this, err)
} else {
this.broker.emit('connectionError', this, err)
}
this.broker.emit(this.id ? 'clientError' : 'connectionError', this, err)
this.close()
}

Expand All @@ -177,8 +173,10 @@ Client.prototype.publish = function (message, done) {
var that = this
if (packet.qos === 0) {
// skip offline and send it as it is
this.deliver0(packet, done || nop)
} else if (!this.clean && this.id) {
this.deliver0(packet, done)
return
}
if (!this.clean && this.id) {
this.broker.persistence.outgoingEnqueue({
clientId: this.id
}, packet, function deliver (err) {
Expand Down Expand Up @@ -309,13 +307,14 @@ Client.prototype.resume = function () {
}

function enqueue (packet) {
this.client._parsingBatch++
var client = this.client
client._parsingBatch++
// already connected or it's the first packet
if (this.client.connackSent || this.client._parsingBatch === 1) {
handle(this.client, packet, this.client._nextBatch)
if (client.connackSent || client._parsingBatch === 1) {
handle(client, packet, client._nextBatch)
} else {
this.client.on('connected', () => {
handle(this.client, packet, this.client._nextBatch)
client.on('connected', () => {
handle(client, packet, client._nextBatch)
})
}
}
Expand Down