1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 #ifndef SRC_STREAM_WRAP_H_
23 #define SRC_STREAM_WRAP_H_
26 #include "handle_wrap.h"
28 #include "string_bytes.h"
33 // Forward declaration
36 typedef class ReqWrap<uv_shutdown_t> ShutdownWrap;
38 class WriteWrap: public ReqWrap<uv_write_t> {
40 // TODO(trevnorris): WrapWrap inherits from ReqWrap, which I've globbed
41 // into the same provider. How should these be broken apart?
42 WriteWrap(Environment* env, v8::Local<v8::Object> obj, StreamWrap* wrap)
43 : ReqWrap<uv_write_t>(env, obj),
47 void* operator new(size_t size, char* storage) { return storage; }
49 // This is just to keep the compiler happy. It should never be called, since
50 // we don't use exceptions in node.
51 void operator delete(void* ptr, char* storage) { assert(0); }
53 inline StreamWrap* wrap() const {
58 // People should not be using the non-placement new and delete operator on a
59 // WriteWrap. Ensure this never happens.
60 void* operator new(size_t size) { assert(0); }
61 void operator delete(void* ptr) { assert(0); }
63 StreamWrap* const wrap_;
66 // Overridable callbacks' types
67 class StreamWrapCallbacks {
69 explicit StreamWrapCallbacks(StreamWrap* wrap) : wrap_(wrap) {
72 explicit StreamWrapCallbacks(StreamWrapCallbacks* old) : wrap_(old->wrap()) {
75 virtual ~StreamWrapCallbacks() {
78 virtual const char* Error();
80 virtual int TryWrite(uv_buf_t** bufs, size_t* count);
82 virtual int DoWrite(WriteWrap* w,
85 uv_stream_t* send_handle,
87 virtual void AfterWrite(WriteWrap* w);
88 virtual void DoAlloc(uv_handle_t* handle,
89 size_t suggested_size,
91 virtual void DoRead(uv_stream_t* handle,
94 uv_handle_type pending);
95 virtual int DoShutdown(ShutdownWrap* req_wrap, uv_shutdown_cb cb);
98 inline StreamWrap* wrap() const {
103 StreamWrap* const wrap_;
106 class StreamWrap : public HandleWrap {
108 void OverrideCallbacks(StreamWrapCallbacks* callbacks) {
109 StreamWrapCallbacks* old = callbacks_;
110 callbacks_ = callbacks;
111 if (old != &default_callbacks_)
115 static void GetFD(v8::Local<v8::String>,
116 const v8::PropertyCallbackInfo<v8::Value>&);
118 // JavaScript functions
119 static void ReadStart(const v8::FunctionCallbackInfo<v8::Value>& args);
120 static void ReadStop(const v8::FunctionCallbackInfo<v8::Value>& args);
121 static void Shutdown(const v8::FunctionCallbackInfo<v8::Value>& args);
123 static void Writev(const v8::FunctionCallbackInfo<v8::Value>& args);
124 static void WriteBuffer(const v8::FunctionCallbackInfo<v8::Value>& args);
125 static void WriteAsciiString(const v8::FunctionCallbackInfo<v8::Value>& args);
126 static void WriteUtf8String(const v8::FunctionCallbackInfo<v8::Value>& args);
127 static void WriteUcs2String(const v8::FunctionCallbackInfo<v8::Value>& args);
129 static void SetBlocking(const v8::FunctionCallbackInfo<v8::Value>& args);
131 inline StreamWrapCallbacks* callbacks() const {
135 inline uv_stream_t* stream() const {
139 inline bool is_named_pipe() const {
140 return stream()->type == UV_NAMED_PIPE;
143 inline bool is_named_pipe_ipc() const {
144 return is_named_pipe() &&
145 reinterpret_cast<const uv_pipe_t*>(stream())->ipc != 0;
148 inline bool is_tcp() const {
149 return stream()->type == UV_TCP;
153 static size_t WriteBuffer(v8::Handle<v8::Value> val, uv_buf_t* buf);
155 StreamWrap(Environment* env,
156 v8::Local<v8::Object> object,
158 AsyncWrap::ProviderType provider);
161 if (callbacks_ != &default_callbacks_) {
167 void StateChange() { }
168 void UpdateWriteQueueSize();
171 // Callbacks for libuv
172 static void AfterWrite(uv_write_t* req, int status);
173 static void OnAlloc(uv_handle_t* handle,
174 size_t suggested_size,
176 static void AfterShutdown(uv_shutdown_t* req, int status);
178 static void OnRead(uv_stream_t* handle,
180 const uv_buf_t* buf);
181 static void OnReadCommon(uv_stream_t* handle,
184 uv_handle_type pending);
186 template <enum encoding encoding>
187 static void WriteStringImpl(const v8::FunctionCallbackInfo<v8::Value>& args);
189 uv_stream_t* const stream_;
190 StreamWrapCallbacks default_callbacks_;
191 StreamWrapCallbacks* callbacks_; // Overridable callbacks
193 friend class StreamWrapCallbacks;
200 #endif // SRC_STREAM_WRAP_H_