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-11-30 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
1647
void StreamWrap::Initialize(Local<Object> target,
37
                            Local<Value> unused,
38
                            Local<Context> context) {
39
1647
  Environment* env = Environment::GetCurrent(context);
40
41
  Local<FunctionTemplate> sw =
42
3294
      FunctionTemplate::New(env->isolate(), ShutdownWrap::NewShutdownWrap);
43
3294
  sw->InstanceTemplate()->SetInternalFieldCount(1);
44
1647
  sw->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"));
45
6588
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap"),
46
1647
              sw->GetFunction());
47
48
  Local<FunctionTemplate> ww =
49
3294
      FunctionTemplate::New(env->isolate(), WriteWrap::NewWriteWrap);
50
3294
  ww->InstanceTemplate()->SetInternalFieldCount(1);
51
1647
  ww->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"));
52
6588
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap"),
53
1647
              ww->GetFunction());
54
1647
  env->set_write_wrap_constructor_function(ww->GetFunction());
55
1647
}
56
57
58
7657
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
15314
      stream_(stream) {
70
15314
  set_after_write_cb({ OnAfterWriteImpl, this });
71
15314
  set_alloc_cb({ OnAllocImpl, this });
72
7657
  set_read_cb({ OnReadImpl, this });
73
7657
}
74
75
76
4950
void StreamWrap::AddMethods(Environment* env,
77
                            v8::Local<v8::FunctionTemplate> target,
78
                            int flags) {
79
4950
  env->SetProtoMethod(target, "setBlocking", SetBlocking);
80
4950
  StreamBase::AddMethods<StreamWrap>(env, target, flags);
81
4950
}
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
36607
bool StreamWrap::IsAlive() {
95
73214
  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
34371
AsyncWrap* StreamWrap::GetAsyncWrap() {
110
34371
  return static_cast<AsyncWrap*>(this);
111
}
112
113
114
14332
bool StreamWrap::IsIPCPipe() {
115
14332
  return is_named_pipe_ipc();
116
}
117
118
119
34281
void StreamWrap::UpdateWriteQueueSize() {
120
68562
  HandleScope scope(env()->isolate());
121
  Local<Integer> write_queue_size =
122
34281
      Integer::NewFromUnsigned(env()->isolate(), stream()->write_queue_size);
123
137124
  object()->Set(env()->write_queue_size_string(), write_queue_size);
124
34281
}
125
126
127
6720
int StreamWrap::ReadStart() {
128
6720
  return uv_read_start(stream(), OnAlloc, OnRead);
129
}
130
131
132
228
int StreamWrap::ReadStop() {
133
228
  return uv_read_stop(stream());
134
}
135
136
137
24646
void StreamWrap::OnAlloc(uv_handle_t* handle,
138
                         size_t suggested_size,
139
                         uv_buf_t* buf) {
140
24646
  StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);
141
49292
  HandleScope scope(wrap->env()->isolate());
142
98584
  Context::Scope context_scope(wrap->env()->context());
143
144
24646
  CHECK_EQ(wrap->stream(), reinterpret_cast<uv_stream_t*>(handle));
145
146
73938
  return static_cast<StreamBase*>(wrap)->OnAlloc(suggested_size, buf);
147
}
148
149
150
19239
void StreamWrap::OnAllocImpl(size_t size, uv_buf_t* buf, void* ctx) {
151
19239
  buf->base = node::Malloc(size);
152
19239
  buf->len = size;
153
19239
}
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
19471
void StreamWrap::OnReadImpl(ssize_t nread,
178
                            const uv_buf_t* buf,
179
                            uv_handle_type pending,
180
                            void* ctx) {
181
19471
  StreamWrap* wrap = static_cast<StreamWrap*>(ctx);
182
19471
  Environment* env = wrap->env();
183
36933
  HandleScope handle_scope(env->isolate());
184
56404
  Context::Scope context_scope(env->context());
185
186
19471
  Local<Object> pending_obj;
187
188
19471
  if (nread < 0)  {
189
1966
    if (buf->base != nullptr)
190
1734
      free(buf->base);
191
1966
    wrap->EmitData(nread, Local<Object>(), pending_obj);
192
3903
    return;
193
  }
194
195
17505
  if (nread == 0) {
196
5
    if (buf->base != nullptr)
197
5
      free(buf->base);
198
    return;
199
  }
200
201
17500
  CHECK_LE(static_cast<size_t>(nread), buf->len);
202
17500
  char* base = node::Realloc(buf->base, nread);
203
204
17500
  if (pending == UV_TCP) {
205
124
    pending_obj = AcceptHandle<TCPWrap, uv_tcp_t>(env, wrap);
206
17376
  } else if (pending == UV_NAMED_PIPE) {
207
1
    pending_obj = AcceptHandle<PipeWrap, uv_pipe_t>(env, wrap);
208
17375
  } else if (pending == UV_UDP) {
209
11
    pending_obj = AcceptHandle<UDPWrap, uv_udp_t>(env, wrap);
210
  } else {
211
17364
    CHECK_EQ(pending, UV_UNKNOWN_HANDLE);
212
  }
213
214
35000
  Local<Object> obj = Buffer::New(env, base, nread).ToLocalChecked();
215
17500
  wrap->EmitData(nread, obj, pending_obj);
216
}
217
218
219
24793
void StreamWrap::OnReadCommon(uv_stream_t* handle,
220
                              ssize_t nread,
221
                              const uv_buf_t* buf,
222
                              uv_handle_type pending) {
223
24793
  StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);
