src: deduplicate CHECK_EQ/CHECK_NE macros
[platform/upstream/nodejs.git] / src / stream_wrap.h
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 #include "env.h"
26 #include "handle_wrap.h"
27 #include "req_wrap.h"
28 #include "string_bytes.h"
29 #include "v8.h"
30
31 namespace node {
32
33 // Forward declaration
34 class StreamWrap;
35
36 typedef class ReqWrap<uv_shutdown_t> ShutdownWrap;
37
38 class WriteWrap: public ReqWrap<uv_write_t> {
39  public:
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),
44         wrap_(wrap) {
45   }
46
47   void* operator new(size_t size, char* storage) { return storage; }
48
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); }
52
53   inline StreamWrap* wrap() const {
54     return wrap_;
55   }
56
57  private:
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); }
62
63   StreamWrap* const wrap_;
64 };
65
66 // Overridable callbacks' types
67 class StreamWrapCallbacks {
68  public:
69   explicit StreamWrapCallbacks(StreamWrap* wrap) : wrap_(wrap) {
70   }
71
72   explicit StreamWrapCallbacks(StreamWrapCallbacks* old) : wrap_(old->wrap()) {
73   }
74
75   virtual ~StreamWrapCallbacks() {
76   }
77
78   virtual const char* Error();
79
80   virtual int TryWrite(uv_buf_t** bufs, size_t* count);
81
82   virtual int DoWrite(WriteWrap* w,
83                       uv_buf_t* bufs,
84                       size_t count,
85                       uv_stream_t* send_handle,
86                       uv_write_cb cb);
87   virtual void AfterWrite(WriteWrap* w);
88   virtual void DoAlloc(uv_handle_t* handle,
89                        size_t suggested_size,
90                        uv_buf_t* buf);
91   virtual void DoRead(uv_stream_t* handle,
92                       ssize_t nread,
93                       const uv_buf_t* buf,
94                       uv_handle_type pending);
95   virtual int DoShutdown(ShutdownWrap* req_wrap, uv_shutdown_cb cb);
96
97  protected:
98   inline StreamWrap* wrap() const {
99     return wrap_;
100   }
101
102  private:
103   StreamWrap* const wrap_;
104 };
105
106 class StreamWrap : public HandleWrap {
107  public:
108   void OverrideCallbacks(StreamWrapCallbacks* callbacks) {
109     StreamWrapCallbacks* old = callbacks_;
110     callbacks_ = callbacks;
111     if (old != &default_callbacks_)
112       delete old;
113   }
114
115   static void GetFD(v8::Local<v8::String>,
116                     const v8::PropertyCallbackInfo<v8::Value>&);
117
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);
122
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);
128
129   static void SetBlocking(const v8::FunctionCallbackInfo<v8::Value>& args);
130
131   inline StreamWrapCallbacks* callbacks() const {
132     return callbacks_;
133   }
134
135   inline uv_stream_t* stream() const {
136     return stream_;
137   }
138
139   inline bool is_named_pipe() const {
140     return stream()->type == UV_NAMED_PIPE;
141   }
142
143   inline bool is_named_pipe_ipc() const {
144     return is_named_pipe() &&
145            reinterpret_cast<const uv_pipe_t*>(stream())->ipc != 0;
146   }
147
148   inline bool is_tcp() const {
149     return stream()->type == UV_TCP;
150   }
151
152  protected:
153   static size_t WriteBuffer(v8::Handle<v8::Value> val, uv_buf_t* buf);
154
155   StreamWrap(Environment* env,
156              v8::Local<v8::Object> object,
157              uv_stream_t* stream,
158              AsyncWrap::ProviderType provider);
159
160   ~StreamWrap() {
161     if (callbacks_ != &default_callbacks_) {
162       delete callbacks_;
163       callbacks_ = NULL;
164     }
165   }
166
167   void StateChange() { }
168   void UpdateWriteQueueSize();
169
170  private:
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,
175                       uv_buf_t* buf);
176   static void AfterShutdown(uv_shutdown_t* req, int status);
177
178   static void OnRead(uv_stream_t* handle,
179                      ssize_t nread,
180                      const uv_buf_t* buf);
181   static void OnReadCommon(uv_stream_t* handle,
182                            ssize_t nread,
183                            const uv_buf_t* buf,
184                            uv_handle_type pending);
185
186   template <enum encoding encoding>
187   static void WriteStringImpl(const v8::FunctionCallbackInfo<v8::Value>& args);
188
189   uv_stream_t* const stream_;
190   StreamWrapCallbacks default_callbacks_;
191   StreamWrapCallbacks* callbacks_;  // Overridable callbacks
192
193   friend class StreamWrapCallbacks;
194 };
195
196
197 }  // namespace node
198
199
200 #endif  // SRC_STREAM_WRAP_H_