GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/stream_wrap.cc Lines: 157 162 96.9 %
Date: 2016-12-16 Branches: 49 72 68.1 %

Line Branch Exec Source
1
#include "stream_wrap.h"
2
#include "stream_base.h"
3
#include "stream_base-inl.h"
4
5
#include "env-inl.h"
6
#include "env.h"
7
#include "handle_wrap.h"
8
#include "node_buffer.h"
9
#include "node_counters.h"
10
#include "pipe_wrap.h"
11
#include "req-wrap.h"
12
#include "req-wrap-inl.h"
13
#include "tcp_wrap.h"
14
#include "udp_wrap.h"
15
#include "util.h"
16
#include "util-inl.h"
17
18
#include <stdlib.h>  // abort()
19
#include <string.h>  // memcpy()
20
#include <limits.h>  // INT_MAX
21
22
23
namespace node {
24
25
using v8::Context;
26
using v8::EscapableHandleScope;
27
using v8::FunctionCallbackInfo;
28
using v8::FunctionTemplate;
29
using v8::HandleScope;
30
using v8::Integer;
31
using v8::Local;
32
using v8::Object;
33
using v8::Value;
34
35
36
1671
void StreamWrap::Initialize(Local<Object> target,
37
                            Local<Value> unused,
38
                            Local<Context> context) {
39
1671
  Environment* env = Environment::GetCurrent(context);
40
41
  Local<FunctionTemplate> sw =
42
3342
      FunctionTemplate::New(env->isolate(), ShutdownWrap::NewShutdownWrap);
43
3342
  sw->InstanceTemplate()->SetInternalFieldCount(1);
44
1671
  sw->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"));
45
6684
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"),
46
1671
              sw->GetFunction());
47
48
  Local<FunctionTemplate> ww =
49
3342
      FunctionTemplate::New(env->isolate(), WriteWrap::NewWriteWrap);
50
3342
  ww->InstanceTemplate()->SetInternalFieldCount(1);
51
1671
  ww->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"));
52
6684
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"),
53
1671
              ww->GetFunction());
54
1671
  env->set_write_wrap_constructor_function(ww->GetFunction());
55
1671
}
56
57
58
7697
StreamWrap::StreamWrap(Environment* env,
59
                       Local<Object> object,
60
                       uv_stream_t* stream,
61
                       AsyncWrap::ProviderType provider,
62
                       AsyncWrap* parent)
63
    : HandleWrap(env,
64
                 object,
65
                 reinterpret_cast<uv_handle_t*>(stream),
66
                 provider,
67
                 parent),
68
      StreamBase(env),
69
15394
      stream_(stream) {
70
15394
  set_after_write_cb({ OnAfterWriteImpl, this });
71
15394
  set_alloc_cb({ OnAllocImpl, this });
72
7697
  set_read_cb({ OnReadImpl, this });
73
7697
}
74
75
76
5022
void StreamWrap::AddMethods(Environment* env,
77
                            v8::Local<v8::FunctionTemplate> target,
78
                            int flags) {
79
5022
  env->SetProtoMethod(target, "setBlocking", SetBlocking);
80
5022
  StreamBase::AddMethods<StreamWrap>(env, target, flags);
81
5022
}
82
83
84
15
int StreamWrap::GetFD() {
85
15
  int fd = -1;
86
#if !defined(_WIN32)
87
15
  if (stream() != nullptr)
88
15
    uv_fileno(reinterpret_cast<uv_handle_t*>(stream()), &fd);
89
#endif
90
15
  return fd;
91
}
92
93
94
37294
bool StreamWrap::IsAlive() {
95
74588
  return HandleWrap::IsAlive(this);
96
}
97
98
99
3
bool StreamWrap::IsClosing() {
100
3
  return uv_is_closing(reinterpret_cast<uv_handle_t*>(stream()));
101
}
102
103
104
void* StreamWrap::Cast() {
105
  return reinterpret_cast<void*>(this);
106
}
107
108
109
34537
AsyncWrap* StreamWrap::GetAsyncWrap() {
110
34537
  return static_cast<AsyncWrap*>(this);
111
}
112
113
114
14919
bool StreamWrap::IsIPCPipe() {
115
14919
  return is_named_pipe_ipc();
116
}
117
118
119
34387
void StreamWrap::UpdateWriteQueueSize() {
120
68774
  HandleScope scope(env()->isolate());
121
  Local<Integer> write_queue_size =
122
34387
      Integer::NewFromUnsigned(env()->isolate(), stream()->write_queue_size);
123
137548
  object()->Set(env()->write_queue_size_string(), write_queue_size);
124
34387
}
125
126
127
6774
int StreamWrap::ReadStart() {
128
6774
  return uv_read_start(stream(), OnAlloc, OnRead);
129
}
130
131
132
249
int StreamWrap::ReadStop() {
133
249
  return uv_read_stop(stream());
134
}
135
136
137
24871
void StreamWrap::OnAlloc(uv_handle_t* handle,
138
                         size_t suggested_size,
139
                         uv_buf_t* buf) {
140
24871
  StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);
