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: 2017-01-02 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
1697
void StreamWrap::Initialize(Local<Object> target,
37
                            Local<Value> unused,
38
                            Local<Context> context) {
39
1697
  Environment* env = Environment::GetCurrent(context);
40
41
  Local<FunctionTemplate> sw =
42
3394
      FunctionTemplate::New(env->isolate(), ShutdownWrap::NewShutdownWrap);
43
3394
  sw->InstanceTemplate()->SetInternalFieldCount(1);
44
1697
  sw->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"));
45
6788
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"),
46
1697
              sw->GetFunction());
47
48
  Local<FunctionTemplate> ww =
49
3394
      FunctionTemplate::New(env->isolate(), WriteWrap::NewWriteWrap);
50
3394
  ww->InstanceTemplate()->SetInternalFieldCount(1);
51
1697
  ww->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"));
52
6788
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"),
53
1697
              ww->GetFunction());
54
1697
  env->set_write_wrap_constructor_function(ww->GetFunction());
55
1697
}
56
57
58
7727
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
15454
      stream_(stream) {
70
15454
  set_after_write_cb({ OnAfterWriteImpl, this });
71
15454
  set_alloc_cb({ OnAllocImpl, this });
72
7727
  set_read_cb({ OnReadImpl, this });
73
7727
}
74
75
76
5100
void StreamWrap::AddMethods(Environment* env,
77
                            v8::Local<v8::FunctionTemplate> target,
78
                            int flags) {
79
5100
  env->SetProtoMethod(target, "setBlocking", SetBlocking);
80
5100
  StreamBase::AddMethods<StreamWrap>(env, target, flags);
81
5100
}
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
36792
bool StreamWrap::IsAlive() {
95
73584
  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
34501
AsyncWrap* StreamWrap::GetAsyncWrap() {
110
34501
  return static_cast<AsyncWrap*>(this);
111
}
112
113
114
14378
bool StreamWrap::IsIPCPipe() {
115
14378
  return is_named_pipe_ipc();
116
}
117
118
119
34444
void StreamWrap::UpdateWriteQueueSize() {
120
68888
  HandleScope scope(env()->isolate());
121
  Local<Integer> write_queue_size =
122
34444
      Integer::NewFromUnsigned(env()->isolate(), stream()->write_queue_size);
123
137776
  object()->Set(env()->write_queue_size_string(), write_queue_size);
124
34444
}
125
126
127
6799
int StreamWrap::ReadStart() {
128
6799
  return uv_read_start(stream(), OnAlloc, OnRead);
129
}
130
131
132
250
int StreamWrap::ReadStop() {
133
250
  return uv_read_stop(stream());
134
}
135
136
137
24883
void StreamWrap::OnAlloc(uv_handle_t* handle,
138
                         size_t suggested_size,
139
                         uv_buf_t* buf) {
140
24883
  StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);
141
49766
  HandleScope scope(wrap->env()->isolate());
142
99532
  Context::Scope context_scope(wrap->env()->context());
143
144
24883
  CHECK_EQ(wrap->stream(), reinterpret_cast<uv_stream_t*>(handle));
145
146
74649
  return static_cast<StreamBase*>(wrap)->OnAlloc(suggested_size, buf);
147
}
148
149
150
19360
void StreamWrap::OnAllocImpl(size_t size, uv_buf_t* buf, void* ctx) {
151
19360
  buf->base = node::Malloc(size);
152
19360
  buf->len = size;
153
19360
}
154
155
156
template <class WrapType, class UVType>
157
136
static Local<Object> AcceptHandle(Environment* env, StreamWrap* parent) {
158
272
  EscapableHandleScope scope(env->isolate());
159
136
  Local<Object> wrap_obj;
160
  UVType* handle;
161
162
136
  wrap_obj = WrapType::Instantiate(env, parent);
163

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

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

136
  if (uv_accept(parent->stream(), reinterpret_cast<uv_stream_t*>(handle)))
171
    ABORT();
172
173
  return scope.Escape(wrap_obj);
174
}
175
176
177
19601
void StreamWrap::OnReadImpl(ssize_t nread,
178
                            const uv_buf_t* buf,
179
                            uv_handle_type pending,
180
                            void* ctx) {
181
19601
  StreamWrap* wrap = static_cast<StreamWrap*>(ctx);
182
19601
  Environment* env = wrap->env();
183
37165
  HandleScope handle_scope(env->isolate());
184
56766
  Context::Scope context_scope(env->context());
185
186
19601
  Local<Object> pending_obj;
187
188
19601
  if (nread < 0)  {
189
1982
    if (buf->base != nullptr)
190
1741
      free(buf->base);
191
1982
    wrap->EmitData(nread, Local<Object>(), pending_obj);
192
3947
    return;
193
  }
194
195
17619
  if (nread == 0) {
196
17
    if (buf->base != nullptr)
197
17
      free(buf->base);
198
    return;
199
  }
200
201
17602
  CHECK_LE(static_cast<size_t>(nread), buf->len);
202
17602
  char* base = node::Realloc(buf->base, nread);
203
204
17602
  if (pending == UV_TCP) {
205
124
    pending_obj = AcceptHandle<TCPWrap, uv_tcp_t>(env, wrap);
206
17478
  } else if (pending == UV_NAMED_PIPE) {
207
1
    pending_obj = AcceptHandle<PipeWrap, uv_pipe_t>(env, wrap);
208
17477
  } else if (pending == UV_UDP) {
209
11
    pending_obj = AcceptHandle<UDPWrap, uv_udp_t>(env, wrap);
210
  } else {
211
17466
    CHECK_EQ(pending, UV_UNKNOWN_HANDLE);
212
  }
213
214
35204
  Local<Object> obj = Buffer::New(env, base, nread).ToLocalChecked();
215
17602
  wrap->EmitData(nread, obj, pending_obj);
216
}
217
218
219
25038
void StreamWrap::OnReadCommon(uv_stream_t* handle,
220
                              ssize_t nread,
221
                              const uv_buf_t* buf,
222
                              uv_handle_type pending) {
223
25038
  StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);
