Skip to content

Commit 0d24247

Browse files
danbevFishrock123
authored andcommitted
src: pull AfterConnect from pipe_wrap and tcp_wrap
This commit attempts to address one of the items in #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: #8448 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
1 parent 1620226 commit 0d24247

File tree

6 files changed

+49
-72
lines changed

6 files changed

+49
-72
lines changed

src/connection_wrap.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "connection_wrap.h"
22

3+
#include "connect_wrap.h"
34
#include "env-inl.h"
45
#include "env.h"
56
#include "pipe_wrap.h"
@@ -10,6 +11,7 @@
1011

1112
namespace node {
1213

14+
using v8::Boolean;
1315
using v8::Context;
1416
using v8::HandleScope;
1517
using v8::Integer;
@@ -71,6 +73,46 @@ void ConnectionWrap<WrapType, UVType>::OnConnection(uv_stream_t* handle,
7173
wrap_data->MakeCallback(env->onconnection_string(), arraysize(argv), argv);
7274
}
7375

76+
77+
template <typename WrapType, typename UVType>
78+
void ConnectionWrap<WrapType, UVType>::AfterConnect(uv_connect_t* req,
79+
int status) {
80+
ConnectWrap* req_wrap = static_cast<ConnectWrap*>(req->data);
81+
CHECK_NE(req_wrap, nullptr);
82+
WrapType* wrap = static_cast<WrapType*>(req->handle->data);
83+
CHECK_EQ(req_wrap->env(), wrap->env());
84+
Environment* env = wrap->env();
85+
86+
HandleScope handle_scope(env->isolate());
87+
Context::Scope context_scope(env->context());
88+
89+
// The wrap and request objects should still be there.
90+
CHECK_EQ(req_wrap->persistent().IsEmpty(), false);
91+
CHECK_EQ(wrap->persistent().IsEmpty(), false);
92+
93+
bool readable, writable;
94+
95+
if (status) {
96+
readable = writable = 0;
97+
} else {
98+
readable = uv_is_readable(req->handle) != 0;
99+
writable = uv_is_writable(req->handle) != 0;
100+
}
101+
102+
Local<Object> req_wrap_obj = req_wrap->object();
103+
Local<Value> argv[5] = {
104+
Integer::New(env->isolate(), status),
105+
wrap->object(),
106+
req_wrap_obj,
107+
Boolean::New(env->isolate(), readable),
108+
Boolean::New(env->isolate(), writable)
109+
};
110+
111+
req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
112+
113+
delete req_wrap;
114+
}
115+
74116
template ConnectionWrap<PipeWrap, uv_pipe_t>::ConnectionWrap(
75117
Environment* env,
76118
Local<Object> object,
@@ -89,5 +131,11 @@ template void ConnectionWrap<PipeWrap, uv_pipe_t>::OnConnection(
89131
template void ConnectionWrap<TCPWrap, uv_tcp_t>::OnConnection(
90132
uv_stream_t* handle, int status);
91133

134+
template void ConnectionWrap<PipeWrap, uv_pipe_t>::AfterConnect(
135+
uv_connect_t* handle, int status);
136+
137+
template void ConnectionWrap<TCPWrap, uv_tcp_t>::AfterConnect(
138+
uv_connect_t* handle, int status);
139+
92140

93141
} // namespace node

src/connection_wrap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ConnectionWrap : public StreamWrap {
1717
}
1818

1919
static void OnConnection(uv_stream_t* handle, int status);
20+
static void AfterConnect(uv_connect_t* req, int status);
2021

2122
protected:
2223
ConnectionWrap(Environment* env,

src/pipe_wrap.cc

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515

1616
namespace node {
1717

18-
using v8::Boolean;
1918
using v8::Context;
2019
using v8::EscapableHandleScope;
2120
using v8::External;
2221
using v8::Function;
2322
using v8::FunctionCallbackInfo;
2423
using v8::FunctionTemplate;
2524
using v8::HandleScope;
26-
using v8::Integer;
2725
using v8::Local;
2826
using v8::Object;
2927
using v8::Value;
@@ -141,44 +139,6 @@ void PipeWrap::Listen(const FunctionCallbackInfo<Value>& args) {
141139
}
142140

143141

144-
// TODO(bnoordhuis) Maybe share this with TCPWrap?
145-
void PipeWrap::AfterConnect(uv_connect_t* req, int status) {
146-
ConnectWrap* req_wrap = static_cast<ConnectWrap*>(req->data);
147-
PipeWrap* wrap = static_cast<PipeWrap*>(req->handle->data);
148-
CHECK_EQ(req_wrap->env(), wrap->env());
149-
Environment* env = wrap->env();
150-
151-
HandleScope handle_scope(env->isolate());
152-
Context::Scope context_scope(env->context());
153-
154-
// The wrap and request objects should still be there.
155-
CHECK_EQ(req_wrap->persistent().IsEmpty(), false);
156-
CHECK_EQ(wrap->persistent().IsEmpty(), false);
157-
158-
bool readable, writable;
159-
160-
if (status) {
161-
readable = writable = 0;
162-
} else {
163-
readable = uv_is_readable(req->handle) != 0;
164-
writable = uv_is_writable(req->handle) != 0;
165-
}
166-
167-
Local<Object> req_wrap_obj = req_wrap->object();
168-
Local<Value> argv[5] = {
169-
Integer::New(env->isolate(), status),
170-
wrap->object(),
171-
req_wrap_obj,
172-
Boolean::New(env->isolate(), readable),
173-
Boolean::New(env->isolate(), writable)
174-
};
175-
176-
req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
177-
178-
delete req_wrap;
179-
}
180-
181-
182142
void PipeWrap::Open(const FunctionCallbackInfo<Value>& args) {
183143
Environment* env = Environment::GetCurrent(args);
184144

src/pipe_wrap.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ class PipeWrap : public ConnectionWrap<PipeWrap, uv_pipe_t> {
3434
static void SetPendingInstances(
3535
const v8::FunctionCallbackInfo<v8::Value>& args);
3636
#endif
37-
38-
static void AfterConnect(uv_connect_t* req, int status);
3937
};
4038

4139

src/tcp_wrap.cc

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -235,34 +235,6 @@ void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
235235
}
236236

237237

238-
void TCPWrap::AfterConnect(uv_connect_t* req, int status) {
239-
ConnectWrap* req_wrap = static_cast<ConnectWrap*>(req->data);
240-
TCPWrap* wrap = static_cast<TCPWrap*>(req->handle->data);
241-
CHECK_EQ(req_wrap->env(), wrap->env());
242-
Environment* env = wrap->env();
243-
244-
HandleScope handle_scope(env->isolate());
245-
Context::Scope context_scope(env->context());
246-
247-
// The wrap and request objects should still be there.
248-
CHECK_EQ(req_wrap->persistent().IsEmpty(), false);
249-
CHECK_EQ(wrap->persistent().IsEmpty(), false);
250-
251-
Local<Object> req_wrap_obj = req_wrap->object();
252-
Local<Value> argv[5] = {
253-
Integer::New(env->isolate(), status),
254-
wrap->object(),
255-
req_wrap_obj,
256-
v8::True(env->isolate()),
257-
v8::True(env->isolate())
258-
};
259-
260-
req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
261-
262-
delete req_wrap;
263-
}
264-
265-
266238
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) {
267239
Environment* env = Environment::GetCurrent(args);
268240

src/tcp_wrap.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
4242
static void SetSimultaneousAccepts(
4343
const v8::FunctionCallbackInfo<v8::Value>& args);
4444
#endif
45-
46-
static void AfterConnect(uv_connect_t* req, int status);
4745
};
4846

4947

0 commit comments

Comments
 (0)