@@ -13,10 +13,14 @@ var string2compact = require('string2compact')
1313var dgram = require ( 'dgram' )
1414var parseUrl = require ( 'url' ) . parse
1515
16- var CONNECTION_ID = Buffer . concat ( [ fromInt32 ( 0x417 ) , fromInt32 ( 0x27101980 ) ] ) ;
17- var CONNECT = fromInt32 ( 0 ) ;
18- var ANNOUNCE = fromInt32 ( 1 ) ;
19- var EVENTS = { completed :1 , started :2 , stopped :3 } ;
16+ var CONNECTION_ID = Buffer . concat ( [ toUInt32 ( 0x417 ) , toUInt32 ( 0x27101980 ) ] )
17+ var CONNECT = toUInt32 ( 0 )
18+ var ANNOUNCE = toUInt32 ( 1 )
19+ var EVENTS = {
20+ completed : 1 ,
21+ started : 2 ,
22+ stopped : 3
23+ }
2024
2125inherits ( Client , EventEmitter )
2226
@@ -84,8 +88,61 @@ Client.prototype.setInterval = function (intervalMs) {
8488 }
8589}
8690
87- Client . prototype . _requestUdp = function ( url , opts ) {
88- var parsed = parseUrl ( url )
91+ /**
92+ * Send a request to the tracker
93+ */
94+ Client . prototype . _request = function ( opts ) {
95+ var self = this
96+ opts = extend ( {
97+ info_hash : bytewiseEncodeURIComponent ( self . _infoHash ) ,
98+ peer_id : bytewiseEncodeURIComponent ( self . _peerId ) ,
99+ port : self . _port ,
100+ left : self . _torrentLength - ( opts . downloaded || 0 ) ,
101+ compact : 1 ,
102+ numwant : self . _numWant ,
103+ uploaded : 0 , // default, user should provide real value
104+ downloaded : 0 // default, user should provide real value
105+ } , opts )
106+
107+ if ( self . _trackerId ) {
108+ opts . trackerid = self . _trackerId
109+ }
110+
111+ self . _announce . forEach ( function ( announceUrl ) {
112+ if ( announceUrl . indexOf ( 'udp:' ) === 0 ) {
113+ self . _requestUdp ( announceUrl , opts )
114+ } else {
115+ self . _requestHttp ( announceUrl , opts )
116+ }
117+ } )
118+ }
119+
120+ Client . prototype . _requestHttp = function ( announceUrl , opts ) {
121+ var self = this
122+ var url = announceUrl + '?' + querystring . stringify ( opts )
123+
124+ var req = http . get ( url , function ( res ) {
125+ var data = ''
126+ if ( res . statusCode !== 200 ) {
127+ res . resume ( ) // consume the whole stream
128+ self . emit ( 'error' , new Error ( 'Invalid response code ' + res . statusCode + ' from tracker' ) )
129+ return
130+ }
131+ res . on ( 'data' , function ( chunk ) {
132+ data += chunk
133+ } )
134+ res . on ( 'end' , function ( ) {
135+ self . _handleResponse ( data , announceUrl )
136+ } )
137+ } )
138+
139+ req . on ( 'error' , function ( err ) {
140+ self . emit ( 'error' , err )
141+ } )
142+ }
143+
144+ Client . prototype . _requestUdp = function ( announceUrl , opts ) {
145+ var parsed = parseUrl ( announceUrl )
89146 var socket = dgram . createSocket ( 'udp4' )
90147 var self = this
91148
@@ -102,29 +159,33 @@ Client.prototype._requestUdp = function(url, opts) {
102159
103160 switch ( action ) {
104161 case 0 :
105- if ( message . length < 16 ) return self . emit ( 'error' , new Error ( 'invalid udp handshake' ) )
106- announce ( message . slice ( 8 , 16 ) , opts )
107- return ;
162+ if ( message . length < 16 ) {
163+ return self . emit ( 'error' , new Error ( 'invalid udp handshake' ) )
164+ }
165+ announce ( message . slice ( 8 , 16 ) , opts )
166+ return
108167
109168 case 1 :
110- if ( message . length < 20 ) return self . emit ( 'error' , new Error ( 'invalid announce message' ) )
169+ if ( message . length < 20 ) {
170+ return self . emit ( 'error' , new Error ( 'invalid announce message' ) )
171+ }
111172
112- self . emit ( 'update' , {
113- announce : url ,
114- complete : message . readUInt32BE ( 16 ) ,
115- incomplete : message . readUInt32BE ( 12 )
116- } )
173+ self . emit ( 'update' , {
174+ announce : announceUrl ,
175+ complete : message . readUInt32BE ( 16 ) ,
176+ incomplete : message . readUInt32BE ( 12 )
177+ } )
117178
118- for ( var i = 20 ; i < message . length ; i += 6 ) {
119- self . emit ( 'peer' , compact2string ( message . slice ( i , i + 6 ) ) )
120- }
179+ for ( var i = 20 ; i < message . length ; i += 6 ) {
180+ self . emit ( 'peer' , compact2string ( message . slice ( i , i + 6 ) ) )
181+ }
121182
122- clearTimeout ( timeout )
123- socket . close ( )
183+ clearTimeout ( timeout )
184+ socket . close ( )
124185 }
125- } ) ;
186+ } )
126187
127- function announce ( connectionId , opts ) {
188+ function announce ( connectionId , opts ) {
128189 opts = opts || { }
129190
130191 send ( Buffer . concat ( [
@@ -133,76 +194,29 @@ Client.prototype._requestUdp = function(url, opts) {
133194 new Buffer ( hat ( 32 ) , 'hex' ) ,
134195 new Buffer ( self . _infoHash , 'hex' ) ,
135196 new Buffer ( self . _peerId , 'utf-8' ) ,
136- fromInt32 ( 0 ) , fromInt32 ( opts . downloaded || 0 ) , // fromUint32(0) to expand this to 64bit
137- fromInt32 ( 0 ) , fromInt32 ( opts . left || 0 ) ,
138- fromInt32 ( 0 ) , fromInt32 ( opts . uploaded || 0 ) ,
139- fromInt32 ( EVENTS [ opts . event ] || 0 ) ,
140- fromInt32 ( 0 ) ,
141- fromInt32 ( 0 ) ,
142- fromInt32 ( self . _numWant ) ,
143- fromInt16 ( self . _port || 0 )
144- ] ) ) ;
145- } ;
146-
147- function send ( message ) {
197+ toUInt32 ( 0 ) , toUInt32 ( opts . downloaded || 0 ) , // fromUint32(0) to expand this to 64bit
198+ toUInt32 ( 0 ) , toUInt32 ( opts . left || 0 ) ,
199+ toUInt32 ( 0 ) , toUInt32 ( opts . uploaded || 0 ) ,
200+ toUInt32 ( EVENTS [ opts . event ] || 0 ) ,
201+ toUInt32 ( 0 ) ,
202+ toUInt32 ( 0 ) ,
203+ toUInt32 ( self . _numWant ) ,
204+ toUInt16 ( self . _port || 0 )
205+ ] ) )
206+ }
207+
208+ function send ( message ) {
148209 socket . send ( message , 0 , message . length , parsed . port , parsed . hostname )
149- } ;
210+ }
150211
151212 send ( Buffer . concat ( [
152213 CONNECTION_ID ,
153214 CONNECT ,
154215 new Buffer ( hat ( 32 ) , 'hex' )
155- ] ) ) ;
156- } ;
157-
158- /**
159- * Send a request to the tracker
160- */
161- Client . prototype . _request = function ( opts ) {
162- var self = this
163- opts = extend ( {
164- info_hash : bytewiseEncodeURIComponent ( self . _infoHash ) ,
165- peer_id : bytewiseEncodeURIComponent ( self . _peerId ) ,
166- port : self . _port ,
167- left : self . _torrentLength - ( opts . downloaded || 0 ) ,
168- compact : 1 ,
169- numwant : self . _numWant ,
170- uploaded : 0 , // default, user should provide real value
171- downloaded : 0 // default, user should provide real value
172- } , opts )
173-
174- if ( self . _trackerId ) {
175- opts . trackerid = self . _trackerId
176- }
177-
178- var q = querystring . stringify ( opts )
179-
180- self . _announce . forEach ( function ( announce ) {
181- if ( announce . indexOf ( 'udp:' ) === 0 ) return self . _requestUdp ( announce , opts )
182-
183- var url = announce + '?' + q
184- var req = http . get ( url , function ( res ) {
185- var data = ''
186- if ( res . statusCode !== 200 ) {
187- res . resume ( ) // consume the whole stream
188- self . emit ( 'error' , new Error ( 'Invalid response code ' + res . statusCode + ' from tracker' ) )
189- return
190- }
191- res . on ( 'data' , function ( chunk ) {
192- data += chunk
193- } )
194- res . on ( 'end' , function ( ) {
195- self . _handleResponse ( data , announce )
196- } )
197- } )
198-
199- req . on ( 'error' , function ( err ) {
200- self . emit ( 'error' , err )
201- } )
202- } )
216+ ] ) )
203217}
204218
205- Client . prototype . _handleResponse = function ( data , announce ) {
219+ Client . prototype . _handleResponse = function ( data , announceUrl ) {
206220 var self = this
207221
208222 try {
@@ -234,7 +248,7 @@ Client.prototype._handleResponse = function (data, announce) {
234248 }
235249
236250 self . emit ( 'update' , {
237- announce : announce ,
251+ announce : announceUrl ,
238252 complete : data . complete ,
239253 incomplete : data . incomplete
240254 } )
@@ -436,17 +450,17 @@ Server.prototype._getPeersCompact = function (swarm) {
436450// HELPERS
437451//
438452
439- function fromInt16 ( n ) {
440- var buf = new Buffer ( 2 ) ;
441- buf . writeUInt16BE ( n , 0 ) ;
442- return buf ;
443- } ;
444-
445- function fromInt32 ( n ) {
446- var buf = new Buffer ( 4 ) ;
447- buf . writeUInt32BE ( n , 0 ) ;
448- return buf ;
449- } ;
453+ function toUInt16 ( n ) {
454+ var buf = new Buffer ( 2 )
455+ buf . writeUInt16BE ( n , 0 )
456+ return buf
457+ }
458+
459+ function toUInt32 ( n ) {
460+ var buf = new Buffer ( 4 )
461+ buf . writeUInt32BE ( n , 0 )
462+ return buf
463+ }
450464
451465function bytewiseEncodeURIComponent ( buf ) {
452466 if ( ! Buffer . isBuffer ( buf ) ) {
0 commit comments