From 54a9ec4ef09f51c0903af515aa2404922d9f73f0 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 7 Aug 2013 17:21:25 +0200 Subject: [PATCH] stream_wrap: add handle type checkers Add is_named_pipe(), is_named_pipe_ipc() and is_tcp() and update the code base to use those rather than `stream->type == UV_FOO` and `reinterpret_cast(handle)->ipc` style checks. --- src/stream_wrap.cc | 13 ++++--------- src/stream_wrap.h | 13 +++++++++++++ src/tls_wrap.cc | 4 ++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index 10e70bfaa6..ea8aaecacc 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -107,10 +107,8 @@ void StreamWrap::ReadStart(const FunctionCallbackInfo& args) { UNWRAP(StreamWrap) - bool ipc_pipe = wrap->stream()->type == UV_NAMED_PIPE && - reinterpret_cast(wrap->stream())->ipc; int err; - if (ipc_pipe) { + if (wrap->is_named_pipe_ipc()) { err = uv_read2_start(wrap->stream(), OnAlloc, OnRead2); } else { err = uv_read_start(wrap->stream(), OnAlloc, OnRead); @@ -172,9 +170,9 @@ void StreamWrap::OnReadCommon(uv_stream_t* handle, assert(wrap->persistent().IsEmpty() == false); if (nread > 0) { - if (wrap->stream()->type == UV_TCP) { + if (wrap->is_tcp()) { NODE_COUNT_NET_BYTES_RECV(nread); - } else if (wrap->stream()->type == UV_NAMED_PIPE) { + } else if (wrap->is_named_pipe()) { NODE_COUNT_PIPE_BYTES_RECV(nread); } } @@ -285,10 +283,7 @@ void StreamWrap::WriteStringImpl(const FunctionCallbackInfo& args) { buf.base = data; buf.len = data_size; - bool ipc_pipe = wrap->stream()->type == UV_NAMED_PIPE && - reinterpret_cast(wrap->stream())->ipc; - - if (!ipc_pipe) { + if (!wrap->is_named_pipe_ipc()) { err = wrap->callbacks()->DoWrite(req_wrap, &buf, 1, diff --git a/src/stream_wrap.h b/src/stream_wrap.h index 25aff8b8d3..7f8f74af9f 100644 --- a/src/stream_wrap.h +++ b/src/stream_wrap.h @@ -130,6 +130,19 @@ class StreamWrap : public HandleWrap { return stream_; } + inline bool is_named_pipe() const { + return stream()->type == UV_NAMED_PIPE; + } + + inline bool is_named_pipe_ipc() const { + return is_named_pipe() && + reinterpret_cast(stream())->ipc != 0; + } + + inline bool is_tcp() const { + return stream()->type == UV_TCP; + } + protected: static size_t WriteBuffer(v8::Handle val, uv_buf_t* buf); diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index 66dfd6e4a0..3cc169ec5c 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -398,9 +398,9 @@ void TLSCallbacks::EncOut() { // Ignore errors, this should be already handled in js if (!r) { - if (wrap()->stream()->type == UV_TCP) { + if (wrap()->is_tcp()) { NODE_COUNT_NET_BYTES_SENT(write_size_); - } else if (wrap()->stream()->type == UV_NAMED_PIPE) { + } else if (wrap()->is_named_pipe()) { NODE_COUNT_PIPE_BYTES_SENT(write_size_); } }