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