xref: /third_party/node/src/stream_wrap.h (revision 1cb0ef41)
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22#ifndef SRC_STREAM_WRAP_H_
23#define SRC_STREAM_WRAP_H_
24
25#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27#include "stream_base.h"
28#include "handle_wrap.h"
29#include "v8.h"
30
31namespace node {
32
33class Environment;
34class ExternalReferenceRegistry;
35
36class LibuvStreamWrap : public HandleWrap, public StreamBase {
37 public:
38  static void Initialize(v8::Local<v8::Object> target,
39                         v8::Local<v8::Value> unused,
40                         v8::Local<v8::Context> context,
41                         void* priv);
42  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
43  int GetFD() override;
44  bool IsAlive() override;
45  bool IsClosing() override;
46  bool IsIPCPipe() override;
47
48  // JavaScript functions
49  int ReadStart() override;
50  int ReadStop() override;
51
52  // Resource implementation
53  int DoShutdown(ShutdownWrap* req_wrap) override;
54  int DoTryWrite(uv_buf_t** bufs, size_t* count) override;
55  int DoWrite(WriteWrap* w,
56              uv_buf_t* bufs,
57              size_t count,
58              uv_stream_t* send_handle) override;
59
60  inline uv_stream_t* stream() const {
61    return stream_;
62  }
63
64  inline bool is_named_pipe() const {
65    return stream()->type == UV_NAMED_PIPE;
66  }
67
68  inline bool is_named_pipe_ipc() const {
69    return is_named_pipe() &&
70           reinterpret_cast<const uv_pipe_t*>(stream())->ipc != 0;
71  }
72
73  inline bool is_tcp() const {
74    return stream()->type == UV_TCP;
75  }
76
77  ShutdownWrap* CreateShutdownWrap(v8::Local<v8::Object> object) override;
78  WriteWrap* CreateWriteWrap(v8::Local<v8::Object> object) override;
79
80  static LibuvStreamWrap* From(Environment* env, v8::Local<v8::Object> object);
81
82 protected:
83  LibuvStreamWrap(Environment* env,
84                  v8::Local<v8::Object> object,
85                  uv_stream_t* stream,
86                  AsyncWrap::ProviderType provider);
87
88  AsyncWrap* GetAsyncWrap() override;
89
90  static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
91      Environment* env);
92
93 protected:
94  inline void set_fd(int fd) {
95#ifdef _WIN32
96    fd_ = fd;
97#endif
98  }
99
100
101 private:
102  static void GetWriteQueueSize(
103      const v8::FunctionCallbackInfo<v8::Value>& info);
104  static void SetBlocking(const v8::FunctionCallbackInfo<v8::Value>& args);
105
106  // Callbacks for libuv
107  void OnUvAlloc(size_t suggested_size, uv_buf_t* buf);
108  v8::Maybe<void> OnUvRead(ssize_t nread, const uv_buf_t* buf);
109
110  static void AfterUvWrite(uv_write_t* req, int status);
111  static void AfterUvShutdown(uv_shutdown_t* req, int status);
112
113  uv_stream_t* const stream_;
114
115#ifdef _WIN32
116  // We don't always have an FD that we could look up on the stream_
117  // object itself on Windows. However, for some cases, we open handles
118  // using FDs; In that case, we can store and provide the value.
119  // This became necessary because it allows to detect situations
120  // where multiple handles refer to the same stdio FDs (in particular,
121  // a possible IPC channel and a regular process.std??? stream).
122  int fd_ = -1;
123#endif
124};
125
126
127}  // namespace node
128
129#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
130
131#endif  // SRC_STREAM_WRAP_H_
132