@@ -62,8 +62,8 @@ export class FastTracker implements Tracker {
6262 }
6363
6464 private processAnnounce ( json : any , peer : InternalPeerContext , completed : boolean = false ) {
65- const infoHash : string = json . info_hash ;
66- const peerId : string = json . peer_id ;
65+ const infoHash = json . info_hash ;
66+ const peerId = json . peer_id ;
6767
6868 let swarmContext : SwarmContext | undefined ;
6969
@@ -82,28 +82,7 @@ export class FastTracker implements Tracker {
8282 }
8383
8484 if ( swarmContext === undefined ) {
85- let swarm = this . swarms . get ( infoHash ) ;
86-
87- if ( swarm === undefined ) {
88- if ( typeof infoHash !== "string" ) {
89- throw new TrackerError ( "announce: info_hash field is missing or wrong" ) ;
90- }
91-
92- ldebug ( ( ) => [ "announce: swarm created:" , Buffer . from ( infoHash ) . toString ( "hex" ) ] ) ;
93- swarm = new Swarm ( infoHash ) ;
94- this . swarms . set ( infoHash , swarm ) ;
95- }
96-
97- ldebug ( ( ) => [ "announce: peer" , Buffer . from ( peerId ) . toString ( "hex" ) , "added to swarm" , Buffer . from ( infoHash ) . toString ( "hex" ) ] ) ;
98-
99- const previousPeer = swarm . peers . get ( peerId ) ;
100- if ( previousPeer !== undefined ) {
101- removePeerFromSwarm ( previousPeer , infoHash ) ;
102- }
103-
104- swarm . addPeer ( peer ) ;
105- swarmContext = { completed : false , swarm : swarm } ;
106- peer . swarms ! . push ( swarmContext ) ;
85+ swarmContext = this . addPeerToSwarm ( peer , infoHash ) ;
10786 }
10887
10988 const swarm = swarmContext . swarm ;
@@ -122,49 +101,70 @@ export class FastTracker implements Tracker {
122101 incomplete : swarm . peers . size - swarm . completedCount
123102 } ) ;
124103
125- const offers : any [ ] | undefined = json . offers ;
126- if ( offers == undefined ) {
104+ this . sendOffersToPeers ( json , swarmPeers , peer , infoHash ) ;
105+ }
106+
107+ private addPeerToSwarm ( peer : InternalPeerContext , infoHash : any ) {
108+ let swarm = this . swarms . get ( infoHash ) ;
109+
110+ if ( swarm === undefined ) {
111+ if ( typeof infoHash !== "string" ) {
112+ throw new TrackerError ( "announce: info_hash field is missing or wrong" ) ;
113+ }
114+
115+ ldebug ( ( ) => [ "announce: swarm created:" , Buffer . from ( infoHash ) . toString ( "hex" ) ] ) ;
116+ swarm = new Swarm ( infoHash ) ;
117+ this . swarms . set ( infoHash , swarm ) ;
118+ }
119+
120+ ldebug ( ( ) => [ "announce: peer" , Buffer . from ( peer . id ! ) . toString ( "hex" ) , "added to swarm" , Buffer . from ( infoHash ) . toString ( "hex" ) ] ) ;
121+
122+ const previousPeer = swarm . peers . get ( peer . id ! ) ;
123+ if ( previousPeer !== undefined ) {
124+ removePeerFromSwarm ( previousPeer , infoHash ) ;
125+ }
126+
127+ swarm . addPeer ( peer ) ;
128+ const swarmContext = { completed : false , swarm : swarm } ;
129+ peer . swarms ! . push ( swarmContext ) ;
130+
131+ return swarmContext ;
132+ }
133+
134+ private sendOffersToPeers ( json : any , peers : ReadonlyArray < InternalPeerContext > , peer : InternalPeerContext , infoHash : string ) {
135+ if ( peers . length <= 1 ) {
127136 return ;
128- } else if ( ! ( offers instanceof Array ) ) {
129- throw new TrackerError ( "announce: offers field is not an array" ) ;
130137 }
131138
132- if ( swarmPeers . length <= 1 ) {
139+ const offers : any = json . offers ;
140+ if ( offers == undefined ) {
133141 return ;
142+ } else if ( ! ( offers instanceof Array ) ) {
143+ throw new TrackerError ( "announce: offers field is not an array" ) ;
134144 }
135145
136146 const numwant = json . numwant ;
137147 if ( ! Number . isInteger ( numwant ) ) {
138148 return ;
139149 }
140150
141- const countPeersToSend = swarmPeers . length - 1 ;
151+ const countPeersToSend = peers . length - 1 ;
142152 const countOffersToSend = Math . min ( countPeersToSend , offers . length , this . settings . maxOffers , numwant ) ;
143153
144- if ( countOffersToSend == countPeersToSend ) {
145- // we have offers for all the peers from the swarm - send offers to all
146- const offersIterator = offers . values ( ) ;
147- for ( const toPeer of swarmPeers ) {
148- if ( toPeer !== peer ) {
149- sendOffer ( offersIterator . next ( ) . value , peerId , toPeer , infoHash ) ;
150- }
151- }
152- } else {
153- let peerIndex = Math . floor ( Math . random ( ) * swarmPeers . length ) ;
154- // send offers to random peers
155- for ( let i = 0 ; i < countOffersToSend ; i ++ ) {
156- const toPeer = swarmPeers [ peerIndex ] ;
154+ let peerIndex = ( countOffersToSend == countPeersToSend ) ? 0 : Math . floor ( Math . random ( ) * peers . length ) ;
157155
158- if ( toPeer === peer ) {
159- i -- ; // do one more iteration
160- } else {
161- sendOffer ( offers [ i ] , peerId , toPeer , infoHash ) ;
162- }
156+ for ( let i = 0 ; i < countOffersToSend ; i ++ ) {
157+ const toPeer = peers [ peerIndex ] ;
163158
164- peerIndex ++ ;
165- if ( peerIndex == swarmPeers . length ) {
166- peerIndex = 0 ;
167- }
159+ if ( toPeer === peer ) {
160+ i -- ; // do one more iteration
161+ } else {
162+ sendOffer ( offers [ i ] , peer . id ! , toPeer , infoHash ) ;
163+ }
164+
165+ peerIndex ++ ;
166+ if ( peerIndex == peers . length ) {
167+ peerIndex = 0 ;
168168 }
169169 }
170170
0 commit comments