diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 55d1d8516a8cd2..466b0f8938c699 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -373,10 +373,11 @@ void TCPWrap::Open(const FunctionCallbackInfo& args) { } template -void TCPWrap::Bind( - const FunctionCallbackInfo& args, - int family, - std::function uv_ip_addr) { +void TCPWrap::Bind(const FunctionCallbackInfo& args, + int family, + int (*uv_ip_addr)(const char* ip_address, + int port, + T* addr)) { TCPWrap* wrap; ASSIGN_OR_RETURN_UNWRAP( &wrap, args.This(), args.GetReturnValue().Set(UV_EBADF)); @@ -430,29 +431,18 @@ void TCPWrap::Listen(const FunctionCallbackInfo& args) { } void TCPWrap::Connect(const FunctionCallbackInfo& args) { - CHECK(args[2]->IsUint32()); - // explicit cast to fit to libuv's type expectation - int port = static_cast(args[2].As()->Value()); - Connect(args, [port](const char* ip_address, sockaddr_in* addr) { - return uv_ip4_addr(ip_address, port, addr); - }); + Connect(args, uv_ip4_addr); } void TCPWrap::Connect6(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - CHECK(args[2]->IsUint32()); - int port; - if (!args[2]->Int32Value(env->context()).To(&port)) return; - Connect(args, - [port](const char* ip_address, sockaddr_in6* addr) { - return uv_ip6_addr(ip_address, port, addr); - }); + Connect(args, uv_ip6_addr); } template -void TCPWrap::Connect( - const FunctionCallbackInfo& args, - std::function uv_ip_addr) { +void TCPWrap::Connect(const FunctionCallbackInfo& args, + int (*uv_ip_addr)(const char* ip_address, + int port, + T* addr)) { Environment* env = Environment::GetCurrent(args); TCPWrap* wrap; @@ -462,6 +452,9 @@ void TCPWrap::Connect( CHECK(args[0]->IsObject()); CHECK(args[1]->IsString()); + int port; + if (!args[2]->Int32Value(env->context()).To(&port)) return; + Local req_wrap_obj = args[0].As(); node::Utf8Value ip_address(env->isolate(), args[1]); @@ -469,7 +462,7 @@ void TCPWrap::Connect( env, permission::PermissionScope::kNet, ip_address.ToStringView(), args); T addr; - int err = uv_ip_addr(*ip_address, &addr); + int err = uv_ip_addr(*ip_address, port, &addr); if (err == 0) { AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap); diff --git a/src/tcp_wrap.h b/src/tcp_wrap.h index 9149a3e013f4f5..11332f2bf40a4d 100644 --- a/src/tcp_wrap.h +++ b/src/tcp_wrap.h @@ -83,13 +83,16 @@ class TCPWrap : public ConnectionWrap { static void Connect6(const v8::FunctionCallbackInfo& args); template static void Connect(const v8::FunctionCallbackInfo& args, - std::function uv_ip_addr); + int (*uv_ip_addr)(const char* ip_address, + int port, + T* addr)); static void Open(const v8::FunctionCallbackInfo& args); template - static void Bind( - const v8::FunctionCallbackInfo& args, - int family, - std::function uv_ip_addr); + static void Bind(const v8::FunctionCallbackInfo& args, + int family, + int (*uv_ip_addr)(const char* ip_address, + int port, + T* addr)); static void Reset(const v8::FunctionCallbackInfo& args); int Reset(v8::Local close_callback = v8::Local());