From 83a354c33b29686ba6df9cd017ab6b4baaff4272 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Sun, 24 Jul 2016 05:13:19 +0200 Subject: [PATCH] src: pull AfterConnect from pipe_wrap and tcp_wrap This commit attempts to address one of the items in https://github.com/nodejs/node/issues/4641 which is related to src/pipe_wrap.cc and src/tcp_wrap.cc. Currently both pipe_wrap.cc and tcp_wrap.cc contain an AfterConnect function that are almost identical. This commit extracts this function into ConnectionWrap so that that both can share it. PR-URL: https://github.com/nodejs/node/pull/8448 Reviewed-By: Ben Noordhuis Reviewed-By: Colin Ihrig Reviewed-By: Anna Henningsen Reviewed-By: Sakthipriyan Vairamani --- src/connection_wrap.cc | 48 ++++++++++++++++++++++++++++++++++++++++++ src/connection_wrap.h | 1 + src/pipe_wrap.cc | 40 ----------------------------------- src/pipe_wrap.h | 2 -- src/tcp_wrap.cc | 28 ------------------------ src/tcp_wrap.h | 2 -- 6 files changed, 49 insertions(+), 72 deletions(-) diff --git a/src/connection_wrap.cc b/src/connection_wrap.cc index f98fb79eb9..41571946b2 100644 --- a/src/connection_wrap.cc +++ b/src/connection_wrap.cc @@ -1,5 +1,6 @@ #include "connection_wrap.h" +#include "connect_wrap.h" #include "env-inl.h" #include "env.h" #include "pipe_wrap.h" @@ -10,6 +11,7 @@ namespace node { +using v8::Boolean; using v8::Context; using v8::HandleScope; using v8::Integer; @@ -71,6 +73,46 @@ void ConnectionWrap::OnConnection(uv_stream_t* handle, wrap_data->MakeCallback(env->onconnection_string(), arraysize(argv), argv); } + +template +void ConnectionWrap::AfterConnect(uv_connect_t* req, + int status) { + ConnectWrap* req_wrap = static_cast(req->data); + CHECK_NE(req_wrap, nullptr); + WrapType* wrap = static_cast(req->handle->data); + CHECK_EQ(req_wrap->env(), wrap->env()); + Environment* env = wrap->env(); + + HandleScope handle_scope(env->isolate()); + Context::Scope context_scope(env->context()); + + // The wrap and request objects should still be there. + CHECK_EQ(req_wrap->persistent().IsEmpty(), false); + CHECK_EQ(wrap->persistent().IsEmpty(), false); + + bool readable, writable; + + if (status) { + readable = writable = 0; + } else { + readable = uv_is_readable(req->handle) != 0; + writable = uv_is_writable(req->handle) != 0; + } + + Local req_wrap_obj = req_wrap->object(); + Local argv[5] = { + Integer::New(env->isolate(), status), + wrap->object(), + req_wrap_obj, + Boolean::New(env->isolate(), readable), + Boolean::New(env->isolate(), writable) + }; + + req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv); + + delete req_wrap; +} + template ConnectionWrap::ConnectionWrap( Environment* env, Local object, @@ -89,5 +131,11 @@ template void ConnectionWrap::OnConnection( template void ConnectionWrap::OnConnection( uv_stream_t* handle, int status); +template void ConnectionWrap::AfterConnect( + uv_connect_t* handle, int status); + +template void ConnectionWrap::AfterConnect( + uv_connect_t* handle, int status); + } // namespace node diff --git a/src/connection_wrap.h b/src/connection_wrap.h index 1702e22dd3..7af97fd3f0 100644 --- a/src/connection_wrap.h +++ b/src/connection_wrap.h @@ -17,6 +17,7 @@ class ConnectionWrap : public StreamWrap { } static void OnConnection(uv_stream_t* handle, int status); + static void AfterConnect(uv_connect_t* req, int status); protected: ConnectionWrap(Environment* env, diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index 8590a0dc9e..77476d52db 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -15,7 +15,6 @@ namespace node { -using v8::Boolean; using v8::Context; using v8::EscapableHandleScope; using v8::External; @@ -23,7 +22,6 @@ using v8::Function; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; -using v8::Integer; using v8::Local; using v8::Object; using v8::Value; @@ -141,44 +139,6 @@ void PipeWrap::Listen(const FunctionCallbackInfo& args) { } -// TODO(bnoordhuis) Maybe share this with TCPWrap? -void PipeWrap::AfterConnect(uv_connect_t* req, int status) { - ConnectWrap* req_wrap = static_cast(req->data); - PipeWrap* wrap = static_cast(req->handle->data); - CHECK_EQ(req_wrap->env(), wrap->env()); - Environment* env = wrap->env(); - - HandleScope handle_scope(env->isolate()); - Context::Scope context_scope(env->context()); - - // The wrap and request objects should still be there. - CHECK_EQ(req_wrap->persistent().IsEmpty(), false); - CHECK_EQ(wrap->persistent().IsEmpty(), false); - - bool readable, writable; - - if (status) { - readable = writable = 0; - } else { - readable = uv_is_readable(req->handle) != 0; - writable = uv_is_writable(req->handle) != 0; - } - - Local req_wrap_obj = req_wrap->object(); - Local argv[5] = { - Integer::New(env->isolate(), status), - wrap->object(), - req_wrap_obj, - Boolean::New(env->isolate(), readable), - Boolean::New(env->isolate(), writable) - }; - - req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv); - - delete req_wrap; -} - - void PipeWrap::Open(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); diff --git a/src/pipe_wrap.h b/src/pipe_wrap.h index 1e11221abc..9dcfa91bac 100644 --- a/src/pipe_wrap.h +++ b/src/pipe_wrap.h @@ -34,8 +34,6 @@ class PipeWrap : public ConnectionWrap { static void SetPendingInstances( const v8::FunctionCallbackInfo& args); #endif - - static void AfterConnect(uv_connect_t* req, int status); }; diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 92037d0d68..f2e972970b 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -235,34 +235,6 @@ void TCPWrap::Listen(const FunctionCallbackInfo& args) { } -void TCPWrap::AfterConnect(uv_connect_t* req, int status) { - ConnectWrap* req_wrap = static_cast(req->data); - TCPWrap* wrap = static_cast(req->handle->data); - CHECK_EQ(req_wrap->env(), wrap->env()); - Environment* env = wrap->env(); - - HandleScope handle_scope(env->isolate()); - Context::Scope context_scope(env->context()); - - // The wrap and request objects should still be there. - CHECK_EQ(req_wrap->persistent().IsEmpty(), false); - CHECK_EQ(wrap->persistent().IsEmpty(), false); - - Local req_wrap_obj = req_wrap->object(); - Local argv[5] = { - Integer::New(env->isolate(), status), - wrap->object(), - req_wrap_obj, - v8::True(env->isolate()), - v8::True(env->isolate()) - }; - - req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv); - - delete req_wrap; -} - - void TCPWrap::Connect(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); diff --git a/src/tcp_wrap.h b/src/tcp_wrap.h index 26eb410c43..2b9e288dce 100644 --- a/src/tcp_wrap.h +++ b/src/tcp_wrap.h @@ -42,8 +42,6 @@ class TCPWrap : public ConnectionWrap { static void SetSimultaneousAccepts( const v8::FunctionCallbackInfo& args); #endif - - static void AfterConnect(uv_connect_t* req, int status); };