diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index a07ba2d61231f4..f653297fc0ac8b 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -276,58 +276,44 @@ void TCPWrap::Listen(const FunctionCallbackInfo& args) { void TCPWrap::Connect(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - - TCPWrap* wrap; - ASSIGN_OR_RETURN_UNWRAP(&wrap, - args.Holder(), - args.GetReturnValue().Set(UV_EBADF)); - - CHECK(args[0]->IsObject()); - CHECK(args[1]->IsString()); CHECK(args[2]->IsUint32()); - - Local req_wrap_obj = args[0].As(); - node::Utf8Value ip_address(env->isolate(), args[1]); int port = args[2].As()->Value(); + Connect(args, + [port](const char* ip_address, sockaddr_in* addr) { + return uv_ip4_addr(ip_address, port, addr); + }); +} - sockaddr_in addr; - int err = uv_ip4_addr(*ip_address, port, &addr); - - if (err == 0) { - AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap); - ConnectWrap* req_wrap = - new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP); - err = req_wrap->Dispatch(uv_tcp_connect, - &wrap->handle_, - reinterpret_cast(&addr), - AfterConnect); - if (err) - delete req_wrap; - } - args.GetReturnValue().Set(err); +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); + }); } +template +void TCPWrap::Connect(const FunctionCallbackInfo& args, + std::function uv_ip_addr) { + Environment* env = Environment::GetCurrent(args); -void TCPWrap::Connect6(const FunctionCallbackInfo& args) { TCPWrap* wrap; ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder(), args.GetReturnValue().Set(UV_EBADF)); - Environment* env = wrap->env(); CHECK(args[0]->IsObject()); CHECK(args[1]->IsString()); - CHECK(args[2]->IsUint32()); Local req_wrap_obj = args[0].As(); node::Utf8Value ip_address(env->isolate(), args[1]); - int port; - if (!args[2]->Int32Value(env->context()).To(&port)) return; - sockaddr_in6 addr; - int err = uv_ip6_addr(*ip_address, port, &addr); + T addr; + int err = uv_ip_addr(*ip_address, &addr); if (err == 0) { AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap); diff --git a/src/tcp_wrap.h b/src/tcp_wrap.h index 90c81bcae6fd6f..3cbeae6d64a949 100644 --- a/src/tcp_wrap.h +++ b/src/tcp_wrap.h @@ -75,6 +75,9 @@ class TCPWrap : public ConnectionWrap { static void Listen(const v8::FunctionCallbackInfo& args); static void Connect(const v8::FunctionCallbackInfo& args); static void Connect6(const v8::FunctionCallbackInfo& args); + template + static void Connect(const v8::FunctionCallbackInfo& args, + std::function uv_ip_addr); static void Open(const v8::FunctionCallbackInfo& args); #ifdef _WIN32