@@ -18,7 +18,6 @@ var parseUdpRequest = require('./lib/parse_udp')
1818// Use random port above 1024
1919portfinder . basePort = Math . floor ( Math . random ( ) * 60000 ) + 1025
2020
21-
2221inherits ( Server , EventEmitter )
2322
2423/**
@@ -183,11 +182,7 @@ Server.prototype.onUdpRequest = function (msg, rinfo) {
183182 response . transactionId = params . transactionId
184183 response . connectionId = params . connectionId
185184 var buf = makeUdpPacket ( response )
186- self . _udpSocket . send ( buf , 0 , buf . length , rinfo . port , rinfo . address , function ( ) {
187- try {
188- socket . close ( )
189- } catch ( err ) { }
190- } )
185+ self . _udpSocket . send ( buf , 0 , buf . length , rinfo . port , rinfo . address )
191186 } )
192187}
193188
@@ -274,43 +269,50 @@ Server.prototype._onScrape = function (params, cb) {
274269}
275270
276271function makeUdpPacket ( params ) {
272+ var packet
277273 switch ( params . action ) {
278274 case common . ACTIONS . CONNECT :
279- return Buffer . concat ( [
275+ packet = Buffer . concat ( [
280276 common . toUInt32 ( common . ACTIONS . CONNECT ) ,
281277 common . toUInt32 ( params . transactionId ) ,
282278 params . connectionId
283279 ] )
280+ break
284281 case common . ACTIONS . ANNOUNCE :
285- return Buffer . concat ( [
282+ packet = Buffer . concat ( [
286283 common . toUInt32 ( common . ACTIONS . ANNOUNCE ) ,
287284 common . toUInt32 ( params . transactionId ) ,
288285 common . toUInt32 ( params . interval ) ,
289286 common . toUInt32 ( params . incomplete ) ,
290287 common . toUInt32 ( params . complete ) ,
291288 params . peers
292289 ] )
290+ break
293291 case common . ACTIONS . SCRAPE :
294292 var firstInfoHash = Object . keys ( params . files ) [ 0 ]
295293 var scrapeInfo = firstInfoHash ? {
296294 complete : params . files [ firstInfoHash ] . complete ,
297295 incomplete : params . files [ firstInfoHash ] . incomplete ,
298296 completed : params . files [ firstInfoHash ] . complete // TODO: this only provides a lower-bound
299297 } : { }
300- return Buffer . concat ( [
298+ packet = Buffer . concat ( [
301299 common . toUInt32 ( common . ACTIONS . SCRAPE ) ,
302300 common . toUInt32 ( params . transactionId ) ,
303301 common . toUInt32 ( scrapeInfo . complete ) ,
304302 common . toUInt32 ( scrapeInfo . completed ) ,
305303 common . toUInt32 ( scrapeInfo . incomplete )
306304 ] )
305+ break
307306 case common . ACTIONS . ERROR :
308- return Buffer . concat ( [
307+ packet = Buffer . concat ( [
309308 common . toUInt32 ( common . ACTIONS . ERROR ) ,
310309 common . toUInt32 ( params . transactionId || 0 ) ,
311310 new Buffer ( params . message , 'utf8' )
312311 ] )
313- default :
314- throw new Error ( 'Action not implemented: ' + params . action )
312+ break
313+ default :
314+ throw new Error ( 'Action not implemented: ' + params . action )
315+ break
315316 }
317+ return packet
316318}
0 commit comments