summaryrefslogtreecommitdiff
path: root/src/connection_wrap.cc
diff options
context:
space:
mode:
authorDaniel Bevenius <daniel.bevenius@gmail.com>2016-07-04 19:20:33 +0200
committerAnna Henningsen <anna@addaleax.net>2016-07-25 09:50:51 +0200
commit46633934fed7dfce289298cf16ea0f4161b77bb7 (patch)
treeb0ebe706e4c45deba7e90361c19c6d30b6cac8fd /src/connection_wrap.cc
parentb3127df59ab23baa68e915d62ea1997adb5669e0 (diff)
downloadandroid-node-v8-46633934fed7dfce289298cf16ea0f4161b77bb7.tar.gz
android-node-v8-46633934fed7dfce289298cf16ea0f4161b77bb7.tar.bz2
android-node-v8-46633934fed7dfce289298cf16ea0f4161b77bb7.zip
src: pull OnConnection from pipe_wrap and tcp_wrap
One of the issues in #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. PR-URL: https://github.com/nodejs/node/pull/7547 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'src/connection_wrap.cc')
-rw-r--r--src/connection_wrap.cc93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/connection_wrap.cc b/src/connection_wrap.cc
new file mode 100644
index 0000000000..f98fb79eb9
--- /dev/null
+++ b/src/connection_wrap.cc
@@ -0,0 +1,93 @@
+#include "connection_wrap.h"
+
+#include "env-inl.h"
+#include "env.h"
+#include "pipe_wrap.h"
+#include "stream_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>
+ConnectionWrap<WrapType, UVType>::ConnectionWrap(Environment* env,
+ Local<Object> object,
+ ProviderType provider,
+ AsyncWrap* parent)
+ : StreamWrap(env,
+ object,
+ reinterpret_cast<uv_stream_t*>(&handle_),
+ provider,
+ parent) {}
+
+
+template <typename WrapType, typename UVType>
+void ConnectionWrap<WrapType, UVType>::OnConnection(uv_stream_t* handle,
+ int status) {
+ WrapType* wrap_data = static_cast<WrapType*>(handle->data);
+ CHECK_NE(wrap_data, nullptr);
+ 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 has 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) {
+ // Instantiate 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_);
+ // uv_accept can fail if the new connection has already been closed, in
+ // which case an EAGAIN (resource temporarily unavailable) will be
+ // returned.
+ 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 ConnectionWrap<PipeWrap, uv_pipe_t>::ConnectionWrap(
+ Environment* env,
+ Local<Object> object,
+ ProviderType provider,
+ AsyncWrap* parent);
+
+template ConnectionWrap<TCPWrap, uv_tcp_t>::ConnectionWrap(
+ Environment* env,
+ Local<Object> object,
+ ProviderType provider,
+ AsyncWrap* parent);
+
+template void ConnectionWrap<PipeWrap, uv_pipe_t>::OnConnection(
+ uv_stream_t* handle, int status);
+
+template void ConnectionWrap<TCPWrap, uv_tcp_t>::OnConnection(
+ uv_stream_t* handle, int status);
+
+
+} // namespace node