| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a80452a commit 1bdbf87
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -276,58 +276,44 @@ void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) { | |||
| 276 | 276 | ||
| 277 | 277 | ||
| 278 | 278 | 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()); | ||
| 288 | 279 | CHECK(args[2]->IsUint32()); | |
| 289 | - | ||
| 290 | - Local<Object> req_wrap_obj = args[0].As<Object>(); | ||
| 291 | - node::Utf8Value ip_address(env->isolate(), args[1]); | ||
| 292 | 280 | 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 | + } | ||
| 293 | 286 | ||
| 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 | - } | ||
| 308 | 287 | ||
| 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 | + }); | ||
| 310 | 297 | } | |
| 311 | 298 | ||
| 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); | ||
| 312 | 303 | ||
| 313 | - void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& args) { | ||
| 314 | 304 | TCPWrap* wrap; | |
| 315 | 305 | ASSIGN_OR_RETURN_UNWRAP(&wrap, | |
| 316 | 306 | args.Holder(), | |
| 317 | 307 | args.GetReturnValue().Set(UV_EBADF)); | |
| 318 | - Environment* env = wrap->env(); | ||
| 319 | 308 | ||
| 320 | 309 | CHECK(args[0]->IsObject()); | |
| 321 | 310 | CHECK(args[1]->IsString()); | |
| 322 | - CHECK(args[2]->IsUint32()); | ||
| 323 | 311 | ||
| 324 | 312 | Local<Object> req_wrap_obj = args[0].As<Object>(); | |
| 325 | 313 | node::Utf8Value ip_address(env->isolate(), args[1]); | |
| 326 | - int port; | ||
| 327 | - if (!args[2]->Int32Value(env->context()).To(&port)) return; | ||
| 328 | 314 | ||
| 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); | ||
| 331 | 317 | ||
| 332 | 318 | if (err == 0) { | |
| 333 | 319 | AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -75,6 +75,9 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> { | |||
| 75 | 75 | static void Listen(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 76 | 76 | static void Connect(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 77 | 77 | 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); | ||
| 78 | 81 | static void Open(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 79 | 82 | ||
| 80 | 83 | #ifdef _WIN32 | |
| Back | FazBrowse Home | New Git URL |
0 commit comments