Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: reduce duplication in tcp_wrap Connect #23753

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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