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.
23
#include <node_buffer.h>
25
#include <handle_wrap.h>
26
#include <stream_wrap.h>
27
#include <pipe_wrap.h>
30
assert(!args.Holder().IsEmpty()); \
31
assert(args.Holder()->InternalFieldCount() > 0); \
33
static_cast<PipeWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
36
err.code = UV_EBADF; \
38
return scope.Close(Integer::New(-1)); \
48
using v8::HandleScope;
49
using v8::FunctionTemplate;
58
Persistent<Function> pipeConstructor;
61
// TODO share with TCPWrap?
62
typedef class ReqWrap<uv_connect_t> ConnectWrap;
65
uv_pipe_t* PipeWrap::UVHandle() {
70
PipeWrap* PipeWrap::Unwrap(Local<Object> obj) {
71
assert(!obj.IsEmpty());
72
assert(obj->InternalFieldCount() > 0);
73
return static_cast<PipeWrap*>(obj->GetPointerFromInternalField(0));
77
void PipeWrap::Initialize(Handle<Object> target) {
78
StreamWrap::Initialize(target);
82
Local<FunctionTemplate> t = FunctionTemplate::New(New);
83
t->SetClassName(String::NewSymbol("Pipe"));
85
t->InstanceTemplate()->SetInternalFieldCount(1);
87
NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
88
NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
89
NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref);
91
NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
92
NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
93
NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
94
NODE_SET_PROTOTYPE_METHOD(t, "shutdown", StreamWrap::Shutdown);
96
NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
97
NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen);
98
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
99
NODE_SET_PROTOTYPE_METHOD(t, "open", Open);
102
NODE_SET_PROTOTYPE_METHOD(t, "setPendingInstances", SetPendingInstances);
105
pipeConstructor = Persistent<Function>::New(t->GetFunction());
107
target->Set(String::NewSymbol("Pipe"), pipeConstructor);
111
Handle<Value> PipeWrap::New(const Arguments& args) {
112
// This constructor should not be exposed to public javascript.
113
// Therefore we assert that we are not trying to call this as a
115
assert(args.IsConstructCall());
118
PipeWrap* wrap = new PipeWrap(args.This(), args[0]->IsTrue());
121
return scope.Close(args.This());
125
PipeWrap::PipeWrap(Handle<Object> object, bool ipc)
126
: StreamWrap(object, (uv_stream_t*) &handle_) {
127
int r = uv_pipe_init(uv_default_loop(), &handle_, ipc);
128
assert(r == 0); // How do we proxy this error up to javascript?
129
// Suggestion: uv_pipe_init() returns void.
130
handle_.data = reinterpret_cast<void*>(this);
131
UpdateWriteQueueSize();
135
Handle<Value> PipeWrap::Bind(const Arguments& args) {
140
String::AsciiValue name(args[0]->ToString());
142
int r = uv_pipe_bind(&wrap->handle_, *name);
144
// Error starting the pipe.
145
if (r) SetErrno(uv_last_error(uv_default_loop()));
147
return scope.Close(Integer::New(r));
152
Handle<Value> PipeWrap::SetPendingInstances(const Arguments& args) {
157
int instances = args[0]->Int32Value();
159
uv_pipe_pending_instances(&wrap->handle_, instances);
166
Handle<Value> PipeWrap::Listen(const Arguments& args) {
171
int backlog = args[0]->Int32Value();
173
int r = uv_listen((uv_stream_t*)&wrap->handle_, backlog, OnConnection);
175
// Error starting the pipe.
176
if (r) SetErrno(uv_last_error(uv_default_loop()));
178
return scope.Close(Integer::New(r));
182
// TODO maybe share with TCPWrap?
183
void PipeWrap::OnConnection(uv_stream_t* handle, int status) {
186
PipeWrap* wrap = static_cast<PipeWrap*>(handle->data);
187
assert(&wrap->handle_ == (uv_pipe_t*)handle);
189
// We should not be getting this callback if someone as already called
190
// uv_close() on the handle.
191
assert(wrap->object_.IsEmpty() == false);
194
// TODO Handle server error (set errno and call onconnection with NULL)
199
// Instanciate the client javascript object and handle.
200
Local<Object> client_obj = pipeConstructor->NewInstance();
202
// Unwrap the client javascript object.
203
assert(client_obj->InternalFieldCount() > 0);
204
PipeWrap* client_wrap =
205
static_cast<PipeWrap*>(client_obj->GetPointerFromInternalField(0));
207
if (uv_accept(handle, (uv_stream_t*)&client_wrap->handle_)) return;
209
// Successful accept. Call the onconnection callback in JavaScript land.
210
Local<Value> argv[1] = { client_obj };
211
MakeCallback(wrap->object_, "onconnection", 1, argv);
214
// TODO Maybe share this with TCPWrap?
215
void PipeWrap::AfterConnect(uv_connect_t* req, int status) {
216
ConnectWrap* req_wrap = (ConnectWrap*) req->data;
217
PipeWrap* wrap = (PipeWrap*) req->handle->data;
221
// The wrap and request objects should still be there.
222
assert(req_wrap->object_.IsEmpty() == false);
223
assert(wrap->object_.IsEmpty() == false);
225
bool readable, writable;
228
SetErrno(uv_last_error(uv_default_loop()));
229
readable = writable = 0;
231
readable = uv_is_readable(req->handle) != 0;
232
writable = uv_is_writable(req->handle) != 0;
235
Local<Value> argv[5] = {
236
Integer::New(status),
237
Local<Value>::New(wrap->object_),
238
Local<Value>::New(req_wrap->object_),
239
Local<Value>::New(Boolean::New(readable)),
240
Local<Value>::New(Boolean::New(writable))
243
MakeCallback(req_wrap->object_, "oncomplete", 5, argv);
249
Handle<Value> PipeWrap::Open(const Arguments& args) {
254
int fd = args[0]->IntegerValue();
256
uv_pipe_open(&wrap->handle_, fd);
258
return scope.Close(v8::Null());
262
Handle<Value> PipeWrap::Connect(const Arguments& args) {
267
String::AsciiValue name(args[0]->ToString());
269
ConnectWrap* req_wrap = new ConnectWrap();
271
uv_pipe_connect(&req_wrap->req_,
276
req_wrap->Dispatched();
278
return scope.Close(req_wrap->object_);
284
NODE_MODULE(node_pipe_wrap, node::PipeWrap::Initialize)