Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 278 lines (195 sloc) 7.401 kB
60818b9 @ry Add missing copyright headers
ry authored
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
cc0f608 @ry Add pipe_wrap
ry authored
22 #include <node.h>
23 #include <node_buffer.h>
24 #include <req_wrap.h>
61cda1e @ry PipeWrap should use HandleWrap::Close
ry authored
25 #include <handle_wrap.h>
cc0f608 @ry Add pipe_wrap
ry authored
26 #include <stream_wrap.h>
f01b241 @ry add wrapper for uv_spawn
ry authored
27 #include <pipe_wrap.h>
cc0f608 @ry Add pipe_wrap
ry authored
28
29 #define UNWRAP \
30 assert(!args.Holder().IsEmpty()); \
31 assert(args.Holder()->InternalFieldCount() > 0); \
32 PipeWrap* wrap = \
33 static_cast<PipeWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
34 if (!wrap) { \
6cc4292 @ry Display sys_errno when UV_UNKNOWN is returned
ry authored
35 uv_err_t err; \
36 err.code = UV_EBADF; \
37 SetErrno(err); \
cc0f608 @ry Add pipe_wrap
ry authored
38 return scope.Close(Integer::New(-1)); \
39 }
40
41 namespace node {
42
43 using v8::Object;
44 using v8::Handle;
45 using v8::Local;
46 using v8::Persistent;
47 using v8::Value;
48 using v8::HandleScope;
49 using v8::FunctionTemplate;
50 using v8::String;
51 using v8::Function;
52 using v8::TryCatch;
53 using v8::Context;
54 using v8::Arguments;
55 using v8::Integer;
56
187fe27 stdio binding + javascript to enable process.stdin.listen()
Igor Zinkovsky authored
57 Persistent<Function> pipeConstructor;
cc0f608 @ry Add pipe_wrap
ry authored
58
59
60 // TODO share with TCPWrap?
61 typedef class ReqWrap<uv_connect_t> ConnectWrap;
62
63
f01b241 @ry add wrapper for uv_spawn
ry authored
64 uv_pipe_t* PipeWrap::UVHandle() {
65 return &handle_;
66 }
cc0f608 @ry Add pipe_wrap
ry authored
67
68
f01b241 @ry add wrapper for uv_spawn
ry authored
69 PipeWrap* PipeWrap::Unwrap(Local<Object> obj) {
70 assert(!obj.IsEmpty());
71 assert(obj->InternalFieldCount() > 0);
72 return static_cast<PipeWrap*>(obj->GetPointerFromInternalField(0));
73 }
cc0f608 @ry Add pipe_wrap
ry authored
74
75
f01b241 @ry add wrapper for uv_spawn
ry authored
76 void PipeWrap::Initialize(Handle<Object> target) {
77 StreamWrap::Initialize(target);
cc0f608 @ry Add pipe_wrap
ry authored
78
f01b241 @ry add wrapper for uv_spawn
ry authored
79 HandleScope scope;
61cda1e @ry PipeWrap should use HandleWrap::Close
ry authored
80
f01b241 @ry add wrapper for uv_spawn
ry authored
81 Local<FunctionTemplate> t = FunctionTemplate::New(New);
82 t->SetClassName(String::NewSymbol("Pipe"));
cc0f608 @ry Add pipe_wrap
ry authored
83
f01b241 @ry add wrapper for uv_spawn
ry authored
84 t->InstanceTemplate()->SetInternalFieldCount(1);
cc0f608 @ry Add pipe_wrap
ry authored
85
f01b241 @ry add wrapper for uv_spawn
ry authored
86 NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
7e62bc9 @ry Move process.stdout unref hack to handle_wrap.cc
ry authored
87 NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
cc0f608 @ry Add pipe_wrap
ry authored
88
f01b241 @ry add wrapper for uv_spawn
ry authored
89 NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
90 NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
91 NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
92 NODE_SET_PROTOTYPE_METHOD(t, "shutdown", StreamWrap::Shutdown);
cc0f608 @ry Add pipe_wrap
ry authored
93
f01b241 @ry add wrapper for uv_spawn
ry authored
94 NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
95 NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen);
96 NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
caaa59c @ry Wrap uv_pipe_open, implement net.Stream(fd);
ry authored
97 NODE_SET_PROTOTYPE_METHOD(t, "open", Open);
cc0f608 @ry Add pipe_wrap
ry authored
98
99c9d19 binding for uv_pipe_pending_instances
Igor Zinkovsky authored
99 #ifdef _WIN32
100 NODE_SET_PROTOTYPE_METHOD(t, "setPendingInstances", SetPendingInstances);
101 #endif
102
f01b241 @ry add wrapper for uv_spawn
ry authored
103 pipeConstructor = Persistent<Function>::New(t->GetFunction());
cc0f608 @ry Add pipe_wrap
ry authored
104
f01b241 @ry add wrapper for uv_spawn
ry authored
105 target->Set(String::NewSymbol("Pipe"), pipeConstructor);
106 }
cc0f608 @ry Add pipe_wrap
ry authored
107
108
f01b241 @ry add wrapper for uv_spawn
ry authored
109 Handle<Value> PipeWrap::New(const Arguments& args) {
110 // This constructor should not be exposed to public javascript.
111 // Therefore we assert that we are not trying to call this as a
112 // normal function.
113 assert(args.IsConstructCall());
cc0f608 @ry Add pipe_wrap
ry authored
114
f01b241 @ry add wrapper for uv_spawn
ry authored
115 HandleScope scope;
471c570 @ry uv_write2 uv_read2_start binding
ry authored
116 PipeWrap* wrap = new PipeWrap(args.This(), args[0]->IsTrue());
f01b241 @ry add wrapper for uv_spawn
ry authored
117 assert(wrap);
cc0f608 @ry Add pipe_wrap
ry authored
118
f01b241 @ry add wrapper for uv_spawn
ry authored
119 return scope.Close(args.This());
120 }
cc0f608 @ry Add pipe_wrap
ry authored
121
122
471c570 @ry uv_write2 uv_read2_start binding
ry authored
123 PipeWrap::PipeWrap(Handle<Object> object, bool ipc)
124 : StreamWrap(object, (uv_stream_t*) &handle_) {
125 int r = uv_pipe_init(uv_default_loop(), &handle_, ipc);
f01b241 @ry add wrapper for uv_spawn
ry authored
126 assert(r == 0); // How do we proxy this error up to javascript?
127 // Suggestion: uv_pipe_init() returns void.
128 handle_.data = reinterpret_cast<void*>(this);
129 UpdateWriteQueueSize();
130 }
cc0f608 @ry Add pipe_wrap
ry authored
131
132
f01b241 @ry add wrapper for uv_spawn
ry authored
133 Handle<Value> PipeWrap::Bind(const Arguments& args) {
134 HandleScope scope;
cc0f608 @ry Add pipe_wrap
ry authored
135
f01b241 @ry add wrapper for uv_spawn
ry authored
136 UNWRAP
cc0f608 @ry Add pipe_wrap
ry authored
137
f01b241 @ry add wrapper for uv_spawn
ry authored
138 String::AsciiValue name(args[0]->ToString());
cc0f608 @ry Add pipe_wrap
ry authored
139
f01b241 @ry add wrapper for uv_spawn
ry authored
140 int r = uv_pipe_bind(&wrap->handle_, *name);
cc0f608 @ry Add pipe_wrap
ry authored
141
f01b241 @ry add wrapper for uv_spawn
ry authored
142 // Error starting the pipe.
6cc4292 @ry Display sys_errno when UV_UNKNOWN is returned
ry authored
143 if (r) SetErrno(uv_last_error(uv_default_loop()));
cc0f608 @ry Add pipe_wrap
ry authored
144
f01b241 @ry add wrapper for uv_spawn
ry authored
145 return scope.Close(Integer::New(r));
146 }
cc0f608 @ry Add pipe_wrap
ry authored
147
148
99c9d19 binding for uv_pipe_pending_instances
Igor Zinkovsky authored
149 #ifdef _WIN32
150 Handle<Value> PipeWrap::SetPendingInstances(const Arguments& args) {
151 HandleScope scope;
152
153 UNWRAP
154
155 int instances = args[0]->Int32Value();
156
157 uv_pipe_pending_instances(&wrap->handle_, instances);
158
159 return v8::Null();
160 }
161 #endif
162
163
f01b241 @ry add wrapper for uv_spawn
ry authored
164 Handle<Value> PipeWrap::Listen(const Arguments& args) {
165 HandleScope scope;
cc0f608 @ry Add pipe_wrap
ry authored
166
f01b241 @ry add wrapper for uv_spawn
ry authored
167 UNWRAP
cc0f608 @ry Add pipe_wrap
ry authored
168
f01b241 @ry add wrapper for uv_spawn
ry authored
169 int backlog = args[0]->Int32Value();
cc0f608 @ry Add pipe_wrap
ry authored
170
f01b241 @ry add wrapper for uv_spawn
ry authored
171 int r = uv_listen((uv_stream_t*)&wrap->handle_, backlog, OnConnection);
cc0f608 @ry Add pipe_wrap
ry authored
172
f01b241 @ry add wrapper for uv_spawn
ry authored
173 // Error starting the pipe.
6cc4292 @ry Display sys_errno when UV_UNKNOWN is returned
ry authored
174 if (r) SetErrno(uv_last_error(uv_default_loop()));
cc0f608 @ry Add pipe_wrap
ry authored
175
f01b241 @ry add wrapper for uv_spawn
ry authored
176 return scope.Close(Integer::New(r));
177 }
cc0f608 @ry Add pipe_wrap
ry authored
178
179
f01b241 @ry add wrapper for uv_spawn
ry authored
180 // TODO maybe share with TCPWrap?
181 void PipeWrap::OnConnection(uv_stream_t* handle, int status) {
182 HandleScope scope;
183
184 PipeWrap* wrap = static_cast<PipeWrap*>(handle->data);
185 assert(&wrap->handle_ == (uv_pipe_t*)handle);
186
187 // We should not be getting this callback if someone as already called
188 // uv_close() on the handle.
189 assert(wrap->object_.IsEmpty() == false);
190
191 if (status != 0) {
192 // TODO Handle server error (set errno and call onconnection with NULL)
193 assert(0);
194 return;
cc0f608 @ry Add pipe_wrap
ry authored
195 }
196
f01b241 @ry add wrapper for uv_spawn
ry authored
197 // Instanciate the client javascript object and handle.
198 Local<Object> client_obj = pipeConstructor->NewInstance();
cc0f608 @ry Add pipe_wrap
ry authored
199
f01b241 @ry add wrapper for uv_spawn
ry authored
200 // Unwrap the client javascript object.
201 assert(client_obj->InternalFieldCount() > 0);
202 PipeWrap* client_wrap =
203 static_cast<PipeWrap*>(client_obj->GetPointerFromInternalField(0));
cc0f608 @ry Add pipe_wrap
ry authored
204
f01b241 @ry add wrapper for uv_spawn
ry authored
205 int r = uv_accept(handle, (uv_stream_t*)&client_wrap->handle_);
cc0f608 @ry Add pipe_wrap
ry authored
206
f01b241 @ry add wrapper for uv_spawn
ry authored
207 // uv_accept should always work.
208 assert(r == 0);
cc0f608 @ry Add pipe_wrap
ry authored
209
f01b241 @ry add wrapper for uv_spawn
ry authored
210 // Successful accept. Call the onconnection callback in JavaScript land.
211 Local<Value> argv[1] = { client_obj };
212 MakeCallback(wrap->object_, "onconnection", 1, argv);
213 }
cc0f608 @ry Add pipe_wrap
ry authored
214
f01b241 @ry add wrapper for uv_spawn
ry authored
215 // TODO Maybe share this with TCPWrap?
216 void PipeWrap::AfterConnect(uv_connect_t* req, int status) {
217 ConnectWrap* req_wrap = (ConnectWrap*) req->data;
218 PipeWrap* wrap = (PipeWrap*) req->handle->data;
cc0f608 @ry Add pipe_wrap
ry authored
219
f01b241 @ry add wrapper for uv_spawn
ry authored
220 HandleScope scope;
221
222 // The wrap and request objects should still be there.
223 assert(req_wrap->object_.IsEmpty() == false);
224 assert(wrap->object_.IsEmpty() == false);
225
226 if (status) {
6cc4292 @ry Display sys_errno when UV_UNKNOWN is returned
ry authored
227 SetErrno(uv_last_error(uv_default_loop()));
cc0f608 @ry Add pipe_wrap
ry authored
228 }
229
f01b241 @ry add wrapper for uv_spawn
ry authored
230 Local<Value> argv[3] = {
231 Integer::New(status),
232 Local<Value>::New(wrap->object_),
233 Local<Value>::New(req_wrap->object_)
234 };
cc0f608 @ry Add pipe_wrap
ry authored
235
f01b241 @ry add wrapper for uv_spawn
ry authored
236 MakeCallback(req_wrap->object_, "oncomplete", 3, argv);
cc0f608 @ry Add pipe_wrap
ry authored
237
f01b241 @ry add wrapper for uv_spawn
ry authored
238 delete req_wrap;
239 }
cc0f608 @ry Add pipe_wrap
ry authored
240
241
caaa59c @ry Wrap uv_pipe_open, implement net.Stream(fd);
ry authored
242 Handle<Value> PipeWrap::Open(const Arguments& args) {
243 HandleScope scope;
244
245 UNWRAP
246
247 int fd = args[0]->IntegerValue();
248
249 uv_pipe_open(&wrap->handle_, fd);
250
251 return scope.Close(v8::Null());
252 }
253
254
f01b241 @ry add wrapper for uv_spawn
ry authored
255 Handle<Value> PipeWrap::Connect(const Arguments& args) {
256 HandleScope scope;
cc0f608 @ry Add pipe_wrap
ry authored
257
f01b241 @ry add wrapper for uv_spawn
ry authored
258 UNWRAP
cc0f608 @ry Add pipe_wrap
ry authored
259
f01b241 @ry add wrapper for uv_spawn
ry authored
260 String::AsciiValue name(args[0]->ToString());
261
262 ConnectWrap* req_wrap = new ConnectWrap();
cc0f608 @ry Add pipe_wrap
ry authored
263
6545a6d make updates to work with latest libuv api changes
Igor Zinkovsky authored
264 uv_pipe_connect(&req_wrap->req_,
265 &wrap->handle_,
266 *name,
267 AfterConnect);
f01b241 @ry add wrapper for uv_spawn
ry authored
268
269 req_wrap->Dispatched();
270
6545a6d make updates to work with latest libuv api changes
Igor Zinkovsky authored
271 return scope.Close(req_wrap->object_);
f01b241 @ry add wrapper for uv_spawn
ry authored
272 }
cc0f608 @ry Add pipe_wrap
ry authored
273
274
275 } // namespace node
276
cdcb111 @bnoordhuis Remove stray NODE_MODULE() semi-colons.
bnoordhuis authored
277 NODE_MODULE(node_pipe_wrap, node::PipeWrap::Initialize)
Something went wrong with that request. Please try again.