GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/connection_wrap.cc Lines: 36 36 100.0 %
Date: 2017-01-02 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 "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
7711
ConnectionWrap<WrapType, UVType>::ConnectionWrap(Environment* env,
25
                                                 Local<Object> object,
26
                                                 ProviderType provider,
27
                                                 AsyncWrap* parent)
28
    : StreamWrap(env,
29
                 object,
30
                 reinterpret_cast<uv_stream_t*>(&handle_),
31
                 provider,
32
7711
                 parent) {}
33
34
35
template <typename WrapType, typename UVType>
36
3041
void ConnectionWrap<WrapType, UVType>::OnConnection(uv_stream_t* handle,
37
                                                    int status) {
38
3041
  WrapType* wrap_data = static_cast<WrapType*>(handle->data);
39

3041
  CHECK_NE(wrap_data, nullptr);
40

3041
  CHECK_EQ(&wrap_data->handle_, reinterpret_cast<UVType*>(handle));
41
42
3041
  Environment* env = wrap_data->env();
43
6082
  HandleScope handle_scope(env->isolate());
44
9123
  Context::Scope context_scope(env->context());
45
46
  // We should not be getting this callback if someone has already called
47
  // uv_close() on the handle.
48

6082
  CHECK_EQ(wrap_data->persistent().IsEmpty(), false);
49
50
  Local<Value> argv[] = {
51
    Integer::New(env->isolate(), status),
52
    Undefined(env->isolate())
53
12164
  };
54
55

3041
  if (status == 0) {
56
    // Instantiate the client javascript object and handle.
57
3041
    Local<Object> client_obj = WrapType::Instantiate(env, wrap_data);
58
59
    // Unwrap the client javascript object.
60
    WrapType* wrap;
61

3041
    ASSIGN_OR_RETURN_UNWRAP(&wrap, client_obj);
62
    uv_stream_t* client_handle =
63
3041
        reinterpret_cast<uv_stream_t*>(&wrap->handle_);
64
    // uv_accept can fail if the new connection has already been closed, in
65
    // which case an EAGAIN (resource temporarily unavailable) will be
66
    // returned.
67

3041
    if (uv_accept(handle, client_handle))
68
      return;
69
70
    // Successful accept. Call the onconnection callback in JavaScript land.
71
3041
    argv[1] = client_obj;
72
  }
73
6082
  wrap_data->MakeCallback(env->onconnection_string(), arraysize(argv), argv);
74
}
75
76
77
template <typename WrapType, typename UVType>
78
1937
void ConnectionWrap<WrapType, UVType>::AfterConnect(uv_connect_t* req,
79
                                                    int status) {
80
1937
  ConnectWrap* req_wrap = static_cast<ConnectWrap*>(req->data);
81

1937
  CHECK_NE(req_wrap, nullptr);
82
1937
  WrapType* wrap = static_cast<WrapType*>(req->handle->data);
83

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

3874
  CHECK_EQ(req_wrap->persistent().IsEmpty(), false);
91

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

1937
  if (status) {
96
    readable = writable = 0;
97
  } else {
98
1896
    readable = uv_is_readable(req->handle) != 0;
99
1896
    writable = uv_is_writable(req->handle) != 0;
100
  }
101
102
3874
  Local<Object> req_wrap_obj = req_wrap->object();
103
  Local<Value> argv[5] = {
104
    Integer::New(env->isolate(), status),
105
    wrap->object(),
106
    req_wrap_obj,
107
    Boolean::New(env->isolate(), readable),
108
    Boolean::New(env->isolate(), writable)
109
17433
  };
110
111
3874
  req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
112
113

1937
  delete req_wrap;
114
1937
}
115
116
template ConnectionWrap<PipeWrap, uv_pipe_t>::ConnectionWrap(
117
    Environment* env,
118
    Local<Object> object,
119
    ProviderType provider,
120
    AsyncWrap* parent);
121
122
template ConnectionWrap<TCPWrap, uv_tcp_t>::ConnectionWrap(
123
    Environment* env,
124
    Local<Object> object,
125
    ProviderType provider,
126
    AsyncWrap* parent);
127
128
template void ConnectionWrap<PipeWrap, uv_pipe_t>::OnConnection(
129
    uv_stream_t* handle, int status);
130
131
template void ConnectionWrap<TCPWrap, uv_tcp_t>::OnConnection(
132
    uv_stream_t* handle, int status);
133
134
template void ConnectionWrap<PipeWrap, uv_pipe_t>::AfterConnect(
135
    uv_connect_t* handle, int status);
136
137
template void ConnectionWrap<TCPWrap, uv_tcp_t>::AfterConnect(
138
    uv_connect_t* handle, int status);
139
140
141
}  // namespace node