Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
stream_wrap.h
Go to the documentation of this file.
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 
29 #include "env.h"
30 #include "handle_wrap.h"
31 #include "string_bytes.h"
32 #include "v8.h"
33 
34 namespace node {
35 
36 // Forward declaration
37 class StreamWrap;
38 
39 class StreamWrap : public HandleWrap, public StreamBase {
40  public:
41  static void Initialize(v8::Local<v8::Object> target,
42  v8::Local<v8::Value> unused,
43  v8::Local<v8::Context> context);
44 
45  int GetFD() override;
46  void* Cast() override;
47  bool IsAlive() override;
48  bool IsClosing() override;
49  bool IsIPCPipe() override;
50 
51  // JavaScript functions
52  int ReadStart() override;
53  int ReadStop() override;
54 
55  // Resource implementation
56  int DoShutdown(ShutdownWrap* req_wrap) override;
57  int DoTryWrite(uv_buf_t** bufs, size_t* count) override;
58  int DoWrite(WriteWrap* w,
59  uv_buf_t* bufs,
60  size_t count,
61  uv_stream_t* send_handle) override;
62 
63  inline uv_stream_t* stream() const {
64  return stream_;
65  }
66 
67  inline bool is_named_pipe() const {
68  return stream()->type == UV_NAMED_PIPE;
69  }
70 
71  inline bool is_named_pipe_ipc() const {
72  return is_named_pipe() &&
73  reinterpret_cast<const uv_pipe_t*>(stream())->ipc != 0;
74  }
75 
76  inline bool is_tcp() const {
77  return stream()->type == UV_TCP;
78  }
79 
80  protected:
81  StreamWrap(Environment* env,
82  v8::Local<v8::Object> object,
83  uv_stream_t* stream,
84  AsyncWrap::ProviderType provider);
85 
86  ~StreamWrap() {
87  }
88 
89  AsyncWrap* GetAsyncWrap() override;
90  void UpdateWriteQueueSize();
91 
92  static void AddMethods(Environment* env,
93  v8::Local<v8::FunctionTemplate> target,
94  int flags = StreamBase::kFlagNone);
95 
96  private:
97  static void SetBlocking(const v8::FunctionCallbackInfo<v8::Value>& args);
98 
99  // Callbacks for libuv
100  static void OnAlloc(uv_handle_t* handle,
101  size_t suggested_size,
102  uv_buf_t* buf);
103 
104  static void OnRead(uv_stream_t* handle,
105  ssize_t nread,
106  const uv_buf_t* buf);
107  static void AfterWrite(uv_write_t* req, int status);
108  static void AfterShutdown(uv_shutdown_t* req, int status);
109 
110  // Resource interface implementation
111  static void OnAfterWriteImpl(WriteWrap* w, void* ctx);
112  static void OnAllocImpl(size_t size, uv_buf_t* buf, void* ctx);
113  static void OnReadImpl(ssize_t nread,
114  const uv_buf_t* buf,
115  uv_handle_type pending,
116  void* ctx);
117 
118  uv_stream_t* const stream_;
119 };
120 
121 
122 } // namespace node
123 
124 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
125 
126 #endif // SRC_STREAM_WRAP_H_
unsigned char * buf
Definition: cares_wrap.cc:483
int status
Definition: cares_wrap.cc:479
void Initialize(Local< Object > target, Local< Value > unused, Local< Context > context, void *priv)
Definition: node_http2.cc:1172
uv_fs_t req
Definition: node_file.cc:374
this ctx
Definition: v8ustack.d:369