@@ -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
@@ -326,7 +324,6 @@ Socket.prototype.send = function(buffer,
326
324
port ,
327
325
address ,
328
326
callback ) {
329
- const self = this ;
330
327
let list ;
331
328
332
329
if ( address || ( port && typeof port !== 'function' ) ) {
@@ -358,24 +355,26 @@ Socket.prototype.send = function(buffer,
358
355
if ( typeof callback !== 'function' )
359
356
callback = undefined ;
360
357
361
- self . _healthCheck ( ) ;
358
+ this . _healthCheck ( ) ;
362
359
363
- if ( self . _bindState === BIND_STATE_UNBOUND )
364
- self . bind ( { port : 0 , exclusive : true } , null ) ;
360
+ if ( this . _bindState === BIND_STATE_UNBOUND )
361
+ this . bind ( { port : 0 , exclusive : true } , null ) ;
365
362
366
363
if ( list . length === 0 )
367
364
list . push ( Buffer . alloc ( 0 ) ) ;
368
365
369
366
// If the socket hasn't been bound yet, push the outbound packet onto the
370
367
// send queue and send after binding is complete.
371
- if ( self . _bindState !== BIND_STATE_BOUND ) {
372
- enqueue ( self , self . send . bind ( self , list , port , address , callback ) ) ;
368
+ if ( this . _bindState !== BIND_STATE_BOUND ) {
369
+ enqueue ( this , this . send . bind ( this , list , port , address , callback ) ) ;
373
370
return ;
374
371
}
375
372
376
- self . _handle . lookup ( address , function afterDns ( ex , ip ) {
377
- doSend ( ex , self , ip , list , address , port , callback ) ;
378
- } ) ;
373
+ const afterDns = ( ex , ip ) => {
374
+ doSend ( ex , this , ip , list , address , port , callback ) ;
375
+ } ;
376
+
377
+ this . _handle . lookup ( address , afterDns ) ;
379
378
} ;
380
379
381
380
0 commit comments