224
49526
  HandleScope scope(wrap->env()->isolate());
225
99112
  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
49586
  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
49526
  static_cast<StreamBase*>(wrap)->OnRead(nread, buf, pending);
240
24733
}
241
242
243
24793
void StreamWrap::OnRead(uv_stream_t* handle,
244
                        ssize_t nread,
245
                        const uv_buf_t* buf) {
246
24793
  StreamWrap* wrap = static_cast<StreamWrap*>(handle->data);
247
24793
  uv_handle_type type = UV_UNKNOWN_HANDLE;
248
249

25851
  if (wrap->is_named_pipe_ipc() &&
250
1058
      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
24793
  OnReadCommon(handle, nread, buf, type);
255
24733
}
256
257
258
8
void StreamWrap::SetBlocking(const FunctionCallbackInfo<Value>& args) {
259
  StreamWrap* wrap;
260
8
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
261
262
8
  CHECK_GT(args.Length(), 0);
263
8
  if (!wrap->IsAlive())
264
    return args.GetReturnValue().Set(UV_EINVAL);
265
266
8
  bool enable = args[0]->IsTrue();
267
24
  args.GetReturnValue().Set(uv_stream_set_blocking(wrap->stream(), enable));
268
}
269
270
271
2975
int StreamWrap::DoShutdown(ShutdownWrap* req_wrap) {
272
  int err;
273
5950
  err = uv_shutdown(req_wrap->req(), stream(), AfterShutdown);
274
5950
  req_wrap->Dispatched();
275
2975
  return err;
276
}
277
278
279
2969
void StreamWrap::AfterShutdown(uv_shutdown_t* req, int status) {
280
2969
  ShutdownWrap* req_wrap = ShutdownWrap::from_req(req);
281
2969
  CHECK_NE(req_wrap, nullptr);
282
5938
  HandleScope scope(req_wrap->env()->isolate());
283
11876
  Context::Scope context_scope(req_wrap->env()->context());
284
2969
  req_wrap->Done(status);
285
2969
}
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
13612
int StreamWrap::DoTryWrite(uv_buf_t** bufs, size_t* count) {
293
  int err;
294
  size_t written;
295
13612
  uv_buf_t* vbufs = *bufs;
296
13612
  size_t vcount = *count;
297
298
13612
  err = uv_try_write(stream(), vbufs, vcount);
299
13612
  if (err == UV_ENOSYS || err == UV_EAGAIN)
300
    return 0;
301
13602
  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
13602
  written = err;
307
40690
  for (; vcount > 0; vbufs++, vcount--) {
308
    // Slice
309
13602
    if (vbufs[0].len > written) {
310
58
      vbufs[0].base += written;
311
58
      vbufs[0].len -= written;
312
58
      written = 0;
313
58
      break;
314
315
    // Discard
316
    } else {
317
13544
      written -= vbufs[0].len;
318
    }
319
  }
320
321
13602
  *bufs = vbufs;
322
13602
  *count = vcount;
323
324
13602
  return 0;
325
}
326
327
328
14418
int StreamWrap::DoWrite(WriteWrap* w,
329
                        uv_buf_t* bufs,
330
                        size_t count,
331
                        uv_stream_t* send_handle) {
332
  int r;
333
14418
  if (send_handle == nullptr) {
334
28672
    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
28836
  w->Dispatched();
351
14418
  UpdateWriteQueueSize();
352
353
14418
  return r;
354
}
355
356
357
14398
void StreamWrap::AfterWrite(uv_write_t* req, int status) {
358
14398
  WriteWrap* req_wrap = WriteWrap::from_req(req);
359
14398
  CHECK_NE(req_wrap, nullptr);
360
28790
  HandleScope scope(req_wrap->env()->isolate());
361
57586
  Context::Scope context_scope(req_wrap->env()->context());
362
14398
  req_wrap->Done(status);
363
14392
}
364
365
366
12206
void StreamWrap::OnAfterWriteImpl(WriteWrap* w, void* ctx) {
367
12206
  StreamWrap* wrap = static_cast<StreamWrap*>(ctx);
368
12206
  wrap->UpdateWriteQueueSize();
369
12206
}
370
371
}  // namespace node
372
373
1705
NODE_MODULE_CONTEXT_AWARE_BUILTIN(stream_wrap, node::StreamWrap::Initialize)