The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / src / pipe_wrap.cc @ 0544a586

History | View | Annotate | Download (7.85 KB)

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
#include "node.h"
23
#include "node_buffer.h"
24
#include "req_wrap.h"
25
#include "handle_wrap.h"
26
#include "stream_wrap.h"
27
#include "pipe_wrap.h"
28

    
29
namespace node {
30

    
31
using v8::Object;
32
using v8::Handle;
33
using v8::Local;
34
using v8::Persistent;
35
using v8::Value;
36
using v8::HandleScope;
37
using v8::FunctionTemplate;
38
using v8::String;
39
using v8::Function;
40
using v8::TryCatch;
41
using v8::Context;
42
using v8::Arguments;
43
using v8::Integer;
44
using v8::Boolean;
45

    
46
Persistent<Function> pipeConstructor;
47

    
48
static Persistent<String> onconnection_sym;
49
static Persistent<String> oncomplete_sym;
50

    
51

    
52
// TODO share with TCPWrap?
53
typedef class ReqWrap<uv_connect_t> ConnectWrap;
54

    
55

    
56
uv_pipe_t* PipeWrap::UVHandle() {
57
  return &handle_;
58
}
59

    
60

    
61
Local<Object> PipeWrap::Instantiate() {
62
  HandleScope scope;
63
  assert(!pipeConstructor.IsEmpty());
64
  return scope.Close(pipeConstructor->NewInstance());
65
}
66

    
67

    
68
PipeWrap* PipeWrap::Unwrap(Local<Object> obj) {
69
  assert(!obj.IsEmpty());
70
  assert(obj->InternalFieldCount() > 0);
71
  return static_cast<PipeWrap*>(obj->GetPointerFromInternalField(0));
72
}
73

    
74

    
75
void PipeWrap::Initialize(Handle<Object> target) {
76
  StreamWrap::Initialize(target);
77

    
78
  HandleScope scope;
79

    
80
  Local<FunctionTemplate> t = FunctionTemplate::New(New);
81
  t->SetClassName(String::NewSymbol("Pipe"));
82

    
83
  t->InstanceTemplate()->SetInternalFieldCount(1);
84

    
85
  NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
86
  NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
87

    
88
  NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
89
  NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
90
  NODE_SET_PROTOTYPE_METHOD(t, "shutdown", StreamWrap::Shutdown);
91

    
92
  NODE_SET_PROTOTYPE_METHOD(t, "writeBuffer", StreamWrap::WriteBuffer);
93
  NODE_SET_PROTOTYPE_METHOD(t, "writeAsciiString", StreamWrap::WriteAsciiString);
94
  NODE_SET_PROTOTYPE_METHOD(t, "writeUtf8String", StreamWrap::WriteUtf8String);
95
  NODE_SET_PROTOTYPE_METHOD(t, "writeUcs2String", StreamWrap::WriteUcs2String);
96

    
97
  NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
98
  NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen);
99
  NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
100
  NODE_SET_PROTOTYPE_METHOD(t, "open", Open);
101

    
102
#ifdef _WIN32
103
  NODE_SET_PROTOTYPE_METHOD(t, "setPendingInstances", SetPendingInstances);
104
#endif
105

    
106
  pipeConstructor = Persistent<Function>::New(t->GetFunction());
107

    
108
  target->Set(String::NewSymbol("Pipe"), pipeConstructor);
109
}
110

    
111

    
112
Handle<Value> PipeWrap::New(const Arguments& args) {
113
  // This constructor should not be exposed to public javascript.
114
  // Therefore we assert that we are not trying to call this as a
115
  // normal function.
116
  assert(args.IsConstructCall());
117

    
118
  HandleScope scope;
119
  PipeWrap* wrap = new PipeWrap(args.This(), args[0]->IsTrue());
120
  assert(wrap);
121

    
122
  return scope.Close(args.This());
123
}
124

    
125

    
126
PipeWrap::PipeWrap(Handle<Object> object, bool ipc)
127
    : StreamWrap(object, (uv_stream_t*) &handle_) {
128
  int r = uv_pipe_init(uv_default_loop(), &handle_, ipc);
129
  assert(r == 0); // How do we proxy this error up to javascript?
130
                  // Suggestion: uv_pipe_init() returns void.
131
  handle_.data = reinterpret_cast<void*>(this);
132
  UpdateWriteQueueSize();
133
}
134

    
135

    
136
Handle<Value> PipeWrap::Bind(const Arguments& args) {
137
  HandleScope scope;
138

    
139
  UNWRAP(PipeWrap)
140

    
141
  String::AsciiValue name(args[0]);
142

    
143
  int r = uv_pipe_bind(&wrap->handle_, *name);
144

    
145
  // Error starting the pipe.
146
  if (r) SetErrno(uv_last_error(uv_default_loop()));
147

    
148
  return scope.Close(Integer::New(r));
149
}
150

    
151

    
152
#ifdef _WIN32
153
Handle<Value> PipeWrap::SetPendingInstances(const Arguments& args) {
154
  HandleScope scope;
155

    
156
  UNWRAP(PipeWrap)
157

    
158
  int instances = args[0]->Int32Value();
159

    
160
  uv_pipe_pending_instances(&wrap->handle_, instances);
161

    
162
  return v8::Null();
163
}
164
#endif
165

    
166

    
167
Handle<Value> PipeWrap::Listen(const Arguments& args) {
168
  HandleScope scope;
169

    
170
  UNWRAP(PipeWrap)
171

    
172
  int backlog = args[0]->Int32Value();
173

    
174
  int r = uv_listen((uv_stream_t*)&wrap->handle_, backlog, OnConnection);
175

    
176
  // Error starting the pipe.
177
  if (r) SetErrno(uv_last_error(uv_default_loop()));
178

    
179
  return scope.Close(Integer::New(r));
180
}
181

    
182

    
183
// TODO maybe share with TCPWrap?
184
void PipeWrap::OnConnection(uv_stream_t* handle, int status) {
185
  HandleScope scope;
186

    
187
  PipeWrap* wrap = static_cast<PipeWrap*>(handle->data);
188
  assert(&wrap->handle_ == (uv_pipe_t*)handle);
189

    
190
  // We should not be getting this callback if someone as already called
191
  // uv_close() on the handle.
192
  assert(wrap->object_.IsEmpty() == false);
193

    
194
  if (status != 0) {
195
    SetErrno(uv_last_error(uv_default_loop()));
196
    MakeCallback(wrap->object_, "onconnection", 0, NULL);
197
    return;
198
  }
199

    
200
  // Instanciate the client javascript object and handle.
201
  Local<Object> client_obj = pipeConstructor->NewInstance();
202

    
203
  // Unwrap the client javascript object.
204
  assert(client_obj->InternalFieldCount() > 0);
205
  PipeWrap* client_wrap =
206
      static_cast<PipeWrap*>(client_obj->GetPointerFromInternalField(0));
207

    
208
  if (uv_accept(handle, (uv_stream_t*)&client_wrap->handle_)) return;
209

    
210
  // Successful accept. Call the onconnection callback in JavaScript land.
211
  Local<Value> argv[1] = { client_obj };
212
  if (onconnection_sym.IsEmpty()) {
213
    onconnection_sym = NODE_PSYMBOL("onconnection");
214
  }
215
  MakeCallback(wrap->object_, onconnection_sym, ARRAY_SIZE(argv), argv);
216
}
217

    
218
// TODO Maybe share this with TCPWrap?
219
void PipeWrap::AfterConnect(uv_connect_t* req, int status) {
220
  ConnectWrap* req_wrap = (ConnectWrap*) req->data;
221
  PipeWrap* wrap = (PipeWrap*) req->handle->data;
222

    
223
  HandleScope scope;
224

    
225
  // The wrap and request objects should still be there.
226
  assert(req_wrap->object_.IsEmpty() == false);
227
  assert(wrap->object_.IsEmpty() == false);
228

    
229
  bool readable, writable;
230

    
231
  if (status) {
232
    SetErrno(uv_last_error(uv_default_loop()));
233
    readable = writable = 0;
234
  } else {
235
    readable = uv_is_readable(req->handle) != 0;
236
    writable = uv_is_writable(req->handle) != 0;
237
  }
238

    
239
  Local<Value> argv[5] = {
240
    Integer::New(status),
241
    Local<Value>::New(wrap->object_),
242
    Local<Value>::New(req_wrap->object_),
243
    Local<Value>::New(Boolean::New(readable)),
244
    Local<Value>::New(Boolean::New(writable))
245
  };
246

    
247
  if (oncomplete_sym.IsEmpty()) {
248
    oncomplete_sym = NODE_PSYMBOL("oncomplete");
249
  }
250
  MakeCallback(req_wrap->object_, oncomplete_sym, ARRAY_SIZE(argv), argv);
251

    
252
  delete req_wrap;
253
}
254

    
255

    
256
Handle<Value> PipeWrap::Open(const Arguments& args) {
257
  HandleScope scope;
258

    
259
  UNWRAP(PipeWrap)
260

    
261
  int fd = args[0]->IntegerValue();
262

    
263
  uv_pipe_open(&wrap->handle_, fd);
264

    
265
  return scope.Close(v8::Null());
266
}
267

    
268

    
269
Handle<Value> PipeWrap::Connect(const Arguments& args) {
270
  HandleScope scope;
271

    
272
  UNWRAP(PipeWrap)
273

    
274
  String::AsciiValue name(args[0]);
275

    
276
  ConnectWrap* req_wrap = new ConnectWrap();
277

    
278
  uv_pipe_connect(&req_wrap->req_,
279
                  &wrap->handle_,
280
                  *name,
281
                  AfterConnect);
282

    
283
  req_wrap->Dispatched();
284

    
285
  return scope.Close(req_wrap->object_);
286
}
287

    
288

    
289
}  // namespace node
290

    
291
NODE_MODULE(node_pipe_wrap, node::PipeWrap::Initialize)