141
49742
  HandleScope scope(wrap->env()->isolate());
142
99484
  Context::Scope context_scope(wrap->env()->context());
143
144
24871
  CHECK_EQ(wrap->stream(), reinterpret_cast<uv_stream_t*>(handle));
145
146
74613
  return static_cast<StreamBase*>(wrap)->OnAlloc(suggested_size, buf);
147
}
148
149
150
19410
void StreamWrap::OnAllocImpl(size_t size, uv_buf_t* buf, void* ctx) {
151
19410
  buf->base = node::Malloc(size);
152
19410
  buf->len = size;
153
19410
}
154
155
156
template <class WrapType, class UVType>
157
135
static Local<Object> AcceptHandle(Environment* env, StreamWrap* parent) {
158
270
  EscapableHandleScope scope(env->isolate());
159
135
  Local<Object> wrap_obj;
160
  UVType* handle;
161
162
135
  wrap_obj = WrapType::Instantiate(env, parent);
163

135
  if (wrap_obj.IsEmpty())
164
    return Local<Object>();
165
166
  WrapType* wrap;
167

135
  ASSIGN_OR_RETURN_UNWRAP(&wrap, wrap_obj, Local<Object>());
168
258
  handle = wrap->UVHandle();
169
170

135
  if (uv_accept(parent->stream(), reinterpret_cast<uv_stream_t*>(handle)))
171
    ABORT();
172
173
  return scope.Escape(wrap_obj);
174
}
175
176
177
19628
void StreamWrap::OnReadImpl(ssize_t nread,
178
                            const uv_buf_t* buf,
179
                            uv_handle_type pending,
180
                            void* ctx) {
181
19628
  StreamWrap* wrap = static_cast<StreamWrap*>(ctx);
182
19628
  Environment* env = wrap->env();
183
37237
  HandleScope handle_scope(env->isolate());
184
56865
  Context::Scope context_scope(env->context());
185
186
19628
  Local<Object> pending_obj;
187
188
19628
  if (nread < 0)  {
189
1970
    if (buf->base != nullptr)
190
1752
      free(buf->base);
191
1970
    wrap->EmitData(nread, Local<Object>(), pending_obj);
192
3918
    return;
193
  }
194
195
17658
  if (nread == 0) {
196
10
    if (buf->base != nullptr)
197
10
      free(buf->base);
198
    return;
199
  }
200
201
17648
  CHECK_LE(static_cast<size_t>(nread), buf->len);
202
17648
  char* base = node::Realloc(buf->base, nread);
203
204
17648
  if (pending == UV_TCP) {
205
122
    pending_obj = AcceptHandle<TCPWrap, uv_tcp_t>(env, wrap);
206
17526
  } else if (pending == UV_NAMED_PIPE) {
207
1
    pending_obj = AcceptHandle<PipeWrap, uv_pipe_t>(env, wrap);
208
17525
  } else if (pending == UV_UDP) {
209
12
    pending_obj = AcceptHandle<UDPWrap, uv_udp_t>(env, wrap);
210
  } else {
211
17513
    CHECK_EQ(pending, UV_UNKNOWN_HANDLE);
212
  }
213
214
35296
  Local<Object> obj = Buffer::New(env, base, nread).ToLocalChecked();
215
17648
  wrap->EmitData(nread, obj, pending_obj);
216
}
217
218
219
25003
void StreamWrap::OnReadCommon(uv_stream_t* handle,
220
                              ssize_t nread,
221
                              const uv_buf_t* buf,
222
                              uv_handle_type pending) {
223
25003
  StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);
224
49946
  HandleScope scope(wrap->env()->isolate());
225
99952
  Context::Scope context_scope(wrap->env()->context());
226
227
  // We should not be getting this callback if someone as already called
228
  // uv_close() on the handle.
229
50006
  CHECK_EQ(wrap->persistent().IsEmpty(), false);
230
231
  if (nread > 0) {
232
    if (wrap->is_tcp()) {
233
      NODE_COUNT_NET_BYTES_RECV(nread);
234
    } else if (wrap->is_named_pipe()) {
235
      NODE_COUNT_PIPE_BYTES_RECV(nread);
236
    }
237
  }
238
239
49946
  static_cast<StreamBase*>(wrap)->OnRead(nread, buf, pending);
240
24943
}
241
242
243
25003
void StreamWrap::OnRead(uv_stream_t* handle,
244
                        ssize_t nread,
245
                        const uv_buf_t* buf) {
246
25003
  StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);
247
25003
  uv_handle_type type = UV_UNKNOWN_HANDLE;
248
249

