Returns a JSON structure detailing the peer's certificate, containing a dictionary
with keys for the certificate `'subject'`, `'issuer'`, `'valid_from'` and `'valid_to'`.
-#### stream.write(data, [encoding])
+#### stream.write(data, [encoding], [callback])
Sends data on the stream. The second parameter specifies the encoding in the
case of a string--it defaults to UTF8 encoding.
buffer. Returns `false` if all or part of the data was queued in user memory.
`'drain'` will be emitted when the buffer is again free.
-#### stream.write(data, [encoding], [fileDescriptor])
+The optional `callback` parameter will be executed when the data is finally
+written out - this may not be immediately.
+
+#### stream.write(data, [encoding], [fileDescriptor], [callback])
For UNIX sockets, it is possible to send a file descriptor through the
stream. Simply add the `fileDescriptor` argument and listen for the `'fd'`
self._writeQueue = [];
self._writeQueueEncoding = [];
self._writeQueueFD = [];
+ self._writeQueueCallbacks = [];
self._writeWatcher = ioWatchers.alloc();
self._writeWatcher.socket = self;
this._writeQueue = [];
this._writeQueueEncoding = [];
this._writeQueueFD = [];
+ this._writeQueueCallbacks = [];
}
// Slow. There is already a write queue, so let's append to it.
this._writeQueueEncoding[last] === encoding) {
// optimization - concat onto last
this._writeQueue[last] += data;
+
+ if (cb) {
+ if (!this._writeQueueCallbacks[last]) {
+ this._writeQueueCallbacks[last] = cb;
+ } else {
+ // awful
+ this._writeQueueCallbacks[last] = function () {
+ this._writeQueueCallbacks[last]();
+ cb();
+ };
+ }
+ }
} else {
this._writeQueue.push(data);
this._writeQueueEncoding.push(encoding);
+ this._writeQueueCallbacks.push(cb);
}
if (fd != undefined) {
// Fast.
// The most common case. There is no write queue. Just push the data
// directly to the socket.
- return this._writeOut(data, encoding, fd);
+ return this._writeOut(data, encoding, fd, cb);
}
};
// 2. Write data to socket. Return true if flushed.
// 3. Slice out remaining
// 4. Unshift remaining onto _writeQueue. Return false.
-Stream.prototype._writeOut = function(data, encoding, fd) {
+Stream.prototype._writeOut = function(data, encoding, fd, cb) {
if (!this.writable) {
throw new Error('Stream is not writable');
}
// Unshift whatever didn't fit onto the buffer
this._writeQueue.unshift(data.slice(charsWritten));
this._writeQueueEncoding.unshift(encoding);
+ this._writeQueueCallbacks.unshift(cb);
this._writeWatcher.start();
queuedData = true;
}
if (queuedData) {
return false;
} else {
+ if (cb) cb();
return true;
}
}
// data should be the next thing to write.
this._writeQueue.unshift(leftOver);
this._writeQueueEncoding.unshift(null);
+ this._writeQueueCallbacks.unshift(cb);
// If didn't successfully write any bytes, enqueue our fd and try again
if (!bytesWritten) {
while (this._writeQueue && this._writeQueue.length) {
var data = this._writeQueue.shift();
var encoding = this._writeQueueEncoding.shift();
+ var cb = this._writeQueueCallbacks.shift();
var fd = this._writeQueueFD.shift();
if (data === END_OF_FILE) {
return true;
}
- var flushed = this._writeOut(data, encoding, fd);
+ var flushed = this._writeOut(data, encoding, fd, cb);
if (!flushed) return false;
}
if (this._writeWatcher) this._writeWatcher.stop();
var net = require('net');
var tcpPort = common.PORT;
+var fooWritten = false;
+var connectHappened = false;
var tcp = net.Server(function(s) {
tcp.close();
process.exit(1);
});
});
-tcp.listen(tcpPort, startClient);
-function startClient() {
+tcp.listen(common.PORT, function () {
var socket = net.Stream();
console.log('Connecting to socket');
socket.connect(tcpPort);
+
socket.on('connect', function() {
console.log('socket connected');
+ connectHappened = true;
});
assert.equal('opening', socket.readyState);
- assert.equal(false, socket.write('foo'));
+ var r = socket.write('foo', function () {
+ fooWritten = true;
+ assert.ok(connectHappened);
+ console.error("foo written");
+ });
+
+ assert.equal(false, r);
socket.end('bar');
assert.equal('opening', socket.readyState);
-}
+});
+
+process.on('exit', function () {
+ assert.ok(connectHappened);
+ assert.ok(fooWritten);
+});
function pingPongTest(port, host) {
var N = 1000;
var count = 0;
+ var sentPongs = 0;
var sent_final_ping = false;
var server = net.createServer({ allowHalfOpen: true }, function(socket) {
assert.equal(true, socket.readable);
assert.equal(true, count <= N);
if (/PING/.exec(data)) {
- socket.write('PONG');
+ socket.write('PONG', function () {
+ sentPongs++;
+ console.error('sent PONG');
+ });
}
});
});
client.addListener('close', function() {
- console.log('client.endd');
+ console.log('client.end');
assert.equal(N + 1, count);
+ assert.equal(N + 1, sentPongs);
assert.equal(true, sent_final_ping);
tests_run += 1;
});