Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit bdf7ac2

Browse files
AndreasMadsenbnoordhuis
authored andcommittedMar 7, 2013
child_process: support sending dgram socket
child.send can send net servers and sockets. Now that we have support for dgram clusters this functionality should be extended to include dgram sockets.
·
1 parent 7169436 commit bdf7ac2

File tree

4 files changed

+154
-13
lines changed

4 files changed

+154
-13
lines changed
 

‎doc/api/child_process.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ And the child would the receive the server object as:
200200
Note that the server is now shared between the parent and child, this means
201201
that some connections will be handled by the parent and some by the child.
202202

203+
For `dgram` servers the workflow is exactly the same. Here you listen on
204+
a `message` event instead of `connection` and use `server.bind` instead of
205+
`server.listen`.
206+
203207
#### Example: sending socket object
204208

205209
Here is an example of sending a socket. It will spawn two children and handle

‎lib/child_process.js

+21
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
var EventEmitter = require('events').EventEmitter;
2323
var net = require('net');
24+
var dgram = require('dgram');
2425
var Process = process.binding('process_wrap').Process;
2526
var util = require('util');
2627
var constants; // if (!constants) constants = process.binding('constants');
@@ -167,6 +168,24 @@ var handleConversion = {
167168
got: function(message, handle, emit) {
168169
emit(handle);
169170
}
171+
},
172+
173+
'dgram.Socket': {
174+
simultaneousAccepts: false,
175+
176+
send: function(message, socket) {
177+
message.dgramType = socket.type;
178+
179+
return socket._handle;
180+
},
181+
182+
got: function(message, handle, emit) {
183+
var socket = new dgram.Socket(message.dgramType);
184+
185+
socket.bind(handle, function() {
186+
emit(socket);
187+
});
188+
}
170189
}
171190
};
172191

@@ -377,6 +396,8 @@ function setupChannel(target, channel) {
377396
} else if (handle instanceof process.binding('tcp_wrap').TCP ||
378397
handle instanceof process.binding('pipe_wrap').Pipe) {
379398
message.type = 'net.Native';
399+
} else if (handle instanceof dgram.Socket) {
400+
message.type = 'dgram.Socket';
380401
} else if (handle instanceof process.binding('udp_wrap').UDP) {
381402
message.type = 'dgram.Native';
382403
} else {

‎lib/dgram.js

+26-13
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,20 @@ function startListening(socket) {
141141
socket.emit('listening');
142142
}
143143

144+
function replaceHandle(self, newHandle) {
144145

145-
Socket.prototype.bind = function(port, address, callback) {
146+
// Set up the handle that we got from master.
147+
newHandle.lookup = self._handle.lookup;
148+
newHandle.bind = self._handle.bind;
149+
newHandle.send = self._handle.send;
150+
newHandle.owner = self;
151+
152+
// Replace the existing handle by the handle we got from master.
153+
self._handle.close();
154+
self._handle = newHandle;
155+
}
156+
157+
Socket.prototype.bind = function(/*port, address, callback*/) {
146158
var self = this;
147159

148160
self._healthCheck();
@@ -152,8 +164,18 @@ Socket.prototype.bind = function(port, address, callback) {
152164

153165
this._bindState = BIND_STATE_BINDING;
154166

155-
if (typeof callback === 'function')
156-
self.once('listening', callback);
167+
if (typeof arguments[arguments.length - 1] === 'function')
168+
self.once('listening', arguments[arguments.length - 1]);
169+
170+
var UDP = process.binding('udp_wrap').UDP;
171+
if (arguments[0] instanceof UDP) {
172+
replaceHandle(self, arguments[0]);
173+
startListening(self);
174+
return;
175+
}
176+
177+
var port = arguments[0];
178+
var address = arguments[1];
157179

158180
// resolve address first
159181
self._handle.lookup(address, function(err, ip) {
@@ -172,16 +194,7 @@ Socket.prototype.bind = function(port, address, callback) {
172194
// handle has been closed in the mean time.
173195
return handle.close();
174196

175-
// Set up the handle that we got from master.
176-
handle.lookup = self._handle.lookup;
177-
handle.bind = self._handle.bind;
178-
handle.send = self._handle.send;
179-
handle.owner = self;
180-
181-
// Replace the existing handle by the handle we got from master.
182-
self._handle.close();
183-
self._handle = handle;
184-
197+
replaceHandle(self, handle);
185198
startListening(self);
186199
});
187200

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
var dgram = require('dgram');
23+
var fork = require('child_process').fork;
24+
var assert = require('assert');
25+
var common = require('../common');
26+
27+
if (process.argv[2] === 'child') {
28+
var childCollected = 0;
29+
var server;
30+
31+
process.on('message', function removeMe(msg, clusterServer) {
32+
if (msg === 'server') {
33+
server = clusterServer;
34+
35+
server.on('message', function () {
36+
childCollected += 1;
37+
});
38+
39+
} else if (msg === 'stop') {
40+
server.close();
41+
process.send(childCollected);
42+
process.removeListener('message', removeMe);
43+
}
44+
});
45+
46+
} else {
47+
var server = dgram.createSocket('udp4');
48+
var client = dgram.createSocket('udp4');
49+
var child = fork(__filename, ['child']);
50+
var msg = new Buffer('Some bytes');
51+
52+
var parentCollected = 0;
53+
var childCollected = 0;
54+
server.on('message', function (msg, rinfo) {
55+
parentCollected += 1;
56+
});
57+
58+
server.on('listening', function () {
59+
child.send('server', server);
60+
61+
sendMessages();
62+
});
63+
64+
var sendMessages = function () {
65+
var wait = 0;
66+
var send = 0;
67+
var total = 100;
68+
69+
var timer = setInterval(function () {
70+
send += 1;
71+
if (send === total) {
72+
clearInterval(timer);
73+
}
74+
75+
client.send(msg, 0, msg.length, common.PORT, '127.0.0.1', function(err) {
76+
if (err) throw err;
77+
78+
wait += 1;
79+
if (wait === total) {
80+
shutdown();
81+
}
82+
}
83+
);
84+
}, 1);
85+
};
86+
87+
var shutdown = function () {
88+
child.send('stop');
89+
child.once('message', function (collected) {
90+
childCollected = collected;
91+
});
92+
93+
server.close();
94+
client.close();
95+
};
96+
97+
server.bind(common.PORT, '127.0.0.1');
98+
99+
process.once('exit', function () {
100+
assert(childCollected > 0);
101+
assert(parentCollected > 0);
102+
});
103+
}

0 commit comments

Comments
 (0)
This repository has been archived.