Skip to content

Commit 1721eed

Browse files
committed
udp client: unify error handling
1 parent b406d4e commit 1721eed

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,31 +147,37 @@ Client.prototype._requestUdp = function (announceUrl, opts) {
147147
var transactionId = new Buffer(hat(32), 'hex')
148148

149149
var timeout = setTimeout(function () {
150+
error('tracker request timed out')
151+
}, 15000)
152+
153+
function error (message) {
154+
self.emit('error', new Error(message))
150155
socket.close()
151-
}, 5000)
156+
clearTimeout(timeout)
157+
}
152158

153159
socket.on('error', function (err) {
154-
self.emit('error', err)
160+
error(err)
155161
})
156162

157163
socket.on('message', function (message, rinfo) {
158164

159165
if (message.length < 8 || message.readUInt32BE(4) !== transactionId.readUInt32BE(0)) {
160-
return self.emit('error', new Error('tracker sent back invalid transaction id'))
166+
return error(new Error('tracker sent back invalid transaction id'))
161167
}
162168

163169
var action = message.readUInt32BE(0)
164170
switch (action) {
165171
case 0:
166172
if (message.length < 16) {
167-
return self.emit('error', new Error('invalid udp handshake'))
173+
return error(new Error('invalid udp handshake'))
168174
}
169175
announce(message.slice(8, 16), opts)
170176
return
171177

172178
case 1:
173179
if (message.length < 20) {
174-
return self.emit('error', new Error('invalid announce message'))
180+
return error(new Error('invalid announce message'))
175181
}
176182

177183
// TODO: this should be stored per tracker, not globally for all trackers

0 commit comments

Comments
 (0)