Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
connection_wrap.cc
Go to the documentation of this file.
1 #include "connection_wrap.h"
2 
3 #include "connect_wrap.h"
4 #include "env-inl.h"
5 #include "env.h"
6 #include "pipe_wrap.h"
7 #include "stream_wrap.h"
8 #include "tcp_wrap.h"
9 #include "util.h"
10 #include "util-inl.h"
11 
12 namespace node {
13 
14 using v8::Boolean;
15 using v8::Context;
16 using v8::HandleScope;
17 using v8::Integer;
18 using v8::Local;
19 using v8::Object;
20 using v8::Value;
21 
22 
23 template <typename WrapType, typename UVType>
24 ConnectionWrap<WrapType, UVType>::ConnectionWrap(Environment* env,
25  Local<Object> object,
26  ProviderType provider)
27  : StreamWrap(env,
28  object,
29  reinterpret_cast<uv_stream_t*>(&handle_),
30  provider) {}
31 
32 
33 template <typename WrapType, typename UVType>
34 void ConnectionWrap<WrapType, UVType>::OnConnection(uv_stream_t* handle,
35  int status) {
36  WrapType* wrap_data = static_cast<WrapType*>(handle->data);
37  CHECK_NE(wrap_data, nullptr);
38  CHECK_EQ(&wrap_data->handle_, reinterpret_cast<UVType*>(handle));
39 
40  Environment* env = wrap_data->env();
41  HandleScope handle_scope(env->isolate());
42  Context::Scope context_scope(env->context());
43 
44  // We should not be getting this callback if someone has already called
45  // uv_close() on the handle.
46  CHECK_EQ(wrap_data->persistent().IsEmpty(), false);
47 
48  Local<Value> argv[] = {
49  Integer::New(env->isolate(), status),
50  Undefined(env->isolate())
51  };
52 
53  if (status == 0) {
54  env->set_init_trigger_id(wrap_data->get_id());
55  // Instantiate the client javascript object and handle.
56  Local<Object> client_obj = WrapType::Instantiate(env, wrap_data);
57 
58  // Unwrap the client javascript object.
59  WrapType* wrap;
60  ASSIGN_OR_RETURN_UNWRAP(&wrap, client_obj);
61  uv_stream_t* client_handle =
62  reinterpret_cast<uv_stream_t*>(&wrap->handle_);
63  // uv_accept can fail if the new connection has already been closed, in
64  // which case an EAGAIN (resource temporarily unavailable) will be
65  // returned.
66  if (uv_accept(handle, client_handle))
67  return;
68 
69  // Successful accept. Call the onconnection callback in JavaScript land.
70  argv[1] = client_obj;
71  }
72  wrap_data->MakeCallback(env->onconnection_string(), arraysize(argv), argv);
73 }
74 
75 
76 template <typename WrapType, typename UVType>
77 void ConnectionWrap<WrapType, UVType>::AfterConnect(uv_connect_t* req,
78  int status) {
79  ConnectWrap* req_wrap = static_cast<ConnectWrap*>(req->data);
80  CHECK_NE(req_wrap, nullptr);
81  WrapType* wrap = static_cast<WrapType*>(req->handle->data);
82  CHECK_EQ(req_wrap->env(), wrap->env());
83  Environment* env = wrap->env();
84 
85  HandleScope handle_scope(env->isolate());
86  Context::Scope context_scope(env->context());
87 
88  // The wrap and request objects should still be there.
89  CHECK_EQ(req_wrap->persistent().IsEmpty(), false);
90  CHECK_EQ(wrap->persistent().IsEmpty(), false);
91 
92  bool readable, writable;
93 
94  if (status) {
95  readable = writable = 0;
96  } else {
97  readable = uv_is_readable(req->handle) != 0;
98  writable = uv_is_writable(req->handle) != 0;
99  }
100 
101  Local<Value> argv[5] = {
102  Integer::New(env->isolate(), status),
103  wrap->object(),
104  req_wrap->object(),
105  Boolean::New(env->isolate(), readable),
106  Boolean::New(env->isolate(), writable)
107  };
108 
109  req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
110 
111  delete req_wrap;
112 }
113 
114 template ConnectionWrap<PipeWrap, uv_pipe_t>::ConnectionWrap(
115  Environment* env,
116  Local<Object> object,
117  ProviderType provider);
118 
119 template ConnectionWrap<TCPWrap, uv_tcp_t>::ConnectionWrap(
120  Environment* env,
121  Local<Object> object,
122  ProviderType provider);
123 
124 template void ConnectionWrap<PipeWrap, uv_pipe_t>::OnConnection(
125  uv_stream_t* handle, int status);
126 
127 template void ConnectionWrap<TCPWrap, uv_tcp_t>::OnConnection(
128  uv_stream_t* handle, int status);
129 
130 template void ConnectionWrap<PipeWrap, uv_pipe_t>::AfterConnect(
131  uv_connect_t* handle, int status);
132 
133 template void ConnectionWrap<TCPWrap, uv_tcp_t>::AfterConnect(
134  uv_connect_t* handle, int status);
135 
136 
137 } // namespace node
QueryWrap * wrap
Definition: cares_wrap.cc:478
int status
Definition: cares_wrap.cc:479
uv_fs_t req
Definition: node_file.cc:374
MaybeLocal< Object > New(Isolate *isolate, Local< String > string, enum encoding enc)
Definition: node_buffer.cc:241