-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
client.js
290 lines (242 loc) · 6.43 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
'use strict'
var Buffer = require('safe-buffer').Buffer
var mqtt = require('mqtt-packet')
var EE = require('events').EventEmitter
var util = require('util')
var eos = require('end-of-stream')
var empty = Buffer.allocUnsafe(0)
var Packet = require('aedes-packet')
var write = require('./write')
var QoSPacket = require('./qos-packet')
var handleSubscribe = require('./handlers/subscribe')
var handleUnsubscribe = require('./handlers/unsubscribe')
var handle = require('./handlers')
module.exports = Client
function Client (broker, conn) {
var that = this
this.broker = broker
this.conn = conn
this.parser = mqtt.parser()
this.connected = false
this.errored = false
this.clean = true
this._handling = 0
this.subscriptions = {}
this.id = null
conn.client = this
this.parser.client = this
this.duplicates = {}
this._parsingBatch = 1
this._nextId = Math.ceil(Math.random() * 65535)
this.disconnected = false
this.parser.on('packet', enqueue)
function nextBatch (err) {
if (err) {
return that.emit('error', err)
}
var buf = empty
var client = that
if (client._paused) {
return
}
that._parsingBatch--
if (that._parsingBatch <= 0) {
that._parsingBatch = 0
buf = client.conn.read(null)
if (buf) {
client.parser.parse(buf)
}
}
}
this._nextBatch = nextBatch
nextBatch()
conn.on('readable', nextBatch)
conn.on('error', this.emit.bind(this, 'error'))
this.parser.on('error', this.emit.bind(this, 'error'))
this.on('error', onError)
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) &&
that.broker.authorizeForward(that, _packet)
if (toForward) {
var packet = new Packet(toForward, broker)
packet.qos = 0
write(that, packet, cb)
} else {
cb()
}
}
this.deliverQoS = function deliverQoS (_packet, cb) {
// 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)
packet.writeCallback = cb
if (that.clean) {
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()
} else {
cb()
}
}
}
this._keepaliveTimer = null
this._keepaliveInterval = -1
this._connectTimer = setTimeout(function () {
that.emit('error', new Error('connect did not arrive in time'))
}, broker.connectTimeout)
}
function writeQoS (err, client, packet) {
if (err) {
// is this right, or we should ignore thins?
client.emit('error', err)
} else {
write(client, packet, packet.writeCallback)
}
}
function drainRequest (req) {
req.callback()
}
function onError (err) {
this.errored = true
this.conn.removeAllListeners('error')
this.conn.on('error', nop)
this.broker.emit('clientError', this, err)
this.close()
}
util.inherits(Client, EE)
Client.prototype._onError = onError
Client.prototype.publish = function (message, done) {
var packet = new Packet(message, this.broker)
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.broker.persistence.outgoingEnqueue({
clientId: this.id
}, packet, function deliver (err) {
if (err) {
return done(err)
}
that.deliverQoS(packet, done)
})
} else {
that.deliverQoS(packet, done)
}
}
Client.prototype.subscribe = function (packet, done) {
if (!packet.subscriptions) {
if (!Array.isArray(packet)) {
packet = [packet]
}
packet = {
subscriptions: packet
}
}
handleSubscribe(this, packet, done)
}
Client.prototype.unsubscribe = function (packet, done) {
if (!packet.unsubscriptions) {
if (!Array.isArray(packet)) {
packet = [packet]
}
packet = {
unsubscriptions: packet
}
}
handleUnsubscribe(this, packet, done)
}
Client.prototype.close = function (done) {
var that = this
var conn = this.conn
if (this.connected) {
handleUnsubscribe(
this,
{
close: true,
unsubscriptions: Object.keys(this.subscriptions)
},
finish)
} else {
finish()
}
this.parser.removeAllListeners('packet')
conn.removeAllListeners('readable')
if (this._keepaliveTimer) {
this._keepaliveTimer.clear()
this._keepaliveInterval = -1
this._keepaliveTimer = null
}
if (this._connectTimer) {
clearTimeout(this._connectTimer)
this._connectTimer = null
}
this._eos()
this._eos = nop
function finish () {
if (!that.disconnected && that.will) {
that.broker.publish(that.will, that, nop)
that.will = null // this function might be called twice
}
conn.removeAllListeners('error')
conn.on('error', nop)
if (that.broker.clients[that.id]) {
that.broker.unregisterClient(that)
}
// hack to clean up the write callbacks
// supports streams2 & streams3, so node 0.10, 0.11, and iojs
var state = that.conn._writableState
var list = (state.getBuffer && state.getBuffer()) || state.buffer
list.forEach(drainRequest)
// clear up the drain event listeners
that.conn.emit('drain')
that.conn.removeAllListeners('drain')
if (conn.destroySoon) {
conn.destroySoon()
} if (conn.destroy) {
conn.destroy()
} else {
conn.end()
}
if (typeof done === 'function') {
done()
}
}
}
Client.prototype.pause = function () {
this._paused = true
}
Client.prototype.resume = function () {
this._paused = false
this._nextBatch()
}
function enqueue (packet) {
this.client._parsingBatch++
handle(this.client, packet, this.client._nextBatch)
}
function nop () {}