2015-03-26 23:20:27 +01:00
|
|
|
'use strict';
|
|
|
|
|
2015-06-07 00:37:35 +02:00
|
|
|
const assert = require('assert');
|
2015-02-23 21:09:44 +01:00
|
|
|
const util = require('util');
|
|
|
|
const Socket = require('net').Socket;
|
|
|
|
const JSStream = process.binding('js_stream').JSStream;
|
|
|
|
const uv = process.binding('uv');
|
2015-06-07 00:37:35 +02:00
|
|
|
const debug = util.debuglog('stream_wrap');
|
2017-06-05 08:54:02 +02:00
|
|
|
const errors = require('internal/errors');
|
2015-02-23 21:09:44 +01:00
|
|
|
|
|
|
|
function StreamWrap(stream) {
|
2015-06-07 00:37:35 +02:00
|
|
|
const handle = new JSStream();
|
2015-02-23 21:09:44 +01:00
|
|
|
|
|
|
|
this.stream = stream;
|
|
|
|
|
2015-06-07 00:37:35 +02:00
|
|
|
this._list = null;
|
|
|
|
|
|
|
|
const self = this;
|
2015-02-23 21:09:44 +01:00
|
|
|
handle.close = function(cb) {
|
2015-06-07 00:37:35 +02:00
|
|
|
debug('close');
|
|
|
|
self.doClose(cb);
|
2015-02-23 21:09:44 +01:00
|
|
|
};
|
|
|
|
handle.isAlive = function() {
|
|
|
|
return self.isAlive();
|
|
|
|
};
|
|
|
|
handle.isClosing = function() {
|
|
|
|
return self.isClosing();
|
|
|
|
};
|
|
|
|
handle.onreadstart = function() {
|
|
|
|
return self.readStart();
|
|
|
|
};
|
|
|
|
handle.onreadstop = function() {
|
|
|
|
return self.readStop();
|
|
|
|
};
|
|
|
|
handle.onshutdown = function(req) {
|
2015-06-07 00:37:35 +02:00
|
|
|
return self.doShutdown(req);
|
2015-02-23 21:09:44 +01:00
|
|
|
};
|
|
|
|
handle.onwrite = function(req, bufs) {
|
2015-06-07 00:37:35 +02:00
|
|
|
return self.doWrite(req, bufs);
|
2015-02-23 21:09:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
this.stream.pause();
|
2015-12-05 22:03:30 +01:00
|
|
|
this.stream.on('error', function onerror(err) {
|
2015-06-07 00:37:35 +02:00
|
|
|
self.emit('error', err);
|
|
|
|
});
|
2015-12-05 22:03:30 +01:00
|
|
|
this.stream.on('data', function ondata(chunk) {
|
2017-06-22 02:38:41 +02:00
|
|
|
if (typeof chunk === 'string' || this._readableState.objectMode === true) {
|
2015-12-05 22:03:30 +01:00
|
|
|
// Make sure that no further `data` events will happen
|
|
|
|
this.pause();
|
|
|
|
this.removeListener('data', ondata);
|
|
|
|
|
2017-06-22 02:38:41 +02:00
|
|
|
self.emit('error', new errors.Error('ERR_STREAM_WRAP'));
|
2015-12-05 22:03:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-07 00:37:35 +02:00
|
|
|
debug('data', chunk.length);
|
|
|
|
if (self._handle)
|
|
|
|
self._handle.readBuffer(chunk);
|
2015-02-23 21:09:44 +01:00
|
|
|
});
|
2015-12-05 22:03:30 +01:00
|
|
|
this.stream.once('end', function onend() {
|
2015-06-07 00:37:35 +02:00
|
|
|
debug('end');
|
|
|
|
if (self._handle)
|
|
|
|
self._handle.emitEOF();
|
2015-02-23 21:09:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
Socket.call(this, {
|
|
|
|
handle: handle
|
|
|
|
});
|
|
|
|
}
|
|
|
|
util.inherits(StreamWrap, Socket);
|
|
|
|
module.exports = StreamWrap;
|
|
|
|
|
|
|
|
// require('_stream_wrap').StreamWrap
|
|
|
|
StreamWrap.StreamWrap = StreamWrap;
|
|
|
|
|
|
|
|
StreamWrap.prototype.isAlive = function isAlive() {
|
2015-06-07 00:37:35 +02:00
|
|
|
return true;
|
2015-02-23 21:09:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
StreamWrap.prototype.isClosing = function isClosing() {
|
2015-06-07 00:37:35 +02:00
|
|
|
return !this.readable || !this.writable;
|
2015-02-23 21:09:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
StreamWrap.prototype.readStart = function readStart() {
|
|
|
|
this.stream.resume();
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
StreamWrap.prototype.readStop = function readStop() {
|
|
|
|
this.stream.pause();
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2015-06-07 00:37:35 +02:00
|
|
|
StreamWrap.prototype.doShutdown = function doShutdown(req) {
|
|
|
|
const self = this;
|
|
|
|
const handle = this._handle;
|
|
|
|
const item = this._enqueue('shutdown', req);
|
2015-02-23 21:09:44 +01:00
|
|
|
|
|
|
|
this.stream.end(function() {
|
|
|
|
// Ensure that write was dispatched
|
|
|
|
setImmediate(function() {
|
2015-06-07 00:37:35 +02:00
|
|
|
if (!self._dequeue(item))
|
|
|
|
return;
|
|
|
|
|
|
|
|
handle.finishShutdown(req, 0);
|
2015-02-23 21:09:44 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2015-06-07 00:37:35 +02:00
|
|
|
StreamWrap.prototype.doWrite = function doWrite(req, bufs) {
|
|
|
|
const self = this;
|
|
|
|
const handle = self._handle;
|
|
|
|
|
2015-02-23 21:09:44 +01:00
|
|
|
var pending = bufs.length;
|
2015-06-07 00:37:35 +02:00
|
|
|
|
|
|
|
// Queue the request to be able to cancel it
|
|
|
|
const item = self._enqueue('write', req);
|
2015-02-23 21:09:44 +01:00
|
|
|
|
|
|
|
self.stream.cork();
|
2017-02-26 07:16:57 +01:00
|
|
|
for (var n = 0; n < bufs.length; n++)
|
|
|
|
self.stream.write(bufs[n], done);
|
2015-02-23 21:09:44 +01:00
|
|
|
self.stream.uncork();
|
|
|
|
|
|
|
|
function done(err) {
|
|
|
|
if (!err && --pending !== 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Ensure that this is called once in case of error
|
|
|
|
pending = 0;
|
|
|
|
|
|
|
|
// Ensure that write was dispatched
|
|
|
|
setImmediate(function() {
|
2015-06-07 00:37:35 +02:00
|
|
|
// Do not invoke callback twice
|
|
|
|
if (!self._dequeue(item))
|
|
|
|
return;
|
|
|
|
|
2015-02-23 21:09:44 +01:00
|
|
|
var errCode = 0;
|
|
|
|
if (err) {
|
|
|
|
if (err.code && uv['UV_' + err.code])
|
|
|
|
errCode = uv['UV_' + err.code];
|
|
|
|
else
|
|
|
|
errCode = uv.UV_EPIPE;
|
|
|
|
}
|
|
|
|
|
2015-06-07 00:37:35 +02:00
|
|
|
handle.doAfterWrite(req);
|
|
|
|
handle.finishWrite(req, errCode);
|
2015-02-23 21:09:44 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
};
|
2015-06-07 00:37:35 +02:00
|
|
|
|
|
|
|
function QueueItem(type, req) {
|
|
|
|
this.type = type;
|
|
|
|
this.req = req;
|
|
|
|
this.prev = this;
|
|
|
|
this.next = this;
|
|
|
|
}
|
|
|
|
|
2016-10-16 00:02:43 +02:00
|
|
|
StreamWrap.prototype._enqueue = function _enqueue(type, req) {
|
2015-06-07 00:37:35 +02:00
|
|
|
const item = new QueueItem(type, req);
|
|
|
|
if (this._list === null) {
|
|
|
|
this._list = item;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
item.next = this._list.next;
|
|
|
|
item.prev = this._list;
|
|
|
|
item.next.prev = item;
|
|
|
|
item.prev.next = item;
|
|
|
|
|
|
|
|
return item;
|
|
|
|
};
|
|
|
|
|
2016-10-16 00:02:43 +02:00
|
|
|
StreamWrap.prototype._dequeue = function _dequeue(item) {
|
2015-06-07 00:37:35 +02:00
|
|
|
assert(item instanceof QueueItem);
|
|
|
|
|
|
|
|
var next = item.next;
|
|
|
|
var prev = item.prev;
|
|
|
|
|
|
|
|
if (next === null && prev === null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
item.next = null;
|
|
|
|
item.prev = null;
|
|
|
|
|
|
|
|
if (next === item) {
|
|
|
|
prev = null;
|
|
|
|
next = null;
|
|
|
|
} else {
|
|
|
|
prev.next = next;
|
|
|
|
next.prev = prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._list === item)
|
|
|
|
this._list = next;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
StreamWrap.prototype.doClose = function doClose(cb) {
|
|
|
|
const self = this;
|
|
|
|
const handle = self._handle;
|
|
|
|
|
|
|
|
setImmediate(function() {
|
|
|
|
while (self._list !== null) {
|
|
|
|
const item = self._list;
|
|
|
|
const req = item.req;
|
|
|
|
self._dequeue(item);
|
|
|
|
|
|
|
|
const errCode = uv.UV_ECANCELED;
|
|
|
|
if (item.type === 'write') {
|
|
|
|
handle.doAfterWrite(req);
|
|
|
|
handle.finishWrite(req, errCode);
|
|
|
|
} else if (item.type === 'shutdown') {
|
|
|
|
handle.finishShutdown(req, errCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be already set by net.js
|
|
|
|
assert(self._handle === null);
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
};
|