GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/connection_wrap.cc Lines: 36 38 94.7 %
Date: 2019-01-07 12:15:22 Branches: 26 48 54.2 %

Line Branch Exec Source
1
#include "connection_wrap.h"
2
3
#include "connect_wrap.h"
4
#include "env-inl.h"
5
#include "pipe_wrap.h"
6
#include "node_internals.h"
7
#include "stream_base-inl.h"
8
#include "stream_wrap.h"
9
#include "tcp_wrap.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
13930
ConnectionWrap<WrapType, UVType>::ConnectionWrap(Environment* env,
25
                                                 Local<Object> object,
26
                                                 ProviderType provider)
27
    : LibuvStreamWrap(env,
28
                      object,
29
                      reinterpret_cast<uv_stream_t*>(&handle_),
30
13930
                      provider) {}
31
32
33
template <typename WrapType, typename UVType>
34
4206
void ConnectionWrap<WrapType, UVType>::OnConnection(uv_stream_t* handle,
35
                                                    int status) {
36
4206
  WrapType* wrap_data = static_cast<WrapType*>(handle->data);
37

4206
  CHECK_NOT_NULL(wrap_data);
38

4206
  CHECK_EQ(&wrap_data->handle_, reinterpret_cast<UVType*>(handle));
39
40
4206
  Environment* env = wrap_data->env();
41
4206
  HandleScope handle_scope(env->isolate());
42

8412
  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

8412
  CHECK_EQ(wrap_data->persistent().IsEmpty(), false);
47
48
  Local<Value> client_handle;
49
50

4206
  if (status == 0) {
51
    // Instantiate the client javascript object and handle.
52
    Local<Object> client_obj = WrapType::Instantiate(env,
53
                                                     wrap_data,
54
4206
                                                     WrapType::SOCKET);
55
56
    // Unwrap the client javascript object.
57
    WrapType* wrap;
58

4206
    ASSIGN_OR_RETURN_UNWRAP(&wrap, client_obj);
59
4206
    uv_stream_t* client = reinterpret_cast<uv_stream_t*>(&wrap->handle_);
60
    // uv_accept can fail if the new connection has already been closed, in
61
    // which case an EAGAIN (resource temporarily unavailable) will be
62
    // returned.
63

4206
    if (uv_accept(handle, client))
64
      return;
65
66
    // Successful accept. Call the onconnection callback in JavaScript land.
67
4206
    client_handle = client_obj;
68
  } else {
69
    client_handle = Undefined(env->isolate());
70
  }
71
72
8412
  Local<Value> argv[] = { Integer::New(env->isolate(), status), client_handle };
73
8412
  wrap_data->MakeCallback(env->onconnection_string(), arraysize(argv), argv);
74
}
75
76
77
template <typename WrapType, typename UVType>
78
4412
void ConnectionWrap<WrapType, UVType>::AfterConnect(uv_connect_t* req,
79
                                                    int status) {
80
  std::unique_ptr<ConnectWrap> req_wrap
81
4412
    (static_cast<ConnectWrap*>(req->data));
82

4412
  CHECK_NOT_NULL(req_wrap);
83
4412
  WrapType* wrap = static_cast<WrapType*>(req->handle->data);
84

4412
  CHECK_EQ(req_wrap->env(), wrap->env());
85
4412
  Environment* env = wrap->env();
86
87
8824
  HandleScope handle_scope(env->isolate());
88
4412
  Context::Scope context_scope(env->context());
89
90
  // The wrap and request objects should still be there.
91

8824
  CHECK_EQ(req_wrap->persistent().IsEmpty(), false);
92

8824
  CHECK_EQ(wrap->persistent().IsEmpty(), false);
93
94
  bool readable, writable;
95
96

4412
  if (status) {
97
415
    readable = writable = 0;
98
  } else {
99
3997
    readable = uv_is_readable(req->handle) != 0;
100
3997
    writable = uv_is_writable(req->handle) != 0;
101
  }
102
103
  Local<Value> argv[5] = {
104
    Integer::New(env->isolate(), status),
105
    wrap->object(),
106
4412
    req_wrap->object(),
107
    Boolean::New(env->isolate(), readable),
108
    Boolean::New(env->isolate(), writable)
109
30884
  };
110
111
8824
  req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
112
4412
}
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