Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1bdbf87

Browse filesBrowse files
danbevtargos
authored andcommitted
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: #23753 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matheus Marchini <mat@mmarchini.me>
1 parent a80452a commit 1bdbf87
Copy full SHA for 1bdbf87

File tree

Expand file treeCollapse file tree

2 files changed

+23
-34
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+23
-34
lines changed
Open diff view settings
Collapse file

‎src/tcp_wrap.cc‎

Copy file name to clipboardExpand all lines: src/tcp_wrap.cc
+20-34Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -276,58 +276,44 @@ void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
276276

277277

278278
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) {
279-
Environment* env = Environment::GetCurrent(args);
280-
281-
TCPWrap* wrap;
282-
ASSIGN_OR_RETURN_UNWRAP(&wrap,
283-
args.Holder(),
284-
args.GetReturnValue().Set(UV_EBADF));
285-
286-
CHECK(args[0]->IsObject());
287-
CHECK(args[1]->IsString());
288279
CHECK(args[2]->IsUint32());
289-
290-
Local<Object> req_wrap_obj = args[0].As<Object>();
291-
node::Utf8Value ip_address(env->isolate(), args[1]);
292280
int port = args[2].As<Uint32>()->Value();
281+
Connect<sockaddr_in>(args,
282+
[port](const char* ip_address, sockaddr_in* addr) {
283+
return uv_ip4_addr(ip_address, port, addr);
284+
});
285+
}
293286

294-
sockaddr_in addr;
295-
int err = uv_ip4_addr(*ip_address, port, &addr);
296-
297-
if (err == 0) {
298-
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap);
299-
ConnectWrap* req_wrap =
300-
new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP);
301-
err = req_wrap->Dispatch(uv_tcp_connect,
302-
&wrap->handle_,
303-
reinterpret_cast<const sockaddr*>(&addr),
304-
AfterConnect);
305-
if (err)
306-
delete req_wrap;
307-
}
308287

309-
args.GetReturnValue().Set(err);
288+
void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& args) {
289+
Environment* env = Environment::GetCurrent(args);
290+
CHECK(args[2]->IsUint32());
291+
int port;
292+
if (!args[2]->Int32Value(env->context()).To(&port)) return;
293+
Connect<sockaddr_in6>(args,
294+
[port](const char* ip_address, sockaddr_in6* addr) {
295+
return uv_ip6_addr(ip_address, port, addr);
296+
});
310297
}
311298

299+
template <typename T>
300+
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
301+
std::function<int(const char* ip_address, T* addr)> uv_ip_addr) {
302+
Environment* env = Environment::GetCurrent(args);
312303

313-
void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& args) {
314304
TCPWrap* wrap;
315305
ASSIGN_OR_RETURN_UNWRAP(&wrap,
316306
args.Holder(),
317307
args.GetReturnValue().Set(UV_EBADF));
318-
Environment* env = wrap->env();
319308

320309
CHECK(args[0]->IsObject());
321310
CHECK(args[1]->IsString());
322-
CHECK(args[2]->IsUint32());
323311

324312
Local<Object> req_wrap_obj = args[0].As<Object>();
325313
node::Utf8Value ip_address(env->isolate(), args[1]);
326-
int port;
327-
if (!args[2]->Int32Value(env->context()).To(&port)) return;
328314

329-
sockaddr_in6 addr;
330-
int err = uv_ip6_addr(*ip_address, port, &addr);
315+
T addr;
316+
int err = uv_ip_addr(*ip_address, &addr);
331317

332318
if (err == 0) {
333319
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap);
Collapse file

‎src/tcp_wrap.h‎

Copy file name to clipboardExpand all lines: src/tcp_wrap.h
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
7575
static void Listen(const v8::FunctionCallbackInfo<v8::Value>& args);
7676
static void Connect(const v8::FunctionCallbackInfo<v8::Value>& args);
7777
static void Connect6(const v8::FunctionCallbackInfo<v8::Value>& args);
78+
template <typename T>
79+
static void Connect(const v8::FunctionCallbackInfo<v8::Value>& args,
80+
std::function<int(const char* ip_address, T* addr)> uv_ip_addr);
7881
static void Open(const v8::FunctionCallbackInfo<v8::Value>& args);
7982

8083
#ifdef _WIN32

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.