@@ -225,40 +225,28 @@ void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
225
225
args.GetReturnValue ().Set (err);
226
226
}
227
227
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) {
230
233
TCPWrap* wrap;
231
234
ASSIGN_OR_RETURN_UNWRAP (&wrap,
232
235
args.Holder (),
233
236
args.GetReturnValue ().Set (UV_EBADF));
234
237
Environment* env = wrap->env ();
235
238
node::Utf8Value ip_address (env->isolate (), args[0 ]);
236
239
int port;
240
+ unsigned int flags = 0 ;
237
241
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 ;
244
245
}
245
- args.GetReturnValue ().Set (err);
246
- }
247
246
247
+ T addr;
248
+ int err = uv_ip_addr (*ip_address, port, &addr);
248
249
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);
262
250
if (err == 0 ) {
263
251
err = uv_tcp_bind (&wrap->handle_ ,
264
252
reinterpret_cast <const sockaddr*>(&addr),
@@ -267,6 +255,15 @@ void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
267
255
args.GetReturnValue ().Set (err);
268
256
}
269
257
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
+
270
267
271
268
void TCPWrap::Listen (const FunctionCallbackInfo<Value>& args) {
272
269
TCPWrap* wrap;
0 commit comments