26064
  if (wrap->is_named_pipe_ipc() &&
250
1061
      uv_pipe_pending_count(reinterpret_cast<uv_pipe_t*>(handle)) > 0) {
251
135
    type = uv_pipe_pending_type(reinterpret_cast<uv_pipe_t*>(handle));
252
  }
253
254
25003
  OnReadCommon(handle, nread, buf, type);
255
24943
}
256
257
258
10
void StreamWrap::SetBlocking(const FunctionCallbackInfo<Value>& args) {
259
  StreamWrap* wrap;
260
10
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
261
262
10
  CHECK_GT(args.Length(), 0);
263
10
  if (!wrap->IsAlive())
264
    return args.GetReturnValue().Set(UV_EINVAL);
265
266
10
  bool enable = args[0]->IsTrue();
267
30
  args.GetReturnValue().Set(uv_stream_set_blocking(wrap->stream(), enable));
268
}
269
270
271
2995
int StreamWrap::DoShutdown(ShutdownWrap* req_wrap) {
272
  int err;
273
5990
  err = uv_shutdown(req_wrap->req(), stream(), AfterShutdown);
274
5990
  req_wrap->Dispatched();
275
2995
  return err;
276
}
277
278
279
2989
void StreamWrap::AfterShutdown(uv_shutdown_t* req, int status) {
280
2989
  ShutdownWrap* req_wrap = ShutdownWrap::from_req(req);
281
2989
  CHECK_NE(req_wrap, nullptr);
282
5978
  HandleScope scope(req_wrap->env()->isolate());
283
11956
  Context::Scope context_scope(req_wrap->env()->context());
284
2989
  req_wrap->Done(status);
285
2989
}
286
287
288
// NOTE: Call to this function could change both `buf`'s and `count`'s
289
// values, shifting their base and decrementing their length. This is
290
// required in order to skip the data that was successfully written via
291
// uv_try_write().
292
14198
int StreamWrap::DoTryWrite(uv_buf_t** bufs, size_t* count) {
293
  int err;
294
  size_t written;
295
14198
  uv_buf_t* vbufs = *bufs;
296
14198
  size_t vcount = *count;
297
298
14198
  err = uv_try_write(stream(), vbufs, vcount);
299
14198
  if (err == UV_ENOSYS || err == UV_EAGAIN)
300
    return 0;
301
14183
  if (err < 0)
302
    return err;
303
304
  // Slice off the buffers: skip all written buffers and slice the one that
305
  // was partially written.
306
14183
  written = err;
307
42437
  for (; vcount > 0; vbufs++, vcount--) {
308
    // Slice
309
14183
    if (vbufs[0].len > written) {
310
56
      vbufs[0].base += written;
311
56
      vbufs[0].len -= written;
312
56
      written = 0;
313
56
      break;
314
315
    // Discard
316
    } else {
317
14127
      written -= vbufs[0].len;
318
    }
319
  }
320
321
14183
  *bufs = vbufs;
322
14183
  *count = vcount;
323
324
14183
  return 0;
325
}
326
327
328
14477
int StreamWrap::DoWrite(WriteWrap* w,
329
                        uv_buf_t* bufs,
330
                        size_t count,
331
                        uv_stream_t* send_handle) {
332
  int r;
333
14477
  if (send_handle == nullptr) {
334
28788
    r = uv_write(w->req(), stream(), bufs, count, AfterWrite);
335
  } else {
336
166
    r = uv_write2(w->req(), stream(), bufs, count, send_handle, AfterWrite);
337
  }
338
339
  if (!r) {
340
    size_t bytes = 0;
341
    for (size_t i = 0; i < count; i++)
342
      bytes += bufs[i].len;
343
    if (stream()->type == UV_TCP) {
344
      NODE_COUNT_NET_BYTES_SENT(bytes);
345
    } else if (stream()->type == UV_NAMED_PIPE) {
346
      NODE_COUNT_PIPE_BYTES_SENT(bytes);
347
    }
348
  }
349
350
28954
  w->Dispatched();
351
14477
  UpdateWriteQueueSize();
352
353
14477
  return r;
354
}
355
356
357
14457
void StreamWrap::AfterWrite(uv_write_t* req, int status) {
358
14457
  WriteWrap* req_wrap = WriteWrap::from_req(req);
359
14457
  CHECK_NE(req_wrap, nullptr);
360
28908
  HandleScope scope(req_wrap->env()->isolate());
361
57822
  Context::Scope context_scope(req_wrap->env()->context());
362
14457
  req_wrap->Done(status);
363
14451
}
364
365
366
12213
void StreamWrap::OnAfterWriteImpl(WriteWrap* w, void* ctx) {
367
12213
  StreamWrap* wrap = static_cast<StreamWrap*>(ctx);
368
12213
  wrap->UpdateWriteQueueSize();
369
12213
}
370
371
}  // namespace node
372
373
1729
NODE_MODULE_CONTEXT_AWARE_BUILTIN(stream_wrap, node::StreamWrap::Initialize)