-
-
Notifications
You must be signed in to change notification settings - Fork 35.9k
src: extracting OnConnection from pipe_wrap and tcp_wrap #7547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
ad6dee3
a3b976e
34d5abc
564342d
623ad2e
458accc
ee0f2b9
5562c45
ee62109
579f48a
1437ec6
d0da4bc
1b3be60
e364ec4
e8ed621
7ce112a
c9ec24d
5ef85f9
ece2d31
73c956d
4feb84a
0a1ff38
85af1a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| #include "connection_wrap.h" | ||
|
|
||
| #include "env-inl.h" | ||
| #include "env.h" | ||
| #include "pipe_wrap.h" | ||
| #include "tcp_wrap.h" | ||
| #include "util.h" | ||
| #include "util-inl.h" | ||
|
|
||
| namespace node { | ||
|
|
||
| using v8::Context; | ||
| using v8::HandleScope; | ||
| using v8::Integer; | ||
| using v8::Local; | ||
| using v8::Object; | ||
| using v8::Value; | ||
|
|
||
|
|
||
| template<typename WrapType, typename UVType> | ||
| void ConnectionWrap::OnConnection(uv_stream_t* handle, int status) { | ||
| WrapType* wrap_data = static_cast<WrapType*>(handle->data); | ||
| CHECK_EQ(&wrap_data->handle_, reinterpret_cast<UVType*>(handle)); | ||
|
|
||
| Environment* env = wrap_data->env(); | ||
| HandleScope handle_scope(env->isolate()); | ||
| Context::Scope context_scope(env->context()); | ||
|
|
||
| // We should not be getting this callback if someone as already called | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While you’re at it, I realize it’s copypasted but there’s a typo here:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I missed that, I'll correct it. |
||
| // uv_close() on the handle. | ||
| CHECK_EQ(wrap_data->persistent().IsEmpty(), false); | ||
|
|
||
| Local<Value> argv[] = { | ||
| Integer::New(env->isolate(), status), | ||
| Undefined(env->isolate()) | ||
| }; | ||
|
|
||
| if (status == 0) { | ||
| // Instanciate the client javascript object and handle. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instantiate? I realize it's an existing comment but since you're here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problems, I'll correct this. |
||
| Local<Object> client_obj = WrapType::Instantiate(env, wrap_data); | ||
|
|
||
| // Unwrap the client javascript object. | ||
| WrapType* wrap; | ||
| ASSIGN_OR_RETURN_UNWRAP(&wrap, client_obj); | ||
| uv_stream_t* client_handle = reinterpret_cast<uv_stream_t*>(&wrap->handle_); | ||
| if (uv_accept(handle, client_handle)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While you're here, can you add a comment explaining that uv_accept() can fail when the new connection is already closed again by the peer?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem, I'll add a comment. |
||
| return; | ||
|
|
||
| // Successful accept. Call the onconnection callback in JavaScript land. | ||
| argv[1] = client_obj; | ||
| } | ||
| wrap_data->MakeCallback(env->onconnection_string(), arraysize(argv), argv); | ||
| } | ||
|
|
||
| template void ConnectionWrap::OnConnection<PipeWrap, uv_pipe_t>( | ||
| uv_stream_t* handle, | ||
| int status); | ||
|
|
||
| template void ConnectionWrap::OnConnection<TCPWrap, uv_tcp_t>( | ||
| uv_stream_t* handle, | ||
| int status); | ||
|
|
||
|
|
||
| } // namespace node | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #ifndef SRC_CONNECTION_WRAP_H_ | ||
| #define SRC_CONNECTION_WRAP_H_ | ||
|
|
||
| #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
|
||
| #include "env.h" | ||
| #include "v8.h" | ||
|
|
||
| namespace node { | ||
|
|
||
| class ConnectionWrap { | ||
| protected: | ||
| template<typename WrapType, typename UVType> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be nicer to have the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, that does sound better. Let me give that a try and see what you think. Thanks |
||
| static void OnConnection(uv_stream_t* handle, int status); | ||
| }; | ||
|
|
||
|
|
||
| } // namespace node | ||
|
|
||
| #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
|
||
| #endif // SRC_CONNECTION_WRAP_H_ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,11 @@ | |
| #include "async-wrap.h" | ||
| #include "env.h" | ||
| #include "stream_wrap.h" | ||
| #include "connection_wrap.h" | ||
|
|
||
| namespace node { | ||
|
|
||
| class PipeWrap : public StreamWrap { | ||
| class PipeWrap : public StreamWrap, ConnectionWrap { | ||
| public: | ||
| uv_pipe_t* UVHandle(); | ||
|
|
||
|
|
@@ -21,6 +22,7 @@ class PipeWrap : public StreamWrap { | |
| size_t self_size() const override { return sizeof(*this); } | ||
|
|
||
| private: | ||
| friend class ConnectionWrap; // So handle_ can be accessed | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. uvhandle_?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm just about to push a few commits, one of which changes the name back to handle_, but also one that removes this friend class as it was no longer required. |
||
| PipeWrap(Environment* env, | ||
| v8::Local<v8::Object> object, | ||
| bool ipc, | ||
|
|
@@ -37,7 +39,6 @@ class PipeWrap : public StreamWrap { | |
| const v8::FunctionCallbackInfo<v8::Value>& args); | ||
| #endif | ||
|
|
||
| static void OnConnection(uv_stream_t* handle, int status); | ||
| static void AfterConnect(uv_connect_t* req, int status); | ||
|
|
||
| uv_pipe_t handle_; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,11 @@ | |
| #include "async-wrap.h" | ||
| #include "env.h" | ||
| #include "stream_wrap.h" | ||
| #include "connection_wrap.h" | ||
|
|
||
| namespace node { | ||
|
|
||
| class TCPWrap : public StreamWrap { | ||
| class TCPWrap : public StreamWrap, ConnectionWrap { | ||
| public: | ||
| static v8::Local<v8::Object> Instantiate(Environment* env, AsyncWrap* parent); | ||
| static void Initialize(v8::Local<v8::Object> target, | ||
|
|
@@ -21,6 +22,7 @@ class TCPWrap : public StreamWrap { | |
| size_t self_size() const override { return sizeof(*this); } | ||
|
|
||
| private: | ||
| friend class ConnectionWrap; // So handle_ can be accessed | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No longer necessary, is it?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah good point, I'll remove this. |
||
| typedef uv_tcp_t HandleType; | ||
|
|
||
| template <typename T, | ||
|
|
@@ -45,7 +47,6 @@ class TCPWrap : public StreamWrap { | |
| const v8::FunctionCallbackInfo<v8::Value>& args); | ||
| #endif | ||
|
|
||
| static void OnConnection(uv_stream_t* handle, int status); | ||
| static void AfterConnect(uv_connect_t* req, int status); | ||
|
|
||
| uv_tcp_t handle_; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tiny style nit: space before
<.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll correct this.