GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: connection_wrap.cc Lines: 39 42 92.9 %
Date: 2022-08-16 04:20:39 Branches: 17 28 60.7 %

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 "stream_base-inl.h"
7
#include "stream_wrap.h"
8
#include "tcp_wrap.h"
9
#include "util-inl.h"
10
11
namespace node {
12
13
using v8::Boolean;
14
using v8::Context;
15
using v8::HandleScope;
16
using v8::Integer;
17
using v8::Local;
18
using v8::Object;
19
using v8::Value;
20
21
22
template <typename WrapType, typename UVType>
23
38188
ConnectionWrap<WrapType, UVType>::ConnectionWrap(Environment* env,
24
                                                 Local<Object> object,
25
                                                 ProviderType provider)
26
    : LibuvStreamWrap(env,
27
                      object,
28
38188
                      reinterpret_cast<uv_stream_t*>(&handle_),
29
38188
                      provider) {}
30
31
32
template <typename WrapType, typename UVType>
33
10010
void ConnectionWrap<WrapType, UVType>::OnConnection(uv_stream_t* handle,
34
                                                    int status) {
35
10010
  WrapType* wrap_data = static_cast<WrapType*>(handle->data);
36
10010
  CHECK_NOT_NULL(wrap_data);
37
10010
  CHECK_EQ(&wrap_data->handle_, reinterpret_cast<UVType*>(handle));
38
39
10010
  Environment* env = wrap_data->env();
40
10010
  HandleScope handle_scope(env->isolate());
41
10010
  Context::Scope context_scope(env->context());
42
43
  // We should not be getting this callback if someone has already called
44
  // uv_close() on the handle.
45
10010
  CHECK_EQ(wrap_data->persistent().IsEmpty(), false);
46
47
  Local<Value> client_handle;
48
49
10010
  if (status == 0) {
50
    // Instantiate the client javascript object and handle.
51
    Local<Object> client_obj;
52
20020
    if (!WrapType::Instantiate(env, wrap_data, WrapType::SOCKET)
53
             .ToLocal(&client_obj))
54
      return;
55
56
    // Unwrap the client javascript object.
57
    WrapType* wrap;
58
10010
    ASSIGN_OR_RETURN_UNWRAP(&wrap, client_obj);
59
10010
    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
10010
    if (uv_accept(handle, client))
64
      return;
65
66
    // Successful accept. Call the onconnection callback in JavaScript land.
67
10010
    client_handle = client_obj;
68
  } else {
69
    client_handle = Undefined(env->isolate());
70
  }
71
72
10010
  Local<Value> argv[] = { Integer::New(env->isolate(), status), client_handle };
73
10010
  wrap_data->MakeCallback(env->onconnection_string(), arraysize(argv), argv);
74
}
75
76
77
template <typename WrapType, typename UVType>
78
11164
void ConnectionWrap<WrapType, UVType>::AfterConnect(uv_connect_t* req,
79
                                                    int status) {
80
11164
  std::unique_ptr<ConnectWrap> req_wrap
81
11164
    (static_cast<ConnectWrap*>(req->data));
82
11164
  CHECK_NOT_NULL(req_wrap);
83
11164
  WrapType* wrap = static_cast<WrapType*>(req->handle->data);
84
11164
  CHECK_EQ(req_wrap->env(), wrap->env());
85
11164
  Environment* env = wrap->env();
86
87
22328
  HandleScope handle_scope(env->isolate());
88
11164
  Context::Scope context_scope(env->context());
89
90
  // The wrap and request objects should still be there.
91
11164
  CHECK_EQ(req_wrap->persistent().IsEmpty(), false);
92
11164
  CHECK_EQ(wrap->persistent().IsEmpty(), false);
93
94
  bool readable, writable;
95
96
11164
  if (status) {
97
828
    readable = writable = false;
98
  } else {
99
10336
    readable = uv_is_readable(req->handle) != 0;
100
10336
    writable = uv_is_writable(req->handle) != 0;
101
  }
102
103
66984
  Local<Value> argv[5] = {
104
    Integer::New(env->isolate(), status),
105
    wrap->object(),
106
11164
    req_wrap->object(),
107
    Boolean::New(env->isolate(), readable),
108
    Boolean::New(env->isolate(), writable)
109
  };
110
111

13088
  TRACE_EVENT_NESTABLE_ASYNC_END1(TRACING_CATEGORY_NODE2(net, native),
112
                                  "connect",
113
                                  req_wrap.get(),
114
                                  "status",
115
                                  status);
116
117
11164
  req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
118
11164
}
119
120
template ConnectionWrap<PipeWrap, uv_pipe_t>::ConnectionWrap(
121
    Environment* env,
122
    Local<Object> object,
123
    ProviderType provider);
124
125
template ConnectionWrap<TCPWrap, uv_tcp_t>::ConnectionWrap(
126
    Environment* env,
127
    Local<Object> object,
128
    ProviderType provider);
129
130
template void ConnectionWrap<PipeWrap, uv_pipe_t>::OnConnection(
131
    uv_stream_t* handle, int status);
132
133
template void ConnectionWrap<TCPWrap, uv_tcp_t>::OnConnection(
134
    uv_stream_t* handle, int status);
135
136
template void ConnectionWrap<PipeWrap, uv_pipe_t>::AfterConnect(
137
    uv_connect_t* handle, int status);
138
139
template void ConnectionWrap<TCPWrap, uv_tcp_t>::AfterConnect(
140
    uv_connect_t* handle, int status);
141
142
143
}  // namespace node