Skip to content

Commit fa556a1

Browse files
committed
Add callback to socket.write(), fix test-sendfds
1 parent a6d8425 commit fa556a1

File tree

4 files changed

+63
-29
lines changed

4 files changed

+63
-29
lines changed

lib/net.js

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,18 @@ var ioWatchers = new FreeList("iowatcher", 100, function () {
5656

5757

5858
IOWatcher.prototype.ondrain = function () {
59-
assert(this.socket);
60-
var socket = this.socket;
59+
if (this.socket) {
60+
var socket = this.socket;
6161

62-
if (socket.writable || socket.readable) {
63-
require('timers').active(socket);
64-
}
62+
if (socket.writable || socket.readable) {
63+
require('timers').active(socket);
64+
}
6565

66-
socket.emit('drain');
67-
if (socket.ondrain) socket.ondrain();
66+
socket.emit('drain');
67+
if (socket.ondrain) socket.ondrain();
6868

69-
if (socket._eof) socket._shutdown();
69+
if (socket._eof) socket._shutdown();
70+
}
7071
};
7172

7273

@@ -252,12 +253,13 @@ Object.defineProperty(Stream.prototype, 'readyState', {
252253
});
253254

254255

255-
Stream.prototype._appendBucket = function (data, encoding, fd) {
256+
Stream.prototype._appendBucket = function (data, encoding, fd, callback) {
256257
if (data.length != 0) {
257258
// TODO reject empty data.
258259
var newBucket = { data: data };
259260
if (encoding) newBucket.encoding = encoding;
260261
if (fd) newBucket.fd = fd;
262+
if (callback) newBucket.callback = callback;
261263

262264
// TODO properly calculate queueSize
263265

@@ -280,7 +282,7 @@ Stream.prototype._appendBucket = function (data, encoding, fd) {
280282
};
281283

282284

283-
Stream.prototype.write = function (data, encoding, fd) {
285+
Stream.prototype.write = function (data /* encoding, fd, callback */) {
284286
if (this._eof) {
285287
throw new Error('Stream.end() called already; cannot write.');
286288
}
@@ -289,7 +291,29 @@ Stream.prototype.write = function (data, encoding, fd) {
289291
throw new Error('Stream is not writable');
290292
}
291293

292-
var queueSize = this._appendBucket(data, encoding, fd);
294+
// parse the arguments. ugly.
295+
296+
var encoding, fd, callback;
297+
298+
if (arguments[1] === undefined || typeof arguments[1] == 'string') {
299+
encoding = arguments[1];
300+
if (typeof arguments[2] == 'number') {
301+
fd = arguments[2];
302+
callback = arguments[3];
303+
} else {
304+
callback = arguments[2];
305+
}
306+
} else if (typeof arguments[1] == 'number') {
307+
fd = arguments[1];
308+
callback = arguments[2];
309+
} else if (typeof arguments[1] == 'function') {
310+
callback = arguments[1];
311+
} else {
312+
throw new Error("Bad type for second argument");
313+
}
314+
315+
316+
var queueSize = this._appendBucket(data, encoding, fd, callback);
293317

294318
if (this._connecting) return false;
295319

src/node_io_watcher.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static Persistent<String> is_unix_socket_sym;
3838
static Persistent<String> first_bucket_sym;
3939
static Persistent<String> last_bucket_sym;
4040
static Persistent<String> queue_size_sym;
41+
static Persistent<String> callback_sym;
4142

4243

4344
void IOWatcher::Initialize(Handle<Object> target) {
@@ -73,6 +74,7 @@ void IOWatcher::Initialize(Handle<Object> target) {
7374
is_unix_socket_sym = NODE_PSYMBOL("isUnixSocket");
7475
data_sym = NODE_PSYMBOL("data");
7576
encoding_sym = NODE_PSYMBOL("encoding");
77+
callback_sym = NODE_PSYMBOL("callback");
7678

7779

7880
ev_prepare_init(&dumper, IOWatcher::Dump);
@@ -497,6 +499,17 @@ void IOWatcher::Dump() {
497499

498500
written -= bucket_len - offset;
499501

502+
Local<Value> bucket_callback_v = bucket->Get(callback_sym);
503+
if (bucket_callback_v->IsFunction()) {
504+
Local<Function> bucket_callback =
505+
Local<Function>::Cast(bucket_callback_v);
506+
TryCatch try_catch;
507+
bucket_callback->Call(io->handle_, 0, NULL);
508+
if (try_catch.HasCaught()) {
509+
FatalException(try_catch);
510+
}
511+
}
512+
500513
// Offset is now zero
501514
watcher->Set(offset_sym, Integer::NewFromUnsigned(0));
502515
}

test/fixtures/recvfd.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,33 @@ function processData(s) {
2222
// version of our modified object back. Clean up when we're done.
2323
var pipeStream = new net.Stream(fd);
2424

25-
var drainFunc = function() {
25+
pipeStream.resume();
26+
27+
pipeStream.write(JSON.stringify(d) + '\n', function () {
2628
pipeStream.destroy();
2729

2830
if (++numSentMessages == 2) {
2931
s.destroy();
3032
}
31-
};
32-
33-
pipeStream.addListener('drain', drainFunc);
34-
pipeStream.resume();
35-
36-
if (pipeStream.write(JSON.stringify(d) + '\n')) {
37-
drainFunc();
38-
}
33+
});
3934
};
4035

4136
// Create a UNIX socket to the path defined by argv[2] and read a file
4237
// descriptor and misc data from it.
4338
var s = new net.Stream();
39+
4440
s.addListener('fd', function(fd) {
4541
receivedFDs.unshift(fd);
4642
processData(s);
4743
});
44+
4845
s.addListener('data', function(data) {
4946
data.toString('utf8').trim().split('\n').forEach(function(d) {
5047
receivedData.unshift(JSON.parse(d));
5148
});
5249
processData(s);
5350
});
51+
5452
s.connect(process.argv[2]);
5553

5654
// vim:ts=2 sw=2 et

test/simple/test-sendfd.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var logChild = function(d) {
5353

5454
d.split('\n').forEach(function(l) {
5555
if (l.length > 0) {
56-
common.debug('CHILD: ' + l);
56+
console.error('CHILD: ' + l);
5757
}
5858
});
5959
};
@@ -96,19 +96,18 @@ var srv = net.createServer(function(s) {
9696
buf.write(JSON.stringify(DATA) + '\n', 'utf8');
9797

9898
s.write(str, 'utf8', pipeFDs[1]);
99-
if (s.write(buf, undefined, pipeFDs[1])) {
99+
100+
s.write(buf, pipeFDs[1], function () {
101+
console.error("close pipeFDs[1]");
100102
netBinding.close(pipeFDs[1]);
101-
} else {
102-
s.addListener('drain', function() {
103-
netBinding.close(pipeFDs[1]);
104-
});
105-
}
103+
});
106104
});
107105
srv.listen(SOCK_PATH);
108106

109107
// Spawn a child running test/fixtures/recvfd.js
110-
var cp = child_process.spawn(process.argv[0],
111-
[path.join(common.fixturesDir, 'recvfd.js'), SOCK_PATH]);
108+
var cp = child_process.spawn(process.execPath,
109+
[path.join(common.fixturesDir, 'recvfd.js'),
110+
SOCK_PATH]);
112111

113112
cp.stdout.addListener('data', logChild);
114113
cp.stderr.addListener('data', logChild);

0 commit comments

Comments
 (0)