123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- var assert = require('assert');
- var util = require('util');
- var EventEmitter = require('events').EventEmitter;
- var Buffer = require('buffer').Buffer;
- var Queue = require('./queue');
- // Node.js version
- var mode = /^v0\.8\./.test(process.version) ? 'rusty' :
- /^v0\.(9|10)\./.test(process.version) ? 'old' :
- 'modern';
- function Handle(stream, options) {
- EventEmitter.call(this);
- this._stream = stream;
- this._flowing = false;
- this._reading = false;
- this._options = options || {};
- this.onread = null;
- // Pending requests
- this.pending = new Queue();
- // Start handle once `onread` is set
- if (mode === 'rusty') {
- var self = this;
- Object.defineProperty(this, 'onread', {
- set: function(value) {
- Object.defineProperty(self, 'onread', {
- value: value
- });
- process.nextTick(function() {
- self.readStart();
- });
- }
- });
- }
- // NOTE: v0.8 has some odd .pause()/.resume() semantics in http.js
- if (mode === 'rusty')
- this.writeQueueSize = 0;
- else if (mode !== 'modern')
- this.writeQueueSize = 1;
- if (mode === 'rusty') {
- if (this._stream)
- this._rustyInit();
- else
- this.once('stream', this._rustyInit);
- }
- }
- util.inherits(Handle, EventEmitter);
- module.exports = Handle;
- Handle.mode = mode;
- Handle.create = function create(stream, options) {
- return new Handle(stream, options);
- };
- Handle.prototype._queueReq = function _queueReq(type, req) {
- return this.pending.append(type, req);
- };
- Handle.prototype._pendingList = function _pendingList() {
- var list = [];
- while (!this.pending.isEmpty())
- list.push(this.pending.first().dequeue());
- return list;
- };
- Handle.prototype.setStream = function setStream(stream) {
- assert(this._stream === null, 'Can\'t set stream two times');
- this._stream = stream;
- this.emit('stream', stream);
- };
- Handle.prototype.readStart = function readStart() {
- this._reading = true;
- if (!this._stream) {
- this.once('stream', this.readStart);
- return 0;
- }
- if (!this._flowing) {
- this._flowing = true;
- this._flow();
- }
- this._stream.resume();
- return 0;
- };
- Handle.prototype.readStop = function readStop() {
- this._reading = false;
- if (!this._stream) {
- this.once('stream', this.readStop);
- return 0;
- }
- this._stream.pause();
- return 0;
- };
- if (mode === 'modern') {
- var uv = process.binding('uv');
- Handle.prototype._flow = function flow() {
- var self = this;
- this._stream.on('data', function(chunk) {
- self.onread(chunk.length, chunk);
- });
- this._stream.on('end', function() {
- self.onread(uv.UV_EOF, new Buffer(0));
- });
- this._stream.on('close', function() {
- setImmediate(function() {
- if (self._reading)
- self.onread(uv.UV_ECONNRESET, new Buffer(0));
- });
- });
- };
- Handle.prototype._close = function _close() {
- var list = this._pendingList();
- var self = this;
- setImmediate(function() {
- for (var i = 0; i < list.length; i++) {
- var req = list[i];
- req.oncomplete(uv.UV_ECANCELED, self, req);
- }
- });
- this.readStop();
- };
- } else if (mode === 'old') {
- Handle.prototype._flow = function flow() {
- var self = this;
- this._stream.on('data', function(chunk) {
- self.onread(chunk, 0, chunk.length);
- });
- this._stream.on('end', function() {
- var errno = process._errno;
- process._errno = 'EOF';
- self.onread(null, 0, 0);
- if (process._errno === 'EOF')
- process._errno = errno;
- });
- this._stream.on('close', function() {
- setImmediate(function() {
- if (!self._reading)
- return;
- var errno = process._errno;
- process._errno = 'ECONNRESET';
- self.onread(null, 0, 0);
- if (process._errno === 'ECONNRESET')
- process._errno = errno;
- });
- });
- };
- Handle.prototype._close = function _close() {
- var list = this._pendingList();
- var self = this;
- setImmediate(function() {
- for (var i = 0; i < list.length; i++) {
- process._errno = 'CANCELED';
- var req = list[i];
- req.oncomplete(-1, self, req);
- }
- });
- this.readStop();
- };
- } else {
- Handle.prototype._rustyInit = function _rustyInit() {
- var self = this;
- this._stream.on('close', function() {
- process.nextTick(function() {
- if (!self._reading)
- return;
- var errno = global.errno;
- global.errno = 'ECONNRESET';
- self.onread(null, 0, 0);
- if (global.errno === 'ECONNRESET')
- global.errno = errno;
- });
- });
- };
- Handle.prototype._flow = function flow() {
- var self = this;
- this._stream.on('data', function(chunk) {
- self.onread(chunk, 0, chunk.length);
- });
- this._stream.on('end', function() {
- var errno = global.errno;
- global.errno = 'EOF';
- self.onread(null, 0, 0);
- if (global.errno === 'EOF')
- global.errno = errno;
- });
- };
- Handle.prototype._close = function _close() {
- var list = this._pendingList();
- var self = this;
- process.nextTick(function() {
- for (var i = 0; i < list.length; i++) {
- var req = list[i];
- global.errno = 'CANCELED';
- req.oncomplete(-1, self, req);
- }
- });
- this.readStop();
- };
- }
- if (mode === 'modern') {
- Handle.prototype.shutdown = function shutdown(req) {
- var wrap = this._queueReq('shutdown', req);
- if (!this._stream) {
- this.once('stream', function() {
- this._shutdown(wrap);
- });
- return 0;
- }
- return this._shutdown(wrap);
- };
- Handle.prototype._shutdown = function _shutdown(wrap) {
- var self = this;
- this._stream.end(function() {
- var req = wrap.dequeue();
- if (!req)
- return;
- req.oncomplete(0, self, req);
- });
- return 0;
- };
- } else {
- Handle.prototype.shutdown = function shutdown(req) {
- if (!req)
- req = {};
- var wrap = this._queueReq('shutdown', req);
- if (!this._stream) {
- this.once('stream', function() {
- this._shutdown(wrap);
- });
- return req;
- }
- this._shutdown(wrap);
- return req;
- };
- Handle.prototype._shutdown = function _shutdown(wrap) {
- var self = this;
- this._stream.end(function() {
- var req = wrap.dequeue();
- if (!req)
- return;
- req.oncomplete(0, self, req);
- });
- };
- }
- if (mode !== 'rusty') {
- Handle.prototype.close = function close(callback) {
- this._close();
- if (!this._stream) {
- this.once('stream', function() {
- this.close(callback);
- });
- return 0;
- }
- if (this._options.close)
- this._options.close(callback);
- else
- process.nextTick(callback);
- return 0;
- };
- } else {
- Handle.prototype.close = function close() {
- this._close();
- if (!this._stream)
- this.once('stream', this.close);
- else if (this._options.close)
- this._options.close(function() {});
- return 0;
- };
- }
- if (mode === 'modern') {
- Handle.prototype.writeEnc = function writeEnc(req, data, enc) {
- var wrap = this._queueReq('write', req);
- if (!this._stream) {
- this.once('stream', function() {
- this._writeEnc(wrap, req, data, enc);
- });
- return 0;
- }
- return this._writeEnc(wrap, req, data, enc);
- };
- Handle.prototype._writeEnc = function _writeEnc(wrap, req, data, enc) {
- var self = this;
- req.async = true;
- req.bytes = data.length;
- if (wrap.isEmpty())
- return 0;
- this._stream.write(data, enc, function() {
- var req = wrap.dequeue();
- if (!req)
- return;
- req.oncomplete(0, self, req);
- });
- return 0;
- };
- } else {
- Handle.prototype.writeEnc = function writeEnc(data, ignored, enc, req) {
- if (!req)
- req = { bytes: data.length };
- var wrap = this._queueReq('write', req);
- if (!this._stream) {
- this.once('stream', function() {
- this._writeEnc(data, ignored, enc, wrap);
- });
- return req;
- }
- this._writeEnc(data, ignored, enc, wrap);
- return req;
- };
- Handle.prototype._writeEnc = function _writeEnc(data, ignored, enc, wrap) {
- var self = this;
- var buffer = new Buffer(data, enc);
- if (wrap.isEmpty())
- return;
- this._stream.write(buffer, function() {
- var req = wrap.dequeue();
- if (!req)
- return;
- req.oncomplete(0, self, req);
- });
- };
- }
- Handle.prototype.writeBuffer = function writeBuffer(req, data) {
- return this.writeEnc(req, data, null);
- };
- Handle.prototype.writeAsciiString = function writeAsciiString(req, data) {
- return this.writeEnc(req, data, 'ascii');
- };
- Handle.prototype.writeUtf8String = function writeUtf8String(req, data) {
- return this.writeEnc(req, data, 'utf8');
- };
- Handle.prototype.writeUcs2String = function writeUcs2String(req, data) {
- return this.writeEnc(req, data, 'ucs2');
- };
- Handle.prototype.writeBinaryString = function writeBinaryString(req, data) {
- return this.writeEnc(req, data, 'binary');
- };
- Handle.prototype.writeLatin1String = function writeLatin1String(req, data) {
- return this.writeEnc(req, data, 'binary');
- };
- // v0.8
- Handle.prototype.getsockname = function getsockname() {
- if (this._options.getPeerName)
- return this._options.getPeerName();
- return null;
- };
- if (mode === 'modern') {
- Handle.prototype.getpeername = function getpeername(out) {
- var res = this.getsockname();
- if (!res)
- return -1;
- Object.keys(res).forEach(function(key) {
- out[key] = res[key];
- });
- return 0;
- };
- } else {
- // v0.10
- Handle.prototype.getpeername = function getpeername() {
- return this.getsockname();
- };
- }
|