From d568b53931958075be962ce79d23c24307af66f5 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Fri, 19 Oct 2018 11:27:45 +0200 Subject: [PATCH] src: reduce duplication in tcp_wrap Connect This commit extracts identical code from Connect and Connect6 into a separate function to avoid some code duplication. PR-URL: https://github.com/nodejs/node/pull/23753 Reviewed-By: Anna Henningsen Reviewed-By: Matheus Marchini --- src/tcp_wrap.cc | 54 ++++++++++++++++++------------------------------- src/tcp_wrap.h | 3 +++ 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index ff30be33635a22..cda30d84105735 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