|
3 | 3 | #include <req_wrap.h> |
4 | 4 | #include <handle_wrap.h> |
5 | 5 | #include <stream_wrap.h> |
| 6 | +#include <pipe_wrap.h> |
6 | 7 |
|
7 | 8 | #define UNWRAP \ |
8 | 9 | assert(!args.Holder().IsEmpty()); \ |
@@ -37,176 +38,187 @@ Persistent<Function> pipeConstructor; |
37 | 38 | typedef class ReqWrap<uv_connect_t> ConnectWrap; |
38 | 39 |
|
39 | 40 |
|
40 | | -class PipeWrap : StreamWrap { |
41 | | - public: |
| 41 | +uv_pipe_t* PipeWrap::UVHandle() { |
| 42 | + return &handle_; |
| 43 | +} |
42 | 44 |
|
43 | | - static void Initialize(Handle<Object> target) { |
44 | | - StreamWrap::Initialize(target); |
45 | 45 |
|
46 | | - HandleScope scope; |
| 46 | +PipeWrap* PipeWrap::Unwrap(Local<Object> obj) { |
| 47 | + assert(!obj.IsEmpty()); |
| 48 | + assert(obj->InternalFieldCount() > 0); |
| 49 | + return static_cast<PipeWrap*>(obj->GetPointerFromInternalField(0)); |
| 50 | +} |
47 | 51 |
|
48 | | - Local<FunctionTemplate> t = FunctionTemplate::New(New); |
49 | | - t->SetClassName(String::NewSymbol("Pipe")); |
50 | 52 |
|
51 | | - t->InstanceTemplate()->SetInternalFieldCount(1); |
| 53 | +void PipeWrap::Initialize(Handle<Object> target) { |
| 54 | + StreamWrap::Initialize(target); |
52 | 55 |
|
53 | | - NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close); |
| 56 | + HandleScope scope; |
54 | 57 |
|
55 | | - NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart); |
56 | | - NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop); |
57 | | - NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write); |
58 | | - NODE_SET_PROTOTYPE_METHOD(t, "shutdown", StreamWrap::Shutdown); |
| 58 | + Local<FunctionTemplate> t = FunctionTemplate::New(New); |
| 59 | + t->SetClassName(String::NewSymbol("Pipe")); |
59 | 60 |
|
60 | | - NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind); |
61 | | - NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen); |
62 | | - NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect); |
| 61 | + t->InstanceTemplate()->SetInternalFieldCount(1); |
63 | 62 |
|
64 | | - pipeConstructor = Persistent<Function>::New(t->GetFunction()); |
| 63 | + NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close); |
65 | 64 |
|
66 | | - target->Set(String::NewSymbol("Pipe"), pipeConstructor); |
67 | | - } |
| 65 | + NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart); |
| 66 | + NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop); |
| 67 | + NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write); |
| 68 | + NODE_SET_PROTOTYPE_METHOD(t, "shutdown", StreamWrap::Shutdown); |
68 | 69 |
|
69 | | - private: |
70 | | - static Handle<Value> New(const Arguments& args) { |
71 | | - // This constructor should not be exposed to public javascript. |
72 | | - // Therefore we assert that we are not trying to call this as a |
73 | | - // normal function. |
74 | | - assert(args.IsConstructCall()); |
| 70 | + NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind); |
| 71 | + NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen); |
| 72 | + NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect); |
75 | 73 |
|
76 | | - HandleScope scope; |
77 | | - PipeWrap* wrap = new PipeWrap(args.This()); |
78 | | - assert(wrap); |
| 74 | + pipeConstructor = Persistent<Function>::New(t->GetFunction()); |
79 | 75 |
|
80 | | - return scope.Close(args.This()); |
81 | | - } |
| 76 | + target->Set(String::NewSymbol("Pipe"), pipeConstructor); |
| 77 | +} |
82 | 78 |
|
83 | | - PipeWrap(Handle<Object> object) : StreamWrap(object, |
84 | | - (uv_stream_t*) &handle_) { |
85 | | - int r = uv_pipe_init(&handle_); |
86 | | - assert(r == 0); // How do we proxy this error up to javascript? |
87 | | - // Suggestion: uv_pipe_init() returns void. |
88 | | - handle_.data = reinterpret_cast<void*>(this); |
89 | | - UpdateWriteQueueSize(); |
90 | | - } |
91 | 79 |
|
92 | | - static Handle<Value> Bind(const Arguments& args) { |
93 | | - HandleScope scope; |
| 80 | +Handle<Value> PipeWrap::New(const Arguments& args) { |
| 81 | + // This constructor should not be exposed to public javascript. |
| 82 | + // Therefore we assert that we are not trying to call this as a |
| 83 | + // normal function. |
| 84 | + assert(args.IsConstructCall()); |
94 | 85 |
|
95 | | - UNWRAP |
| 86 | + HandleScope scope; |
| 87 | + PipeWrap* wrap = new PipeWrap(args.This()); |
| 88 | + assert(wrap); |
96 | 89 |
|
97 | | - String::AsciiValue name(args[0]->ToString()); |
| 90 | + return scope.Close(args.This()); |
| 91 | +} |
98 | 92 |
|
99 | | - int r = uv_pipe_bind(&wrap->handle_, *name); |
100 | 93 |
|
101 | | - // Error starting the pipe. |
102 | | - if (r) SetErrno(uv_last_error().code); |
| 94 | +PipeWrap::PipeWrap(Handle<Object> object) : StreamWrap(object, |
| 95 | + (uv_stream_t*) &handle_) { |
| 96 | + int r = uv_pipe_init(&handle_); |
| 97 | + assert(r == 0); // How do we proxy this error up to javascript? |
| 98 | + // Suggestion: uv_pipe_init() returns void. |
| 99 | + handle_.data = reinterpret_cast<void*>(this); |
| 100 | + UpdateWriteQueueSize(); |
| 101 | +} |
103 | 102 |
|
104 | | - return scope.Close(Integer::New(r)); |
105 | | - } |
106 | 103 |
|
107 | | - static Handle<Value> Listen(const Arguments& args) { |
108 | | - HandleScope scope; |
| 104 | +Handle<Value> PipeWrap::Bind(const Arguments& args) { |
| 105 | + HandleScope scope; |
109 | 106 |
|
110 | | - UNWRAP |
| 107 | + UNWRAP |
111 | 108 |
|
112 | | - int backlog = args[0]->Int32Value(); |
| 109 | + String::AsciiValue name(args[0]->ToString()); |
113 | 110 |
|
114 | | - int r = uv_listen((uv_stream_t*)&wrap->handle_, backlog, OnConnection); |
| 111 | + int r = uv_pipe_bind(&wrap->handle_, *name); |
115 | 112 |
|
116 | | - // Error starting the pipe. |
117 | | - if (r) SetErrno(uv_last_error().code); |
| 113 | + // Error starting the pipe. |
| 114 | + if (r) SetErrno(uv_last_error().code); |
118 | 115 |
|
119 | | - return scope.Close(Integer::New(r)); |
120 | | - } |
| 116 | + return scope.Close(Integer::New(r)); |
| 117 | +} |
121 | 118 |
|
122 | | - // TODO maybe share with TCPWrap? |
123 | | - static void OnConnection(uv_stream_t* handle, int status) { |
124 | | - HandleScope scope; |
125 | 119 |
|
126 | | - PipeWrap* wrap = static_cast<PipeWrap*>(handle->data); |
127 | | - assert(&wrap->handle_ == (uv_pipe_t*)handle); |
| 120 | +Handle<Value> PipeWrap::Listen(const Arguments& args) { |
| 121 | + HandleScope scope; |
128 | 122 |
|
129 | | - // We should not be getting this callback if someone as already called |
130 | | - // uv_close() on the handle. |
131 | | - assert(wrap->object_.IsEmpty() == false); |
| 123 | + UNWRAP |
132 | 124 |
|
133 | | - if (status != 0) { |
134 | | - // TODO Handle server error (set errno and call onconnection with NULL) |
135 | | - assert(0); |
136 | | - return; |
137 | | - } |
| 125 | + int backlog = args[0]->Int32Value(); |
138 | 126 |
|
139 | | - // Instanciate the client javascript object and handle. |
140 | | - Local<Object> client_obj = pipeConstructor->NewInstance(); |
| 127 | + int r = uv_listen((uv_stream_t*)&wrap->handle_, backlog, OnConnection); |
141 | 128 |
|
142 | | - // Unwrap the client javascript object. |
143 | | - assert(client_obj->InternalFieldCount() > 0); |
144 | | - PipeWrap* client_wrap = |
145 | | - static_cast<PipeWrap*>(client_obj->GetPointerFromInternalField(0)); |
| 129 | + // Error starting the pipe. |
| 130 | + if (r) SetErrno(uv_last_error().code); |
146 | 131 |
|
147 | | - int r = uv_accept(handle, (uv_stream_t*)&client_wrap->handle_); |
| 132 | + return scope.Close(Integer::New(r)); |
| 133 | +} |
148 | 134 |
|
149 | | - // uv_accept should always work. |
150 | | - assert(r == 0); |
151 | 135 |
|
152 | | - // Successful accept. Call the onconnection callback in JavaScript land. |
153 | | - Local<Value> argv[1] = { client_obj }; |
154 | | - MakeCallback(wrap->object_, "onconnection", 1, argv); |
| 136 | +// TODO maybe share with TCPWrap? |
| 137 | +void PipeWrap::OnConnection(uv_stream_t* handle, int status) { |
| 138 | + HandleScope scope; |
| 139 | + |
| 140 | + PipeWrap* wrap = static_cast<PipeWrap*>(handle->data); |
| 141 | + assert(&wrap->handle_ == (uv_pipe_t*)handle); |
| 142 | + |
| 143 | + // We should not be getting this callback if someone as already called |
| 144 | + // uv_close() on the handle. |
| 145 | + assert(wrap->object_.IsEmpty() == false); |
| 146 | + |
| 147 | + if (status != 0) { |
| 148 | + // TODO Handle server error (set errno and call onconnection with NULL) |
| 149 | + assert(0); |
| 150 | + return; |
155 | 151 | } |
156 | 152 |
|
157 | | - // TODO Maybe share this with TCPWrap? |
158 | | - static void AfterConnect(uv_connect_t* req, int status) { |
159 | | - ConnectWrap* req_wrap = (ConnectWrap*) req->data; |
160 | | - PipeWrap* wrap = (PipeWrap*) req->handle->data; |
| 153 | + // Instanciate the client javascript object and handle. |
| 154 | + Local<Object> client_obj = pipeConstructor->NewInstance(); |
161 | 155 |
|
162 | | - HandleScope scope; |
| 156 | + // Unwrap the client javascript object. |
| 157 | + assert(client_obj->InternalFieldCount() > 0); |
| 158 | + PipeWrap* client_wrap = |
| 159 | + static_cast<PipeWrap*>(client_obj->GetPointerFromInternalField(0)); |
163 | 160 |
|
164 | | - // The wrap and request objects should still be there. |
165 | | - assert(req_wrap->object_.IsEmpty() == false); |
166 | | - assert(wrap->object_.IsEmpty() == false); |
| 161 | + int r = uv_accept(handle, (uv_stream_t*)&client_wrap->handle_); |
167 | 162 |
|
168 | | - if (status) { |
169 | | - SetErrno(uv_last_error().code); |
170 | | - } |
| 163 | + // uv_accept should always work. |
| 164 | + assert(r == 0); |
171 | 165 |
|
172 | | - Local<Value> argv[3] = { |
173 | | - Integer::New(status), |
174 | | - Local<Value>::New(wrap->object_), |
175 | | - Local<Value>::New(req_wrap->object_) |
176 | | - }; |
| 166 | + // Successful accept. Call the onconnection callback in JavaScript land. |
| 167 | + Local<Value> argv[1] = { client_obj }; |
| 168 | + MakeCallback(wrap->object_, "onconnection", 1, argv); |
| 169 | +} |
177 | 170 |
|
178 | | - MakeCallback(req_wrap->object_, "oncomplete", 3, argv); |
| 171 | +// TODO Maybe share this with TCPWrap? |
| 172 | +void PipeWrap::AfterConnect(uv_connect_t* req, int status) { |
| 173 | + ConnectWrap* req_wrap = (ConnectWrap*) req->data; |
| 174 | + PipeWrap* wrap = (PipeWrap*) req->handle->data; |
179 | 175 |
|
180 | | - delete req_wrap; |
| 176 | + HandleScope scope; |
| 177 | + |
| 178 | + // The wrap and request objects should still be there. |
| 179 | + assert(req_wrap->object_.IsEmpty() == false); |
| 180 | + assert(wrap->object_.IsEmpty() == false); |
| 181 | + |
| 182 | + if (status) { |
| 183 | + SetErrno(uv_last_error().code); |
181 | 184 | } |
182 | 185 |
|
183 | | - static Handle<Value> Connect(const Arguments& args) { |
184 | | - HandleScope scope; |
| 186 | + Local<Value> argv[3] = { |
| 187 | + Integer::New(status), |
| 188 | + Local<Value>::New(wrap->object_), |
| 189 | + Local<Value>::New(req_wrap->object_) |
| 190 | + }; |
185 | 191 |
|
186 | | - UNWRAP |
| 192 | + MakeCallback(req_wrap->object_, "oncomplete", 3, argv); |
187 | 193 |
|
188 | | - String::AsciiValue name(args[0]->ToString()); |
| 194 | + delete req_wrap; |
| 195 | +} |
189 | 196 |
|
190 | | - ConnectWrap* req_wrap = new ConnectWrap(); |
191 | 197 |
|
192 | | - int r = uv_pipe_connect(&req_wrap->req_, |
193 | | - &wrap->handle_, |
194 | | - *name, |
195 | | - AfterConnect); |
| 198 | +Handle<Value> PipeWrap::Connect(const Arguments& args) { |
| 199 | + HandleScope scope; |
196 | 200 |
|
197 | | - req_wrap->Dispatched(); |
| 201 | + UNWRAP |
198 | 202 |
|
199 | | - if (r) { |
200 | | - SetErrno(uv_last_error().code); |
201 | | - delete req_wrap; |
202 | | - return scope.Close(v8::Null()); |
203 | | - } else { |
204 | | - return scope.Close(req_wrap->object_); |
205 | | - } |
206 | | - } |
| 203 | + String::AsciiValue name(args[0]->ToString()); |
| 204 | + |
| 205 | + ConnectWrap* req_wrap = new ConnectWrap(); |
207 | 206 |
|
208 | | - uv_pipe_t handle_; |
209 | | -}; |
| 207 | + int r = uv_pipe_connect(&req_wrap->req_, |
| 208 | + &wrap->handle_, |
| 209 | + *name, |
| 210 | + AfterConnect); |
| 211 | + |
| 212 | + req_wrap->Dispatched(); |
| 213 | + |
| 214 | + if (r) { |
| 215 | + SetErrno(uv_last_error().code); |
| 216 | + delete req_wrap; |
| 217 | + return scope.Close(v8::Null()); |
| 218 | + } else { |
| 219 | + return scope.Close(req_wrap->object_); |
| 220 | + } |
| 221 | +} |
210 | 222 |
|
211 | 223 |
|
212 | 224 | } // namespace node |
|
0 commit comments