diff options
author | Daniel Bevenius <daniel.bevenius@gmail.com> | 2018-10-19 12:27:45 +0300 |
---|---|---|
committer | Matheus Marchini <mat@mmarchini.me> | 2018-10-24 17:14:59 +0300 |
commit | d568b53931958075be962ce79d23c24307af66f5 (patch) | |
tree | 74095171f2cf1aa62d1805ea89ca26dca62cc831 /src/tcp_wrap.cc | |
parent | 8c99a224d21b7ab81711c3c0103b1eada82c0613 (diff) |
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 <anna@addaleax.net>
Reviewed-By: Matheus Marchini <mat@mmarchini.me>
Diffstat (limited to 'src/tcp_wrap.cc')
-rw-r--r-- | src/tcp_wrap.cc | 54 |
1 files changed, 20 insertions, 34 deletions
diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index ff30be33635..cda30d84105 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -276,58 +276,44 @@ void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) { void TCPWrap::Connect(const FunctionCallbackInfo<Value>& 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<Object> req_wrap_obj = args[0].As<Object>(); - node::Utf8Value ip_address(env->isolate(), args[1]); int port = args[2].As<Uint32>()->Value(); + Connect<sockaddr_in>(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<const sockaddr*>(&addr), - AfterConnect); - if (err) - delete req_wrap; - } - args.GetReturnValue().Set(err); +void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& args) { + Environment* env = Environment::GetCurrent(args); + CHECK(args[2]->IsUint32()); + int port; + if (!args[2]->Int32Value(env->context()).To(&port)) return; + Connect<sockaddr_in6>(args, + [port](const char* ip_address, sockaddr_in6* addr) { + return uv_ip6_addr(ip_address, port, addr); + }); } +template <typename T> +void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args, + std::function<int(const char* ip_address, T* addr)> uv_ip_addr) { + Environment* env = Environment::GetCurrent(args); -void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& 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<Object> req_wrap_obj = args[0].As<Object>(); 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); |