34
34
35
35
#include < cstdlib>
36
36
37
-
38
37
namespace node {
39
38
40
39
using v8::Boolean;
@@ -68,7 +67,6 @@ MaybeLocal<Object> TCPWrap::Instantiate(Environment* env,
68
67
constructor->NewInstance (env->context (), 1 , &type_value));
69
68
}
70
69
71
-
72
70
void TCPWrap::Initialize (Local<Object> target,
73
71
Local<Value> unused,
74
72
Local<Context> context,
@@ -124,9 +122,7 @@ void TCPWrap::Initialize(Local<Object> target,
124
122
NODE_DEFINE_CONSTANT (constants, SERVER);
125
123
NODE_DEFINE_CONSTANT (constants, UV_TCP_IPV6ONLY);
126
124
NODE_DEFINE_CONSTANT (constants, UV_TCP_REUSEPORT);
127
- target->Set (context,
128
- env->constants_string (),
129
- constants).Check ();
125
+ target->Set (context, env->constants_string (), constants).Check ();
130
126
}
131
127
132
128
void TCPWrap::RegisterExternalReferences (ExternalReferenceRegistry* registry) {
@@ -174,15 +170,13 @@ void TCPWrap::New(const FunctionCallbackInfo<Value>& args) {
174
170
new TCPWrap (env, args.This (), provider);
175
171
}
176
172
177
-
178
173
TCPWrap::TCPWrap (Environment* env, Local<Object> object, ProviderType provider)
179
174
: ConnectionWrap(env, object, provider) {
180
175
int r = uv_tcp_init (env->event_loop (), &handle_);
181
176
CHECK_EQ (r, 0 ); // How do we proxy this error up to javascript?
182
177
// Suggestion: uv_tcp_init() returns void.
183
178
}
184
179
185
-
186
180
void TCPWrap::SetNoDelay (const FunctionCallbackInfo<Value>& args) {
187
181
TCPWrap* wrap;
188
182
ASSIGN_OR_RETURN_UNWRAP (
@@ -192,7 +186,6 @@ void TCPWrap::SetNoDelay(const FunctionCallbackInfo<Value>& args) {
192
186
args.GetReturnValue ().Set (err);
193
187
}
194
188
195
-
196
189
void TCPWrap::SetKeepAlive (const FunctionCallbackInfo<Value>& args) {
197
190
TCPWrap* wrap;
198
191
ASSIGN_OR_RETURN_UNWRAP (
@@ -205,7 +198,6 @@ void TCPWrap::SetKeepAlive(const FunctionCallbackInfo<Value>& args) {
205
198
args.GetReturnValue ().Set (err);
206
199
}
207
200
208
-
209
201
#ifdef _WIN32
210
202
void TCPWrap::SetSimultaneousAccepts (const FunctionCallbackInfo<Value>& args) {
211
203
TCPWrap* wrap;
@@ -217,7 +209,6 @@ void TCPWrap::SetSimultaneousAccepts(const FunctionCallbackInfo<Value>& args) {
217
209
}
218
210
#endif
219
211
220
-
221
212
void TCPWrap::Open (const FunctionCallbackInfo<Value>& args) {
222
213
TCPWrap* wrap;
223
214
ASSIGN_OR_RETURN_UNWRAP (
@@ -228,8 +219,7 @@ void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
228
219
int fd = static_cast <int >(val);
229
220
int err = uv_tcp_open (&wrap->handle_ , fd);
230
221
231
- if (err == 0 )
232
- wrap->set_fd (fd);
222
+ if (err == 0 ) wrap->set_fd (fd);
233
223
234
224
args.GetReturnValue ().Set (err);
235
225
}
@@ -259,9 +249,8 @@ void TCPWrap::Bind(
259
249
int err = uv_ip_addr (*ip_address, port, &addr);
260
250
261
251
if (err == 0 ) {
262
- err = uv_tcp_bind (&wrap->handle_ ,
263
- reinterpret_cast <const sockaddr*>(&addr),
264
- flags);
252
+ err = uv_tcp_bind (
253
+ &wrap->handle_ , reinterpret_cast <const sockaddr*>(&addr), flags);
265
254
}
266
255
args.GetReturnValue ().Set (err);
267
256
}
@@ -270,50 +259,46 @@ void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args) {
270
259
Bind<sockaddr_in>(args, AF_INET, uv_ip4_addr);
271
260
}
272
261
273
-
274
262
void TCPWrap::Bind6 (const FunctionCallbackInfo<Value>& args) {
275
263
Bind<sockaddr_in6>(args, AF_INET6, uv_ip6_addr);
276
264
}
277
265
278
-
279
266
void TCPWrap::Listen (const FunctionCallbackInfo<Value>& args) {
280
267
TCPWrap* wrap;
281
268
ASSIGN_OR_RETURN_UNWRAP (
282
269
&wrap, args.This (), args.GetReturnValue ().Set (UV_EBADF));
283
270
Environment* env = wrap->env ();
284
271
int backlog;
285
272
if (!args[0 ]->Int32Value (env->context ()).To (&backlog)) return ;
286
- int err = uv_listen (reinterpret_cast <uv_stream_t *>(&wrap->handle_ ),
287
- backlog,
288
- OnConnection);
273
+ int err = uv_listen (
274
+ reinterpret_cast <uv_stream_t *>(&wrap->handle_ ), backlog, OnConnection);
289
275
args.GetReturnValue ().Set (err);
290
276
}
291
277
292
-
293
278
void TCPWrap::Connect (const FunctionCallbackInfo<Value>& args) {
294
- CHECK (args[ 2 ]-> IsUint32 () );
295
- // explicit cast to fit to libuv's type expectation
296
- int port = static_cast < int >( args[2 ]. As <Uint32>()-> Value ());
297
- Connect<sockaddr_in>(args,
298
- [port](const char * ip_address, sockaddr_in* addr) {
299
- return uv_ip4_addr (ip_address, port, addr);
279
+ Environment* env = Environment::GetCurrent (args);
280
+ int port;
281
+ if (! args[2 ]-> Int32Value (env-> context ()). To (&port)) return ;
282
+
283
+ Connect<sockaddr_in>(args, [port](const char * ip_address, sockaddr_in* addr) {
284
+ return uv_ip4_addr (ip_address, port, addr);
300
285
});
301
286
}
302
287
303
-
304
288
void TCPWrap::Connect6 (const FunctionCallbackInfo<Value>& args) {
305
289
Environment* env = Environment::GetCurrent (args);
306
- CHECK (args[ 2 ]-> IsUint32 ());
290
+
307
291
int port;
308
292
if (!args[2 ]->Int32Value (env->context ()).To (&port)) return ;
309
293
Connect<sockaddr_in6>(args,
310
294
[port](const char * ip_address, sockaddr_in6* addr) {
311
- return uv_ip6_addr (ip_address, port, addr);
312
- });
295
+ return uv_ip6_addr (ip_address, port, addr);
296
+ });
313
297
}
314
298
315
299
template <typename T>
316
- void TCPWrap::Connect (const FunctionCallbackInfo<Value>& args,
300
+ void TCPWrap::Connect (
301
+ const FunctionCallbackInfo<Value>& args,
317
302
std::function<int (const char * ip_address, T* addr)> uv_ip_addr) {
318
303
Environment* env = Environment::GetCurrent (args);
319
304
@@ -390,61 +375,65 @@ MaybeLocal<Object> AddressToJS(Environment* env,
390
375
391
376
int port;
392
377
393
- if (info.IsEmpty ())
394
- info = Object::New (env->isolate ());
378
+ if (info.IsEmpty ()) info = Object::New (env->isolate ());
395
379
396
380
switch (addr->sa_family ) {
397
- case AF_INET6:
398
- a6 = reinterpret_cast <const sockaddr_in6*>(addr);
399
- uv_inet_ntop (AF_INET6, &a6->sin6_addr , ip, sizeof ip);
400
- // Add an interface identifier to a link local address.
401
- if (IN6_IS_ADDR_LINKLOCAL (&a6->sin6_addr ) && a6->sin6_scope_id > 0 ) {
402
- const size_t addrlen = strlen (ip);
403
- CHECK_LT (addrlen, sizeof (ip));
404
- ip[addrlen] = ' %' ;
405
- size_t scopeidlen = sizeof (ip) - addrlen - 1 ;
406
- CHECK_GE (scopeidlen, UV_IF_NAMESIZE);
407
- const int r = uv_if_indextoiid (a6-> sin6_scope_id ,
408
- ip + addrlen + 1 ,
409
- &scopeidlen);
410
- if (r) {
411
- env-> ThrowUVException (r, " uv_if_indextoiid " ) ;
412
- return {};
381
+ case AF_INET6:
382
+ a6 = reinterpret_cast <const sockaddr_in6*>(addr);
383
+ uv_inet_ntop (AF_INET6, &a6->sin6_addr , ip, sizeof ip);
384
+ // Add an interface identifier to a link local address.
385
+ if (IN6_IS_ADDR_LINKLOCAL (&a6->sin6_addr ) && a6->sin6_scope_id > 0 ) {
386
+ const size_t addrlen = strlen (ip);
387
+ CHECK_LT (addrlen, sizeof (ip));
388
+ ip[addrlen] = ' %' ;
389
+ size_t scopeidlen = sizeof (ip) - addrlen - 1 ;
390
+ CHECK_GE (scopeidlen, UV_IF_NAMESIZE);
391
+ const int r =
392
+ uv_if_indextoiid (a6-> sin6_scope_id , ip + addrlen + 1 , &scopeidlen);
393
+ if (r) {
394
+ env-> ThrowUVException (r, " uv_if_indextoiid " );
395
+ return {} ;
396
+ }
413
397
}
414
- }
415
- port = ntohs (a6->sin6_port );
416
- info->Set (env->context (),
417
- env->address_string (),
418
- OneByteString (env->isolate (), ip)).Check ();
419
- info->Set (env->context (), env->family_string (), env->ipv6_string ()).Check ();
420
- info->Set (env->context (),
421
- env->port_string (),
422
- Integer::New (env->isolate (), port)).Check ();
423
- break ;
424
-
425
- case AF_INET:
426
- a4 = reinterpret_cast <const sockaddr_in*>(addr);
427
- uv_inet_ntop (AF_INET, &a4->sin_addr , ip, sizeof ip);
428
- port = ntohs (a4->sin_port );
429
- info->Set (env->context (),
430
- env->address_string (),
431
- OneByteString (env->isolate (), ip)).Check ();
432
- info->Set (env->context (), env->family_string (), env->ipv4_string ()).Check ();
433
- info->Set (env->context (),
434
- env->port_string (),
435
- Integer::New (env->isolate (), port)).Check ();
436
- break ;
437
-
438
- default :
439
- info->Set (env->context (),
440
- env->address_string (),
441
- String::Empty (env->isolate ())).Check ();
398
+ port = ntohs (a6->sin6_port );
399
+ info->Set (env->context (),
400
+ env->address_string (),
401
+ OneByteString (env->isolate (), ip))
402
+ .Check ();
403
+ info->Set (env->context (), env->family_string (), env->ipv6_string ())
404
+ .Check ();
405
+ info->Set (env->context (),
406
+ env->port_string (),
407
+ Integer::New (env->isolate (), port))
408
+ .Check ();
409
+ break ;
410
+
411
+ case AF_INET:
412
+ a4 = reinterpret_cast <const sockaddr_in*>(addr);
413
+ uv_inet_ntop (AF_INET, &a4->sin_addr , ip, sizeof ip);
414
+ port = ntohs (a4->sin_port );
415
+ info->Set (env->context (),
416
+ env->address_string (),
417
+ OneByteString (env->isolate (), ip))
418
+ .Check ();
419
+ info->Set (env->context (), env->family_string (), env->ipv4_string ())
420
+ .Check ();
421
+ info->Set (env->context (),
422
+ env->port_string (),
423
+ Integer::New (env->isolate (), port))
424
+ .Check ();
425
+ break ;
426
+
427
+ default :
428
+ info->Set (env->context (),
429
+ env->address_string (),
430
+ String::Empty (env->isolate ()))
431
+ .Check ();
442
432
}
443
433
444
434
return scope.Escape (info);
445
435
}
446
436
447
-
448
437
} // namespace node
449
438
450
439
NODE_BINDING_CONTEXT_AWARE_INTERNAL (tcp_wrap, node::TCPWrap::Initialize)
0 commit comments