@@ -132,23 +132,22 @@ function replaceHandle(self, newHandle) {
132
132
}
133
133
134
134
Socket . prototype . bind = function ( port_ /*, address, callback*/ ) {
135
- var self = this ;
136
135
let port = port_ ;
137
136
138
- self . _healthCheck ( ) ;
137
+ this . _healthCheck ( ) ;
139
138
140
139
if ( this . _bindState !== BIND_STATE_UNBOUND )
141
140
throw new Error ( 'Socket is already bound' ) ;
142
141
143
142
this . _bindState = BIND_STATE_BINDING ;
144
143
145
144
if ( typeof arguments [ arguments . length - 1 ] === 'function' )
146
- self . once ( 'listening' , arguments [ arguments . length - 1 ] ) ;
145
+ this . once ( 'listening' , arguments [ arguments . length - 1 ] ) ;
147
146
148
147
if ( port instanceof UDP ) {
149
- replaceHandle ( self , port ) ;
150
- startListening ( self ) ;
151
- return self ;
148
+ replaceHandle ( this , port ) ;
149
+ startListening ( this ) ;
150
+ return this ;
152
151
}
153
152
154
153
var address ;
@@ -164,69 +163,68 @@ Socket.prototype.bind = function(port_ /*, address, callback*/) {
164
163
}
165
164
166
165
// defaulting address for bind to all interfaces
167
- if ( ! address && self . _handle . lookup === lookup4 ) {
166
+ if ( ! address && this . _handle . lookup === lookup4 ) {
168
167
address = '0.0.0.0' ;
169
- } else if ( ! address && self . _handle . lookup === lookup6 ) {
168
+ } else if ( ! address && this . _handle . lookup === lookup6 ) {
170
169
address = '::' ;
171
170
}
172
171
173
172
// resolve address first
174
- self . _handle . lookup ( address , function ( err , ip ) {
173
+ this . _handle . lookup ( address , ( err , ip ) => {
175
174
if ( err ) {
176
- self . _bindState = BIND_STATE_UNBOUND ;
177
- self . emit ( 'error' , err ) ;
175
+ this . _bindState = BIND_STATE_UNBOUND ;
176
+ this . emit ( 'error' , err ) ;
178
177
return ;
179
178
}
180
179
181
180
if ( ! cluster )
182
181
cluster = require ( 'cluster' ) ;
183
182
184
183
var flags = 0 ;
185
- if ( self . _reuseAddr )
184
+ if ( this . _reuseAddr )
186
185
flags |= UV_UDP_REUSEADDR ;
187
186
188
187
if ( cluster . isWorker && ! exclusive ) {
189
- function onHandle ( err , handle ) {
188
+ const onHandle = ( err , handle ) => {
190
189
if ( err ) {
191
190
var ex = exceptionWithHostPort ( err , 'bind' , ip , port ) ;
192
- self . emit ( 'error' , ex ) ;
193
- self . _bindState = BIND_STATE_UNBOUND ;
191
+ this . emit ( 'error' , ex ) ;
192
+ this . _bindState = BIND_STATE_UNBOUND ;
194
193
return ;
195
194
}
196
195
197
- if ( ! self . _handle )
196
+ if ( ! this . _handle )
198
197
// handle has been closed in the mean time.
199
198
return handle . close ( ) ;
200
199
201
- replaceHandle ( self , handle ) ;
202
- startListening ( self ) ;
203
- }
204
- cluster . _getServer ( self , {
200
+ replaceHandle ( this , handle ) ;
201
+ startListening ( this ) ;
202
+ } ;
203
+ cluster . _getServer ( this , {
205
204
address : ip ,
206
205
port : port ,
207
- addressType : self . type ,
206
+ addressType : this . type ,
208
207
fd : - 1 ,
209
208
flags : flags
210
209
} , onHandle ) ;
211
-
212
210
} else {
213
- if ( ! self . _handle )
211
+ if ( ! this . _handle )
214
212
return ; // handle has been closed in the mean time
215
213
216
- const err = self . _handle . bind ( ip , port || 0 , flags ) ;
214
+ const err = this . _handle . bind ( ip , port || 0 , flags ) ;
217
215
if ( err ) {
218
216
var ex = exceptionWithHostPort ( err , 'bind' , ip , port ) ;
219
- self . emit ( 'error' , ex ) ;
220
- self . _bindState = BIND_STATE_UNBOUND ;
217
+ this . emit ( 'error' , ex ) ;
218
+ this . _bindState = BIND_STATE_UNBOUND ;
221
219
// Todo: close?
222
220
return ;
223
221
}
224
222
225
- startListening ( self ) ;
223
+ startListening ( this ) ;
226
224
}
227
225
} ) ;
228
226
229
- return self ;
227
+ return this ;
230
228
} ;
231
229
232
230
@@ -312,7 +310,6 @@ Socket.prototype.send = function(buffer,
312
310
port ,
313
311
address ,
314
312
callback ) {
315
- const self = this ;
316
313
let list ;
317
314
318
315
if ( address || ( port && typeof port !== 'function' ) ) {
@@ -344,24 +341,26 @@ Socket.prototype.send = function(buffer,
344
341
if ( typeof callback !== 'function' )
345
342
callback = undefined ;
346
343
347
- self . _healthCheck ( ) ;
344
+ this . _healthCheck ( ) ;
348
345
349
- if ( self . _bindState === BIND_STATE_UNBOUND )
350
- self . bind ( { port : 0 , exclusive : true } , null ) ;
346
+ if ( this . _bindState === BIND_STATE_UNBOUND )
347
+ this . bind ( { port : 0 , exclusive : true } , null ) ;
351
348
352
349
if ( list . length === 0 )
353
350
list . push ( Buffer . alloc ( 0 ) ) ;
354
351
355
352
// If the socket hasn't been bound yet, push the outbound packet onto the
356
353
// send queue and send after binding is complete.
357
- if ( self . _bindState !== BIND_STATE_BOUND ) {
358
- enqueue ( self , self . send . bind ( self , list , port , address , callback ) ) ;
354
+ if ( this . _bindState !== BIND_STATE_BOUND ) {
355
+ enqueue ( this , this . send . bind ( this , list , port , address , callback ) ) ;
359
356
return ;
360
357
}
361
358
362
- self . _handle . lookup ( address , function afterDns ( ex , ip ) {
363
- doSend ( ex , self , ip , list , address , port , callback ) ;
364
- } ) ;
359
+ const afterDns = ( ex , ip ) => {
360
+ doSend ( ex , this , ip , list , address , port , callback ) ;
361
+ } ;
362
+
363
+ this . _handle . lookup ( address , afterDns ) ;
365
364
} ;
366
365
367
366
0 commit comments