handle.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. var assert = require('assert')
  2. var util = require('util')
  3. var EventEmitter = require('events').EventEmitter
  4. var Buffer = require('buffer').Buffer
  5. var Queue = require('./queue')
  6. // Node.js version
  7. var match = /^v(\d+)\.(\d+)\./.exec(process.version)
  8. var version = match ? Number(match[1]) + Number('0.' + match[2]) : 11
  9. var onreadMode = version >= 11.1 ? 2 : 1
  10. var mode = 'modern'
  11. var setNRead
  12. if (onreadMode === 2) {
  13. var sw = process.binding('stream_wrap')
  14. setNRead = function (nread) {
  15. sw.streamBaseState[sw.kReadBytesOrError] = nread
  16. }
  17. }
  18. function Handle (stream, options) {
  19. EventEmitter.call(this)
  20. this._stream = stream
  21. this._flowing = false
  22. this._reading = false
  23. this._options = options || {}
  24. this.onread = null
  25. // Pending requests
  26. this.pending = new Queue()
  27. }
  28. util.inherits(Handle, EventEmitter)
  29. module.exports = Handle
  30. Handle.mode = mode
  31. Handle.create = function create (stream, options) {
  32. return new Handle(stream, options)
  33. }
  34. Handle.prototype._onread = function _onread (nread, buffer) {
  35. if (onreadMode === 1) {
  36. this.onread(nread, buffer)
  37. } else {
  38. setNRead(nread)
  39. this.onread(buffer)
  40. }
  41. }
  42. Handle.prototype._queueReq = function _queueReq (type, req) {
  43. return this.pending.append(type, req)
  44. }
  45. Handle.prototype._pendingList = function _pendingList () {
  46. var list = []
  47. while (!this.pending.isEmpty()) { list.push(this.pending.first().dequeue()) }
  48. return list
  49. }
  50. Handle.prototype.setStream = function setStream (stream) {
  51. assert(this._stream === null, 'Can\'t set stream two times')
  52. this._stream = stream
  53. this.emit('stream', stream)
  54. }
  55. Handle.prototype.readStart = function readStart () {
  56. this._reading = true
  57. if (!this._stream) {
  58. this.once('stream', this.readStart)
  59. return 0
  60. }
  61. if (!this._flowing) {
  62. this._flowing = true
  63. this._flow()
  64. }
  65. this._stream.resume()
  66. return 0
  67. }
  68. Handle.prototype.readStop = function readStop () {
  69. this._reading = false
  70. if (!this._stream) {
  71. this.once('stream', this.readStop)
  72. return 0
  73. }
  74. this._stream.pause()
  75. return 0
  76. }
  77. var uv = process.binding('uv')
  78. Handle.prototype._flow = function flow () {
  79. var self = this
  80. this._stream.on('data', function (chunk) {
  81. self._onread(chunk.length, chunk)
  82. })
  83. this._stream.on('end', function () {
  84. self._onread(uv.UV_EOF, Buffer.alloc(0))
  85. })
  86. this._stream.on('close', function () {
  87. setImmediate(function () {
  88. if (self._reading) {
  89. self._onread(uv.UV_ECONNRESET, Buffer.alloc(0))
  90. }
  91. })
  92. })
  93. }
  94. Handle.prototype._close = function _close () {
  95. var list = this._pendingList()
  96. var self = this
  97. setImmediate(function () {
  98. for (var i = 0; i < list.length; i++) {
  99. var req = list[i]
  100. req.oncomplete(uv.UV_ECANCELED, self, req)
  101. }
  102. })
  103. this.readStop()
  104. }
  105. Handle.prototype.shutdown = function shutdown (req) {
  106. var wrap = this._queueReq('shutdown', req)
  107. if (!this._stream) {
  108. this.once('stream', function () {
  109. this._shutdown(wrap)
  110. })
  111. return 0
  112. }
  113. return this._shutdown(wrap)
  114. }
  115. Handle.prototype._shutdown = function _shutdown (wrap) {
  116. var self = this
  117. this._stream.end(function () {
  118. var req = wrap.dequeue()
  119. if (!req) { return }
  120. req.oncomplete(0, self, req)
  121. })
  122. return 0
  123. }
  124. Handle.prototype.close = function close (callback) {
  125. this._close()
  126. if (!this._stream) {
  127. this.once('stream', function () {
  128. this.close(callback)
  129. })
  130. return 0
  131. }
  132. if (this._options.close) {
  133. this._options.close(callback)
  134. } else {
  135. process.nextTick(callback)
  136. }
  137. return 0
  138. }
  139. Handle.prototype.writeEnc = function writeEnc (req, data, enc) {
  140. var wrap = this._queueReq('write', req)
  141. if (!this._stream) {
  142. this.once('stream', function () {
  143. this._writeEnc(wrap, req, data, enc)
  144. })
  145. return 0
  146. }
  147. return this._writeEnc(wrap, req, data, enc)
  148. }
  149. Handle.prototype._writeEnc = function _writeEnc (wrap, req, data, enc) {
  150. var self = this
  151. req.async = true
  152. req.bytes = data.length
  153. if (wrap.isEmpty()) {
  154. return 0
  155. }
  156. this._stream.write(data, enc, function () {
  157. var req = wrap.dequeue()
  158. if (!req) { return }
  159. req.oncomplete(0, self, req)
  160. })
  161. return 0
  162. }
  163. /**
  164. * @param {WriteWrap} req
  165. * @param {string[]} chunks
  166. * @param {Boolean} allBuffers
  167. */
  168. Handle.prototype.writev = function _writev (req, chunks, allBuffers) {
  169. while (chunks.length > 0) {
  170. this._stream.write(chunks.shift(), chunks.shift())
  171. }
  172. return 0
  173. }
  174. Handle.prototype.writeBuffer = function writeBuffer (req, data) {
  175. return this.writeEnc(req, data, null)
  176. }
  177. Handle.prototype.writeAsciiString = function writeAsciiString (req, data) {
  178. return this.writeEnc(req, data, 'ascii')
  179. }
  180. Handle.prototype.writeUtf8String = function writeUtf8String (req, data) {
  181. return this.writeEnc(req, data, 'utf8')
  182. }
  183. Handle.prototype.writeUcs2String = function writeUcs2String (req, data) {
  184. return this.writeEnc(req, data, 'ucs2')
  185. }
  186. Handle.prototype.writeBinaryString = function writeBinaryString (req, data) {
  187. return this.writeEnc(req, data, 'binary')
  188. }
  189. Handle.prototype.writeLatin1String = function writeLatin1String (req, data) {
  190. return this.writeEnc(req, data, 'binary')
  191. }
  192. // v0.8
  193. Handle.prototype.getsockname = function getsockname () {
  194. if (this._options.getPeerName) {
  195. return this._options.getPeerName()
  196. }
  197. return null
  198. }
  199. Handle.prototype.getpeername = function getpeername (out) {
  200. var res = this.getsockname()
  201. if (!res) { return -1 }
  202. Object.keys(res).forEach(function (key) {
  203. out[key] = res[key]
  204. })
  205. return 0
  206. }