Skip to content

Commit a4505c6

Browse files
maclover7MylesBorins
authored andcommitted
src: extract common Bind method
`TCPWrap::Bind` and `TCPWrap::Bind6` share a large amount of functionality, so a common `Bind` was extracted to remove duplication. PR-URL: #22315 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com>
1 parent e340b8f commit a4505c6

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

src/tcp_wrap.cc

+20-23
Original file line numberDiff line numberDiff line change
@@ -225,40 +225,28 @@ void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
225225
args.GetReturnValue().Set(err);
226226
}
227227

228-
229-
void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args) {
228+
template <typename T>
229+
void TCPWrap::Bind(
230+
const FunctionCallbackInfo<Value>& args,
231+
int family,
232+
std::function<int(const char* ip_address, int port, T* addr)> uv_ip_addr) {
230233
TCPWrap* wrap;
231234
ASSIGN_OR_RETURN_UNWRAP(&wrap,
232235
args.Holder(),
233236
args.GetReturnValue().Set(UV_EBADF));
234237
Environment* env = wrap->env();
235238
node::Utf8Value ip_address(env->isolate(), args[0]);
236239
int port;
240+
unsigned int flags = 0;
237241
if (!args[1]->Int32Value(env->context()).To(&port)) return;
238-
sockaddr_in addr;
239-
int err = uv_ip4_addr(*ip_address, port, &addr);
240-
if (err == 0) {
241-
err = uv_tcp_bind(&wrap->handle_,
242-
reinterpret_cast<const sockaddr*>(&addr),
243-
0);
242+
if (family == AF_INET6 &&
243+
!args[2]->Uint32Value(env->context()).To(&flags)) {
244+
return;
244245
}
245-
args.GetReturnValue().Set(err);
246-
}
247246

247+
T addr;
248+
int err = uv_ip_addr(*ip_address, port, &addr);
248249

249-
void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
250-
TCPWrap* wrap;
251-
ASSIGN_OR_RETURN_UNWRAP(&wrap,
252-
args.Holder(),
253-
args.GetReturnValue().Set(UV_EBADF));
254-
Environment* env = wrap->env();
255-
node::Utf8Value ip6_address(env->isolate(), args[0]);
256-
int port;
257-
unsigned int flags;
258-
if (!args[1]->Int32Value(env->context()).To(&port)) return;
259-
if (!args[2]->Uint32Value(env->context()).To(&flags)) return;
260-
sockaddr_in6 addr;
261-
int err = uv_ip6_addr(*ip6_address, port, &addr);
262250
if (err == 0) {
263251
err = uv_tcp_bind(&wrap->handle_,
264252
reinterpret_cast<const sockaddr*>(&addr),
@@ -267,6 +255,15 @@ void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
267255
args.GetReturnValue().Set(err);
268256
}
269257

258+
void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args) {
259+
Bind<sockaddr_in>(args, AF_INET, uv_ip4_addr);
260+
}
261+
262+
263+
void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
264+
Bind<sockaddr_in6>(args, AF_INET6, uv_ip6_addr);
265+
}
266+
270267

271268
void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
272269
TCPWrap* wrap;

src/tcp_wrap.h

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
8080
static void Connect(const v8::FunctionCallbackInfo<v8::Value>& args,
8181
std::function<int(const char* ip_address, T* addr)> uv_ip_addr);
8282
static void Open(const v8::FunctionCallbackInfo<v8::Value>& args);
83+
template <typename T>
84+
static void Bind(
85+
const v8::FunctionCallbackInfo<v8::Value>& args,
86+
int family,
87+
std::function<int(const char* ip_address, int port, T* addr)> uv_ip_addr);
8388

8489
#ifdef _WIN32
8590
static void SetSimultaneousAccepts(

0 commit comments

Comments
 (0)