224
50016
  HandleScope scope(wrap->env()->isolate());
225
100092
  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
50076
  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
50016
  static_cast<StreamBase*>(wrap)->OnRead(nread, buf, pending);
240
24978
}
241
242
243
25038
void StreamWrap::OnRead(uv_stream_t* handle,
244
                        ssize_t nread,
245
                        const uv_buf_t* buf) {
246
25038
  StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);
247
25038
  uv_handle_type type = UV_UNKNOWN_HANDLE;
248
249

26106
  if (wrap->is_named_pipe_ipc() &&
250
1068
      uv_pipe_pending_count(reinterpret_cast<uv_pipe_t*>(handle)) > 0) {
251
136
    type = uv_pipe_pending_type(reinterpret_cast<uv_pipe_t*>(handle));
252
  }
253
254
25038
  OnReadCommon(handle, nread, buf, type);
255
24978
}
256
257
258
12
void StreamWrap::SetBlocking(const FunctionCallbackInfo<Value>& args) {
259
  StreamWrap* wrap;
260
12
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
261
262
12
  CHECK_GT(args.Length(), 0);
263
12
  if (!wrap->IsAlive())
264
    return args.GetReturnValue().Set(UV_EINVAL);
265
266
12
  bool enable = args[0]->IsTrue();
267
36
  args.GetReturnValue().Set(uv_stream_set_blocking(wrap->stream(), enable));
268
}
269
270
271
3004
int StreamWrap::DoShutdown(ShutdownWrap* req_wrap) {
272
  int err;
273
6008
  err = uv_shutdown(req_wrap->req(), stream(), AfterShutdown);
274
6008
  req_wrap->Dispatched();
275
3004
  return err;
276
}
277
278
279
2998
void StreamWrap::AfterShutdown(uv_shutdown_t* req, int status) {
280
2998
  ShutdownWrap* req_wrap = ShutdownWrap::from_req(req);
281
2998
  CHECK_NE(req_wrap, nullptr);
282
5996
  HandleScope scope(req_wrap->env()->isolate());
283
11992
  Context::Scope context_scope(req_wrap->env()->context());
284
2998
  req_wrap->Done(status);
285
2998
}
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
13658
int StreamWrap::DoTryWrite(uv_buf_t** bufs, size_t* count) {
293
  int err;
294
  size_t written;
295
13658
  uv_buf_t* vbufs = *bufs;
296
13658
  size_t vcount = *count;
297
298
13658
  err = uv_try_write(stream(), vbufs, vcount);
299
13658
  if (err == UV_ENOSYS || err == UV_EAGAIN)
300
    return 0;
301
13644
  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
13644
  written = err;
307
40822
  for (; vcount > 0; vbufs++, vcount--) {
308
    // Slice
309
13644
    if (vbufs[0].len > written) {
310
55
      vbufs[0].base += written;
311
55
      vbufs[0].len -= written;
312
55
      written = 0;
313
55
      break;
314
315
    // Discard
316
    } else {
317
13589
      written -= vbufs[0].len;
318
    }
319
  }
320
321
13644
  *bufs = vbufs;
322
13644
  *count = vcount;
323
324
13644
  return 0;
325
}
326
327
328
14507
int StreamWrap::DoWrite(WriteWrap* w,
329
                        uv_buf_t* bufs,
330
                        size_t count,
331
                        uv_stream_t* send_handle) {
332
  int r;
333
14507
  if (send_handle == nullptr) {
334
28850
    r = uv_write(w->req(), stream(), bufs, count, AfterWrite);
335
  } else {
336
164
    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
29014
  w->Dispatched();
351
14507
  UpdateWriteQueueSize();
352
353
14507
  return r;
354
}
355
356
357
14487
void StreamWrap::AfterWrite(uv_write_t* req, int status) {
358
14487
  WriteWrap* req_wrap = WriteWrap::from_req(req);
359
14487
  CHECK_NE(req_wrap, nullptr);
360
28968
  HandleScope scope(req_wrap->env()->isolate());
361
57942
  Context::Scope context_scope(req_wrap->env()->context());
362
14487
  req_wrap->Done(status);
363
14481
}
364
365
366
12210
void StreamWrap::OnAfterWriteImpl(WriteWrap* w, void* ctx) {
367
12210
  StreamWrap* wrap = static_cast<StreamWrap*>(ctx);
368
12210
  wrap->UpdateWriteQueueSize();
369
12210
}
370
371
}  // namespace node
372
373
1757
NODE_MODULE_CONTEXT_AWARE_BUILTIN(stream_wrap, node::StreamWrap::Initialize)