forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: extracting OnConnection from pipe_wrap and tcp_wrap
One of the issues in nodejs#4641 concerns OnConnection in pipe_wrap and tcp_wrap which are very similar with some minor difference in how they are coded. This commit extracts OnConnection so both these classes can share the same implementation.
- Loading branch information
Showing
7 changed files
with
98 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// 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. | ||
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)) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
static void OnConnection(uv_stream_t* handle, int status); | ||
}; | ||
|
||
|
||
} // namespace node | ||
|
||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#endif // SRC_CONNECTION_WRAP_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters