Skip to content

Commit

Permalink
src: reduce duplication in tcp_wrap Connect
Browse files Browse the repository at this point in the history
This commit extracts identical code from Connect and Connect6 into a
separate function to avoid some code duplication.

PR-URL: #23753
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matheus Marchini <[email protected]>
  • Loading branch information
danbev authored and mmarchini committed Oct 24, 2018
1 parent 8c99a22 commit d568b53
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
54 changes: 20 additions & 34 deletions src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/tcp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
static void Listen(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Connect(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Connect6(const v8::FunctionCallbackInfo<v8::Value>& args);
template <typename T>
static void Connect(const v8::FunctionCallbackInfo<v8::Value>& args,
std::function<int(const char* ip_address, T* addr)> uv_ip_addr);
static void Open(const v8::FunctionCallbackInfo<v8::Value>& args);

#ifdef _WIN32
Expand Down

0 comments on commit d568b53

Please sign in to comment.