|
| 1 | +const util = require('util'); |
| 2 | +const Socket = require('net').Socket; |
| 3 | +const JSStream = process.binding('js_stream').JSStream; |
| 4 | +const uv = process.binding('uv'); |
| 5 | + |
| 6 | +function StreamWrap(stream) { |
| 7 | + var handle = new JSStream(); |
| 8 | + |
| 9 | + this.stream = stream; |
| 10 | + |
| 11 | + var self = this; |
| 12 | + handle.close = function(cb) { |
| 13 | + cb(); |
| 14 | + }; |
| 15 | + handle.isAlive = function() { |
| 16 | + return self.isAlive(); |
| 17 | + }; |
| 18 | + handle.isClosing = function() { |
| 19 | + return self.isClosing(); |
| 20 | + }; |
| 21 | + handle.onreadstart = function() { |
| 22 | + return self.readStart(); |
| 23 | + }; |
| 24 | + handle.onreadstop = function() { |
| 25 | + return self.readStop(); |
| 26 | + }; |
| 27 | + handle.onshutdown = function(req) { |
| 28 | + return self.shutdown(req); |
| 29 | + }; |
| 30 | + handle.onwrite = function(req, bufs) { |
| 31 | + return self.write(req, bufs); |
| 32 | + }; |
| 33 | + |
| 34 | + this.stream.pause(); |
| 35 | + this.stream.on('data', function(chunk) { |
| 36 | + self._handle.readBuffer(chunk); |
| 37 | + }); |
| 38 | + this.stream.once('end', function() { |
| 39 | + self._handle.emitEOF(); |
| 40 | + }); |
| 41 | + this.stream.on('error', function(err) { |
| 42 | + self.emit('error', err); |
| 43 | + }); |
| 44 | + |
| 45 | + Socket.call(this, { |
| 46 | + handle: handle |
| 47 | + }); |
| 48 | +} |
| 49 | +util.inherits(StreamWrap, Socket); |
| 50 | +module.exports = StreamWrap; |
| 51 | + |
| 52 | +// require('_stream_wrap').StreamWrap |
| 53 | +StreamWrap.StreamWrap = StreamWrap; |
| 54 | + |
| 55 | +StreamWrap.prototype.isAlive = function isAlive() { |
| 56 | + return this.readable && this.writable; |
| 57 | +}; |
| 58 | + |
| 59 | +StreamWrap.prototype.isClosing = function isClosing() { |
| 60 | + return !this.isAlive(); |
| 61 | +}; |
| 62 | + |
| 63 | +StreamWrap.prototype.readStart = function readStart() { |
| 64 | + this.stream.resume(); |
| 65 | + return 0; |
| 66 | +}; |
| 67 | + |
| 68 | +StreamWrap.prototype.readStop = function readStop() { |
| 69 | + this.stream.pause(); |
| 70 | + return 0; |
| 71 | +}; |
| 72 | + |
| 73 | +StreamWrap.prototype.shutdown = function shutdown(req) { |
| 74 | + var self = this; |
| 75 | + |
| 76 | + this.stream.end(function() { |
| 77 | + // Ensure that write was dispatched |
| 78 | + setImmediate(function() { |
| 79 | + self._handle.finishShutdown(req, 0); |
| 80 | + }); |
| 81 | + }); |
| 82 | + return 0; |
| 83 | +}; |
| 84 | + |
| 85 | +StreamWrap.prototype.write = function write(req, bufs) { |
| 86 | + var pending = bufs.length; |
| 87 | + var self = this; |
| 88 | + |
| 89 | + self.stream.cork(); |
| 90 | + bufs.forEach(function(buf) { |
| 91 | + self.stream.write(buf, done); |
| 92 | + }); |
| 93 | + self.stream.uncork(); |
| 94 | + |
| 95 | + function done(err) { |
| 96 | + if (!err && --pending !== 0) |
| 97 | + return; |
| 98 | + |
| 99 | + // Ensure that this is called once in case of error |
| 100 | + pending = 0; |
| 101 | + |
| 102 | + // Ensure that write was dispatched |
| 103 | + setImmediate(function() { |
| 104 | + var errCode = 0; |
| 105 | + if (err) { |
| 106 | + if (err.code && uv['UV_' + err.code]) |
| 107 | + errCode = uv['UV_' + err.code]; |
| 108 | + else |
| 109 | + errCode = uv.UV_EPIPE; |
| 110 | + } |
| 111 | + |
| 112 | + self._handle.doAfterWrite(req); |
| 113 | + self._handle.finishWrite(req, errCode); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + return 0; |
| 118 | +}; |
0 commit comments