@@ -196,17 +196,23 @@ Server.prototype.close = function (cb) {
196196 else cb ( null )
197197}
198198
199- Server . prototype . createSwarm = function ( infoHash ) {
199+ Server . prototype . createSwarm = function ( infoHash , cb ) {
200200 var self = this
201201 if ( Buffer . isBuffer ( infoHash ) ) infoHash = infoHash . toString ( 'hex' )
202- var swarm = self . torrents [ infoHash ] = new Swarm ( infoHash , self )
203- return swarm
202+
203+ process . nextTick ( function ( ) {
204+ var swarm = self . torrents [ infoHash ] = new Swarm ( infoHash , self )
205+ cb ( null , swarm )
206+ } )
204207}
205208
206- Server . prototype . getSwarm = function ( infoHash ) {
209+ Server . prototype . getSwarm = function ( infoHash , cb ) {
207210 var self = this
208211 if ( Buffer . isBuffer ( infoHash ) ) infoHash = infoHash . toString ( 'hex' )
209- return self . torrents [ infoHash ]
212+
213+ process . nextTick ( function ( ) {
214+ cb ( null , self . torrents [ infoHash ] )
215+ } )
210216}
211217
212218Server . prototype . onHttpRequest = function ( req , res , opts ) {
@@ -358,26 +364,35 @@ Server.prototype._onWebSocketRequest = function (socket, params) {
358364 if ( params . answer ) {
359365 debug ( 'got answer %s from %s' , JSON . stringify ( params . answer ) , params . peer_id )
360366
361- var swarm = self . getSwarm ( params . info_hash )
362- if ( ! swarm ) {
363- return self . emit ( 'warning' , new Error ( 'no swarm with that `info_hash`' ) )
364- }
365- var toPeer = swarm . peers [ params . to_peer_id ]
366- if ( ! toPeer ) {
367- return self . emit ( 'warning' , new Error ( 'no peer with that `to_peer_id`' ) )
368- }
367+ self . getSwarm ( params . info_hash , function ( err , swarm ) {
368+ if ( err ) return self . emit ( 'warning' , err )
369+ if ( ! swarm ) {
370+ return self . emit ( 'warning' , new Error ( 'no swarm with that `info_hash`' ) )
371+ }
372+ var toPeer = swarm . peers [ params . to_peer_id ]
373+ if ( ! toPeer ) {
374+ return self . emit ( 'warning' , new Error ( 'no peer with that `to_peer_id`' ) )
375+ }
376+
377+ toPeer . socket . send ( JSON . stringify ( {
378+ answer : params . answer ,
379+ offer_id : params . offer_id ,
380+ peer_id : common . hexToBinary ( params . peer_id ) ,
381+ info_hash : common . hexToBinary ( params . info_hash )
382+ } ) , toPeer . socket . onSend )
383+ debug ( 'sent answer to %s from %s' , toPeer . peerId , params . peer_id )
369384
370- toPeer . socket . send ( JSON . stringify ( {
371- answer : params . answer ,
372- offer_id : params . offer_id ,
373- peer_id : common . hexToBinary ( params . peer_id ) ,
374- info_hash : common . hexToBinary ( params . info_hash )
375- } ) , toPeer . socket . onSend )
376- debug ( 'sent answer to %s from %s' , toPeer . peerId , params . peer_id )
385+ done ( )
386+ } )
387+ } else {
388+ done ( )
377389 }
378390
379- if ( params . action === common . ACTIONS . ANNOUNCE ) {
380- self . emit ( common . EVENT_NAMES [ params . event ] , params . peer_id , params )
391+ function done ( ) {
392+ // emit event once the announce is fully "processed"
393+ if ( params . action === common . ACTIONS . ANNOUNCE ) {
394+ self . emit ( common . EVENT_NAMES [ params . event ] , params . peer_id , params )
395+ }
381396 }
382397 } )
383398}
@@ -398,9 +413,14 @@ Server.prototype._onRequest = function (params, cb) {
398413Server . prototype . _onAnnounce = function ( params , cb ) {
399414 var self = this
400415
401- var swarm = self . getSwarm ( params . info_hash )
402- if ( swarm ) announce ( )
403- else createSwarm ( )
416+ self . getSwarm ( params . info_hash , function ( err , swarm ) {
417+ if ( err ) return cb ( err )
418+ if ( swarm ) {
419+ announce ( swarm )
420+ } else {
421+ createSwarm ( )
422+ }
423+ } )
404424
405425 function createSwarm ( ) {
406426 if ( self . _filter ) {
@@ -410,17 +430,21 @@ Server.prototype._onAnnounce = function (params, cb) {
410430 } else if ( ! allowed ) {
411431 cb ( new Error ( 'disallowed info_hash' ) )
412432 } else {
413- swarm = self . createSwarm ( params . info_hash )
414- announce ( )
433+ self . createSwarm ( params . info_hash , function ( err , swarm ) {
434+ if ( err ) return cb ( err )
435+ announce ( swarm )
436+ } )
415437 }
416438 } )
417439 } else {
418- swarm = self . createSwarm ( params . info_hash )
419- announce ( )
440+ self . createSwarm ( params . info_hash , function ( err , swarm ) {
441+ if ( err ) return cb ( err )
442+ announce ( swarm )
443+ } )
420444 }
421445 }
422446
423- function announce ( ) {
447+ function announce ( swarm ) {
424448 if ( ! params . event || params . event === 'empty' ) params . event = 'update'
425449 swarm . announce ( params , function ( err , response ) {
426450 if ( err ) return cb ( err )
@@ -470,19 +494,21 @@ Server.prototype._onScrape = function (params, cb) {
470494
471495 series ( params . info_hash . map ( function ( infoHash ) {
472496 return function ( cb ) {
473- var swarm = self . getSwarm ( infoHash )
474- if ( swarm ) {
475- swarm . scrape ( params , function ( err , scrapeInfo ) {
476- if ( err ) return cb ( err )
477- cb ( null , {
478- infoHash : infoHash ,
479- complete : ( scrapeInfo && scrapeInfo . complete ) || 0 ,
480- incomplete : ( scrapeInfo && scrapeInfo . incomplete ) || 0
497+ self . getSwarm ( infoHash , function ( err , swarm ) {
498+ if ( err ) return cb ( err )
499+ if ( swarm ) {
500+ swarm . scrape ( params , function ( err , scrapeInfo ) {
501+ if ( err ) return cb ( err )
502+ cb ( null , {
503+ infoHash : infoHash ,
504+ complete : ( scrapeInfo && scrapeInfo . complete ) || 0 ,
505+ incomplete : ( scrapeInfo && scrapeInfo . incomplete ) || 0
506+ } )
481507 } )
482- } )
483- } else {
484- cb ( null , { infoHash : infoHash , complete : 0 , incomplete : 0 } )
485- }
508+ } else {
509+ cb ( null , { infoHash : infoHash , complete : 0 , incomplete : 0 } )
510+ }
511+ } )
486512 }
487513 } ) , function ( err , results ) {
488514 if ( err ) return cb ( err )
0 commit comments