Browse Source

child_process: new stdio API for .spawn() method

v0.9.1-release
Fedor Indutny 13 years ago
parent
commit
af98fc9d5f
  1. 226
      lib/child_process.js
  2. 3
      node.gyp
  3. 5
      src/node.js
  4. 100
      src/process_wrap.cc
  5. 12
      src/tcp_wrap.cc
  6. 2
      src/tcp_wrap.h
  7. 54
      src/tty_wrap.cc
  8. 58
      src/tty_wrap.h
  9. 52
      src/udp_wrap.cc
  10. 60
      src/udp_wrap.h
  11. 6
      test/simple/test-process-wrap.js

226
lib/child_process.js

@ -25,17 +25,38 @@ var Process = process.binding('process_wrap').Process;
var util = require('util');
var constants; // if (!constants) constants = process.binding('constants');
var Pipe;
var handleWraps = {};
function handleWrapGetter(name, callback) {
var cons;
// constructors for lazy loading
function createPipe(ipc) {
// Lazy load
if (!Pipe) {
Pipe = process.binding('pipe_wrap').Pipe;
Object.defineProperty(handleWraps, name, {
get: function() {
if (cons !== undefined) return cons;
return cons = callback();
}
});
}
handleWrapGetter('Pipe', function() {
return process.binding('pipe_wrap').Pipe;
});
handleWrapGetter('TTY', function() {
return process.binding('tty_wrap').TTY;
});
handleWrapGetter('TCP', function() {
return process.binding('tcp_wrap').TCP;
});
return new Pipe(ipc);
handleWrapGetter('UDP', function() {
return process.binding('udp_wrap').UDP;
});
// constructors for lazy loading
function createPipe(ipc) {
return new handleWraps.Pipe(ipc);
}
function createSocket(pipe, readable) {
@ -414,39 +435,18 @@ exports.fork = function(modulePath /*, args, options*/) {
execArgv = options.execArgv || process.execArgv;
args = execArgv.concat([modulePath], args);
// Don't allow stdinStream and customFds since a stdin channel will be used
if (options.stdinStream) {
throw new Error('stdinStream not allowed for fork()');
}
if (options.customFds) {
throw new Error('customFds not allowed for fork()');
}
// Leave stdin open for the IPC channel. stdout and stderr should be the
// same as the parent's if silent isn't set.
options.customFds = (options.silent ? [-1, -1, -1] : [-1, 1, 2]);
// Just need to set this - child process won't actually use the fd.
// For backwards compat - this can be changed to 'NODE_CHANNEL' before v0.6.
options.env = util._extend({}, options.env || process.env);
options.env.NODE_CHANNEL_FD = 42;
// stdin is the IPC channel.
options.stdinStream = createPipe(true);
var child = spawn(process.execPath, args, options);
options.stdio = options.silent ? ['ipc', 'pipe', 'pipe'] : ['ipc', 1, 2];
setupChannel(child, options.stdinStream);
return child;
return spawn(process.execPath, args, options);
};
exports._forkChild = function() {
exports._forkChild = function(fd) {
// set process.send()
var p = createPipe(true);
p.open(0);
p.open(fd);
setupChannel(process, p);
};
@ -591,6 +591,11 @@ var spawn = exports.spawn = function(file, args, options) {
}
var child = new ChildProcess();
if (options && options.customFds && !options.stdio) {
options.stdio = options.customFds.map(function(fd) {
return fd === -1 ? 'pipe' : fd;
});
}
child.spawn({
file: file,
@ -598,8 +603,7 @@ var spawn = exports.spawn = function(file, args, options) {
cwd: options ? options.cwd : null,
windowsVerbatimArguments: !!(options && options.windowsVerbatimArguments),
envPairs: envPairs,
customFds: options ? options.customFds : null,
stdinStream: options ? options.stdinStream : null,
stdio: options ? options.stdio : null,
uid: options ? options.uid : null,
gid: options ? options.gid : null
});
@ -658,45 +662,110 @@ function ChildProcess() {
util.inherits(ChildProcess, EventEmitter);
function setStreamOption(name, index, options) {
// Skip if we already have options.stdinStream
if (options[name]) return;
function getHandleWrapType(stream) {
if (stream instanceof handleWraps.Pipe) return 'pipe';
if (stream instanceof handleWraps.TTY) return 'tty';
if (stream instanceof handleWraps.TCP) return 'tcp';
if (stream instanceof handleWraps.UDP) return 'udp';
if (options.customFds &&
typeof options.customFds[index] == 'number' &&
options.customFds[index] !== -1) {
if (options.customFds[index] === index) {
options[name] = null;
} else {
throw new Error('customFds not yet supported');
}
} else {
options[name] = createPipe();
}
return false;
}
ChildProcess.prototype.spawn = function(options) {
var self = this;
var self = this,
ipc,
ipcFd,
// If no `stdio` option was given - use default
stdio = options.stdio || 'pipe';
// Replace shortcut with an array
if (typeof stdio === 'string') {
switch (stdio) {
case 'ignore': stdio = ['ignore', 'ignore', 'ignore']; break;
case 'pipe': stdio = ['pipe', 'pipe', 'pipe']; break;
case 'inherit': stdio = [0, 1, 2]; break;
default: throw new TypeError('Incorrect value of stdio option: ' + stdio);
}
} else if (!Array.isArray(stdio)) {
throw new TypeError('Incorrect value of stdio option: ' + stdio);
}
// At least 3 stdio will be created
if (stdio.length < 3) {
stdio = stdio.concat(new Array(3 - stdio.length));
}
// Translate stdio into C++-readable form
// (i.e. PipeWraps or fds)
stdio = stdio.reduce(function(acc, stdio, i) {
function cleanup() {
acc.filter(function(stdio) {
return stdio.type === 'pipe' || stdio.type === 'ipc';
}).forEach(function(stdio) {
stdio.handle.close();
});
}
setStreamOption('stdinStream', 0, options);
setStreamOption('stdoutStream', 1, options);
setStreamOption('stderrStream', 2, options);
// Defaults
if (stdio === undefined || stdio === null) {
stdio = i < 3 ? 'pipe' : 'ignore';
}
var r = this._handle.spawn(options);
if (stdio === 'ignore') {
acc.push({type: 'ignore'});
} else if (stdio === 'pipe' || typeof stdio === 'number' && stdio < 0) {
acc.push({type: 'pipe', handle: createPipe()});
} else if (stdio === 'ipc') {
if (ipc !== undefined) {
// Cleanup previously created pipes
cleanup();
throw Error('Child process can have only one IPC pipe');
}
if (r) {
if (options.stdinStream) {
options.stdinStream.close();
ipc = createPipe(true);
ipcFd = i;
acc.push({ type: 'pipe', handle: ipc });
} else if (typeof stdio === 'number' || typeof stdio.fd === 'number') {
acc.push({ type: 'fd', fd: stdio.fd || stdio });
} else if (getHandleWrapType(stdio) || getHandleWrapType(stdio.handle) ||
getHandleWrapType(stdio._handle)) {
var handle = getHandleWrapType(stdio) ?
stdio :
getHandleWrapType(stdio.handle) ? stdio.handle : stdio._handle;
acc.push({
type: 'wrap',
wrapType: getHandleWrapType(handle),
handle: handle
});
} else {
// Cleanup
cleanup();
throw new TypeError('Incorrect value for stdio stream: ' + stdio);
}
if (options.stdoutStream) {
options.stdoutStream.close();
return acc;
}, []);
options.stdio = stdio;
if (ipc !== undefined) {
// Let child process know about opened IPC channel
options.envPairs = options.envPairs || [];
options.envPairs.push('NODE_CHANNEL_FD=' + ipcFd);
}
if (options.stderrStream) {
options.stderrStream.close();
var r = this._handle.spawn(options);
if (r) {
// Close all opened fds on error
stdio.forEach(function(stdio) {
if (stdio.type === 'pipe') {
stdio.handle.close();
}
});
this._handle.close();
this._handle = null;
@ -705,25 +774,36 @@ ChildProcess.prototype.spawn = function(options) {
this.pid = this._handle.pid;
if (options.stdinStream) {
this.stdin = createSocket(options.stdinStream, false);
}
stdio.forEach(function(stdio, i) {
if (stdio.type === 'ignore') return;
if (stdio.handle) {
// when i === 0 - we're dealing with stdin
// (which is the only one writable pipe)
stdio.socket = createSocket(stdio.handle, i > 0);
if (options.stdoutStream) {
this.stdout = createSocket(options.stdoutStream, true);
this._closesNeeded++;
this.stdout.on('close', function() {
if (i > 0) {
self._closesNeeded++;
stdio.socket.on('close', function() {
maybeClose(self);
});
}
}
});
if (options.stderrStream) {
this.stderr = createSocket(options.stderrStream, true);
this._closesNeeded++;
this.stderr.on('close', function() {
maybeClose(self);
this.stdin = stdio.length >= 1 && stdio[0].socket !== undefined ?
stdio[0].socket : null;
this.stdout = stdio.length >= 2 && stdio[1].socket !== undefined ?
stdio[1].socket : null;
this.stderr = stdio.length >= 3 && stdio[2].socket !== undefined ?
stdio[2].socket : null;
this.stdio = stdio.map(function(stdio) {
return stdio.socket === undefined ? null : stdio.socket;
});
}
// Add .send() method and start listening for IPC data
if (ipc !== undefined) setupChannel(this, ipc);
return r;
};

3
node.gyp

@ -109,6 +109,9 @@
'src/node_version.h',
'src/ngx-queue.h',
'src/pipe_wrap.h',
'src/tty_wrap.h',
'src/tcp_wrap.h',
'src/udp_wrap.h',
'src/req_wrap.h',
'src/slab_allocator.h',
'src/stream_wrap.h',

5
src/node.js

@ -482,7 +482,8 @@
// If we were spawned with env NODE_CHANNEL_FD then load that up and
// start parsing data from that stream.
if (process.env.NODE_CHANNEL_FD) {
assert(parseInt(process.env.NODE_CHANNEL_FD) >= 0);
var fd = parseInt(process.env.NODE_CHANNEL_FD, 10);
assert(fd >= 0);
// Make sure it's not accidentally inherited by child processes.
delete process.env.NODE_CHANNEL_FD;
@ -494,7 +495,7 @@
// FIXME is this really necessary?
process.binding('tcp_wrap');
cp._forkChild();
cp._forkChild(fd);
assert(process.send);
}
}

100
src/process_wrap.cc

@ -22,6 +22,10 @@
#include "node.h"
#include "handle_wrap.h"
#include "pipe_wrap.h"
#include "tty_wrap.h"
#include "tcp_wrap.h"
#include "udp_wrap.h"
#include <string.h>
#include <stdlib.h>
@ -36,6 +40,7 @@ using v8::HandleScope;
using v8::FunctionTemplate;
using v8::String;
using v8::Array;
using v8::Number;
using v8::Function;
using v8::TryCatch;
using v8::Context;
@ -82,6 +87,56 @@ class ProcessWrap : public HandleWrap {
ProcessWrap(Handle<Object> object) : HandleWrap(object, NULL) { }
~ProcessWrap() { }
static void ParseStdioOptions(Local<Object> js_options,
uv_process_options_t* options) {
Local<Array> stdios = js_options
->Get(String::NewSymbol("stdio")).As<Array>();
int len = stdios->Length();
options->stdio = new uv_stdio_container_t[len];
options->stdio_count = len;
for (int i = 0; i < len; i++) {
Local<Object> stdio = stdios
->Get(Number::New(static_cast<double>(i))).As<Object>();
Local<Value> type = stdio->Get(String::NewSymbol("type"));
if (type->Equals(String::NewSymbol("ignore"))) {
options->stdio[i].flags = UV_IGNORE;
} else if (type->Equals(String::NewSymbol("pipe"))) {
options->stdio[i].flags = UV_CREATE_PIPE;
options->stdio[i].data.stream = reinterpret_cast<uv_stream_t*>(
PipeWrap::Unwrap(stdio
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
} else if (type->Equals(String::NewSymbol("wrap"))) {
uv_stream_t* stream = NULL;
Local<Value> wrapType = stdio->Get(String::NewSymbol("wrapType"));
if (wrapType->Equals(String::NewSymbol("pipe"))) {
stream = reinterpret_cast<uv_stream_t*>(PipeWrap::Unwrap(stdio
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
} else if (wrapType->Equals(String::NewSymbol("tty"))) {
stream = reinterpret_cast<uv_stream_t*>(TTYWrap::Unwrap(stdio
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
} else if (wrapType->Equals(String::NewSymbol("tcp"))) {
stream = reinterpret_cast<uv_stream_t*>(TCPWrap::Unwrap(stdio
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
} else if (wrapType->Equals(String::NewSymbol("udp"))) {
stream = reinterpret_cast<uv_stream_t*>(UDPWrap::Unwrap(stdio
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
}
assert(stream != NULL);
options->stdio[i].flags = UV_INHERIT_STREAM;
options->stdio[i].data.stream = stream;
} else {
int fd = static_cast<int>(
stdio->Get(String::NewSymbol("fd"))->IntegerValue());
options->stdio[i].flags = UV_INHERIT_FD;
options->stdio[i].data.fd = fd;
}
}
}
static Handle<Value> Spawn(const Arguments& args) {
HandleScope scope;
@ -169,47 +224,8 @@ class ProcessWrap : public HandleWrap {
options.env[envc] = NULL;
}
uv_stdio_container_t stdio[3];
memset(stdio, 0, sizeof(stdio));
options.stdio = stdio;
options.stdio_count = 3;
options.stdio[0].flags = UV_IGNORE;
options.stdio[1].flags = UV_IGNORE;
options.stdio[2].flags = UV_IGNORE;
// options.stdin_stream
Local<Value> stdin_stream_v = js_options->Get(
String::NewSymbol("stdinStream"));
if (!stdin_stream_v.IsEmpty() && stdin_stream_v->IsObject()) {
PipeWrap* stdin_wrap = PipeWrap::Unwrap(stdin_stream_v->ToObject());
options.stdio[0].flags = static_cast<uv_stdio_flags>(
UV_CREATE_PIPE | UV_WRITABLE_PIPE);
options.stdio[0].data.stream = reinterpret_cast<uv_stream_t*>(
stdin_wrap->UVHandle());
}
// options.stdout_stream
Local<Value> stdout_stream_v = js_options->Get(
String::NewSymbol("stdoutStream"));
if (!stdout_stream_v.IsEmpty() && stdout_stream_v->IsObject()) {
PipeWrap* stdout_wrap = PipeWrap::Unwrap(stdout_stream_v->ToObject());
options.stdio[1].flags = static_cast<uv_stdio_flags>(
UV_CREATE_PIPE | UV_READABLE_PIPE);
options.stdio[1].data.stream = reinterpret_cast<uv_stream_t*>(
stdout_wrap->UVHandle());
}
// options.stderr_stream
Local<Value> stderr_stream_v = js_options->Get(
String::NewSymbol("stderrStream"));
if (!stderr_stream_v.IsEmpty() && stderr_stream_v->IsObject()) {
PipeWrap* stderr_wrap = PipeWrap::Unwrap(stderr_stream_v->ToObject());
options.stdio[2].flags = static_cast<uv_stdio_flags>(
UV_CREATE_PIPE | UV_READABLE_PIPE);
options.stdio[2].data.stream = reinterpret_cast<uv_stream_t*>(
stderr_wrap->UVHandle());
}
// options.stdio
ParseStdioOptions(js_options, &options);
// options.windows_verbatim_arguments
if (js_options->Get(String::NewSymbol("windowsVerbatimArguments"))->
@ -238,6 +254,8 @@ class ProcessWrap : public HandleWrap {
delete [] options.env;
}
delete[] options.stdio;
return scope.Close(Integer::New(r));
}

12
src/tcp_wrap.cc

@ -128,6 +128,18 @@ void TCPWrap::Initialize(Handle<Object> target) {
}
TCPWrap* TCPWrap::Unwrap(Local<Object> obj) {
assert(!obj.IsEmpty());
assert(obj->InternalFieldCount() > 0);
return static_cast<TCPWrap*>(obj->GetPointerFromInternalField(0));
}
uv_tcp_t* TCPWrap::UVHandle() {
return &handle_;
}
Handle<Value> TCPWrap::New(const Arguments& args) {
// This constructor should not be exposed to public javascript.
// Therefore we assert that we are not trying to call this as a

2
src/tcp_wrap.h

@ -31,6 +31,8 @@ class TCPWrap : public StreamWrap {
static TCPWrap* Unwrap(v8::Local<v8::Object> obj);
static void Initialize(v8::Handle<v8::Object> target);
uv_tcp_t* UVHandle();
private:
TCPWrap(v8::Handle<v8::Object> object);
~TCPWrap();

54
src/tty_wrap.cc

@ -24,6 +24,7 @@
#include "req_wrap.h"
#include "handle_wrap.h"
#include "stream_wrap.h"
#include "tty_wrap.h"
namespace node {
@ -42,9 +43,8 @@ using v8::Arguments;
using v8::Integer;
using v8::Undefined;
class TTYWrap : StreamWrap {
public:
static void Initialize(Handle<Object> target) {
void TTYWrap::Initialize(Handle<Object> target) {
StreamWrap::Initialize(target);
HandleScope scope;
@ -72,10 +72,22 @@ class TTYWrap : StreamWrap {
NODE_SET_METHOD(target, "guessHandleType", GuessHandleType);
target->Set(String::NewSymbol("TTY"), t->GetFunction());
}
}
TTYWrap* TTYWrap::Unwrap(Local<Object> obj) {
assert(!obj.IsEmpty());
assert(obj->InternalFieldCount() > 0);
return static_cast<TTYWrap*>(obj->GetPointerFromInternalField(0));
}
private:
static Handle<Value> GuessHandleType(const Arguments& args) {
uv_tty_t* TTYWrap::UVHandle() {
return &handle_;
}
Handle<Value> TTYWrap::GuessHandleType(const Arguments& args) {
HandleScope scope;
int fd = args[0]->Int32Value();
assert(fd >= 0);
@ -96,16 +108,18 @@ class TTYWrap : StreamWrap {
assert(0);
return v8::Undefined();
}
}
}
static Handle<Value> IsTTY(const Arguments& args) {
Handle<Value> TTYWrap::IsTTY(const Arguments& args) {
HandleScope scope;
int fd = args[0]->Int32Value();
assert(fd >= 0);
return uv_guess_handle(fd) == UV_TTY ? v8::True() : v8::False();
}
}
static Handle<Value> GetWindowSize(const Arguments& args) {
Handle<Value> TTYWrap::GetWindowSize(const Arguments& args) {
HandleScope scope;
UNWRAP(TTYWrap)
@ -123,9 +137,10 @@ class TTYWrap : StreamWrap {
a->Set(1, Integer::New(height));
return scope.Close(a);
}
}
static Handle<Value> SetRawMode(const Arguments& args) {
Handle<Value> TTYWrap::SetRawMode(const Arguments& args) {
HandleScope scope;
UNWRAP(TTYWrap)
@ -137,9 +152,10 @@ class TTYWrap : StreamWrap {
}
return scope.Close(Integer::New(r));
}
}
static Handle<Value> New(const Arguments& args) {
Handle<Value> TTYWrap::New(const Arguments& args) {
HandleScope scope;
// This constructor should not be exposed to public javascript.
@ -155,15 +171,13 @@ class TTYWrap : StreamWrap {
wrap->UpdateWriteQueueSize();
return scope.Close(args.This());
}
}
TTYWrap(Handle<Object> object, int fd, bool readable)
TTYWrap::TTYWrap(Handle<Object> object, int fd, bool readable)
: StreamWrap(object, (uv_stream_t*)&handle_) {
uv_tty_init(uv_default_loop(), &handle_, fd, readable);
}
uv_tty_t handle_;
};
}
} // namespace node

58
src/tty_wrap.h

@ -0,0 +1,58 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef TTY_WRAP_H_
#define TTY_WRAP_H_
#include "handle_wrap.h"
#include "stream_wrap.h"
namespace node {
using v8::Object;
using v8::Handle;
using v8::Local;
using v8::Value;
using v8::Arguments;
class TTYWrap : StreamWrap {
public:
static void Initialize(Handle<Object> target);
static TTYWrap* Unwrap(Local<Object> obj);
uv_tty_t* UVHandle();
private:
TTYWrap(Handle<Object> object, int fd, bool readable);
static Handle<Value> GuessHandleType(const Arguments& args);
static Handle<Value> IsTTY(const Arguments& args);
static Handle<Value> GetWindowSize(const Arguments& args);
static Handle<Value> SetRawMode(const Arguments& args);
static Handle<Value> New(const Arguments& args);
uv_tty_t handle_;
};
} // namespace node
#endif // TTY_WRAP_H_

52
src/udp_wrap.cc

@ -24,6 +24,7 @@
#include "slab_allocator.h"
#include "req_wrap.h"
#include "handle_wrap.h"
#include "udp_wrap.h"
#include <stdlib.h>
@ -60,45 +61,6 @@ static Persistent<String> onmessage_sym;
static SlabAllocator slab_allocator(SLAB_SIZE);
class UDPWrap: public HandleWrap {
public:
static void Initialize(Handle<Object> target);
static Handle<Value> New(const Arguments& args);
static Handle<Value> Bind(const Arguments& args);
static Handle<Value> Send(const Arguments& args);
static Handle<Value> Bind6(const Arguments& args);
static Handle<Value> Send6(const Arguments& args);
static Handle<Value> RecvStart(const Arguments& args);
static Handle<Value> RecvStop(const Arguments& args);
static Handle<Value> GetSockName(const Arguments& args);
static Handle<Value> AddMembership(const Arguments& args);
static Handle<Value> DropMembership(const Arguments& args);
static Handle<Value> SetMulticastTTL(const Arguments& args);
static Handle<Value> SetMulticastLoopback(const Arguments& args);
static Handle<Value> SetBroadcast(const Arguments& args);
static Handle<Value> SetTTL(const Arguments& args);
private:
UDPWrap(Handle<Object> object);
virtual ~UDPWrap();
static Handle<Value> DoBind(const Arguments& args, int family);
static Handle<Value> DoSend(const Arguments& args, int family);
static Handle<Value> SetMembership(const Arguments& args,
uv_membership membership);
static uv_buf_t OnAlloc(uv_handle_t* handle, size_t suggested_size);
static void OnSend(uv_udp_send_t* req, int status);
static void OnRecv(uv_udp_t* handle,
ssize_t nread,
uv_buf_t buf,
struct sockaddr* addr,
unsigned flags);
uv_udp_t handle_;
};
UDPWrap::UDPWrap(Handle<Object> object): HandleWrap(object,
(uv_handle_t*)&handle_) {
int r = uv_udp_init(uv_default_loop(), &handle_);
@ -426,6 +388,18 @@ void UDPWrap::OnRecv(uv_udp_t* handle,
}
UDPWrap* UDPWrap::Unwrap(Local<Object> obj) {
assert(!obj.IsEmpty());
assert(obj->InternalFieldCount() > 0);
return static_cast<UDPWrap*>(obj->GetPointerFromInternalField(0));
}
uv_udp_t* UDPWrap::UVHandle() {
return &handle_;
}
} // namespace node
NODE_MODULE(node_udp_wrap, node::UDPWrap::Initialize)

60
src/udp_wrap.h

@ -0,0 +1,60 @@
#ifndef UDP_WRAP_H_
#define UDP_WRAP_H_
#include "node.h"
#include "req_wrap.h"
#include "handle_wrap.h"
namespace node {
using v8::Object;
using v8::Handle;
using v8::Local;
using v8::Value;
using v8::String;
using v8::Arguments;
class UDPWrap: public HandleWrap {
public:
static void Initialize(Handle<Object> target);
static Handle<Value> New(const Arguments& args);
static Handle<Value> Bind(const Arguments& args);
static Handle<Value> Send(const Arguments& args);
static Handle<Value> Bind6(const Arguments& args);
static Handle<Value> Send6(const Arguments& args);
static Handle<Value> RecvStart(const Arguments& args);
static Handle<Value> RecvStop(const Arguments& args);
static Handle<Value> GetSockName(const Arguments& args);
static Handle<Value> AddMembership(const Arguments& args);
static Handle<Value> DropMembership(const Arguments& args);
static Handle<Value> SetMulticastTTL(const Arguments& args);
static Handle<Value> SetMulticastLoopback(const Arguments& args);
static Handle<Value> SetBroadcast(const Arguments& args);
static Handle<Value> SetTTL(const Arguments& args);
static UDPWrap* Unwrap(Local<Object> obj);
uv_udp_t* UVHandle();
private:
UDPWrap(Handle<Object> object);
virtual ~UDPWrap();
static Handle<Value> DoBind(const Arguments& args, int family);
static Handle<Value> DoSend(const Arguments& args, int family);
static Handle<Value> SetMembership(const Arguments& args,
uv_membership membership);
static uv_buf_t OnAlloc(uv_handle_t* handle, size_t suggested_size);
static void OnSend(uv_udp_send_t* req, int status);
static void OnRecv(uv_udp_t* handle,
ssize_t nread,
uv_buf_t buf,
struct sockaddr* addr,
unsigned flags);
uv_udp_t handle_;
};
} // namespace node
#endif // UDP_WRAP_H_

6
test/simple/test-process-wrap.js

@ -58,7 +58,11 @@ pipe.onread = function(b, off, len) {
p.spawn({
file: process.execPath,
args: [process.execPath, '-v'],
stdoutStream: pipe
stdio: [
{ type: 'ignore' },
{ type: 'pipe', handle: pipe },
{ type: 'ignore' }
]
});

Loading…
Cancel
Save