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 31 namespace node { 32 33 class Environment; 34 35 class LibuvStreamWrap : public HandleWrap, public StreamBase { 36 public: 37 static void Initialize(v8::Local<v8::Object> target, 38 v8::Local<v8::Value> unused, 39 v8::Local<v8::Context> context, 40 void* priv); 41 42 int GetFD() override; 43 bool IsAlive() override; 44 bool IsClosing() override; 45 bool IsIPCPipe() override; 46 47 // JavaScript functions 48 int ReadStart() override; 49 int ReadStop() override; 50 51 // Resource implementation 52 int DoShutdown(ShutdownWrap* req_wrap) override; 53 int DoTryWrite(uv_buf_t** bufs, size_t* count) override; 54 int DoWrite(WriteWrap* w, 55 uv_buf_t* bufs, 56 size_t count, 57 uv_stream_t* send_handle) override; 58 stream()59 inline uv_stream_t* stream() const { 60 return stream_; 61 } 62 is_named_pipe()63 inline bool is_named_pipe() const { 64 return stream()->type == UV_NAMED_PIPE; 65 } 66 is_named_pipe_ipc()67 inline bool is_named_pipe_ipc() const { 68 return is_named_pipe() && 69 reinterpret_cast<const uv_pipe_t*>(stream())->ipc != 0; 70 } 71 is_tcp()72 inline bool is_tcp() const { 73 return stream()->type == UV_TCP; 74 } 75 76 ShutdownWrap* CreateShutdownWrap(v8::Local<v8::Object> object) override; 77 WriteWrap* CreateWriteWrap(v8::Local<v8::Object> object) override; 78 79 static LibuvStreamWrap* From(Environment* env, v8::Local<v8::Object> object); 80 81 protected: 82 LibuvStreamWrap(Environment* env, 83 v8::Local<v8::Object> object, 84 uv_stream_t* stream, 85 AsyncWrap::ProviderType provider); 86 87 AsyncWrap* GetAsyncWrap() override; 88 89 static v8::Local<v8::FunctionTemplate> GetConstructorTemplate( 90 Environment* env); 91 92 protected: set_fd(int fd)93 inline void set_fd(int fd) { 94 #ifdef _WIN32 95 fd_ = fd; 96 #endif 97 } 98 99 100 private: 101 static void GetWriteQueueSize( 102 const v8::FunctionCallbackInfo<v8::Value>& info); 103 static void SetBlocking(const v8::FunctionCallbackInfo<v8::Value>& args); 104 105 // Callbacks for libuv 106 void OnUvAlloc(size_t suggested_size, uv_buf_t* buf); 107 void OnUvRead(ssize_t nread, const uv_buf_t* buf); 108 109 static void AfterUvWrite(uv_write_t* req, int status); 110 static void AfterUvShutdown(uv_shutdown_t* req, int status); 111 112 uv_stream_t* const stream_; 113 114 #ifdef _WIN32 115 // We don't always have an FD that we could look up on the stream_ 116 // object itself on Windows. However, for some cases, we open handles 117 // using FDs; In that case, we can store and provide the value. 118 // This became necessary because it allows to detect situations 119 // where multiple handles refer to the same stdio FDs (in particular, 120 // a possible IPC channel and a regular process.std??? stream). 121 int fd_ = -1; 122 #endif 123 }; 124 125 126 } // namespace node 127 128 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 129 130 #endif // SRC_STREAM_WRAP_H_ 131