handle.js
5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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 match = /^v(\d+)\.(\d+)\./.exec(process.version)
var version = match ? Number(match[1]) + Number('0.' + match[2]) : 11
var onreadMode = version >= 11.1 ? 2 : 1
var mode = 'modern'
var setNRead
if (onreadMode === 2) {
var sw = process.binding('stream_wrap')
setNRead = function (nread) {
sw.streamBaseState[sw.kReadBytesOrError] = nread
}
}
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()
}
util.inherits(Handle, EventEmitter)
module.exports = Handle
Handle.mode = mode
Handle.create = function create (stream, options) {
return new Handle(stream, options)
}
Handle.prototype._onread = function _onread (nread, buffer) {
if (onreadMode === 1) {
this.onread(nread, buffer)
} else {
setNRead(nread)
this.onread(buffer)
}
}
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
}
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, Buffer.alloc(0))
})
this._stream.on('close', function () {
setImmediate(function () {
if (self._reading) {
self._onread(uv.UV_ECONNRESET, Buffer.alloc(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()
}
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
}
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
}
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
}
/**
* @param {WriteWrap} req
* @param {string[]} chunks
* @param {Boolean} allBuffers
*/
Handle.prototype.writev = function _writev (req, chunks, allBuffers) {
while (chunks.length > 0) {
this._stream.write(chunks.shift(), chunks.shift())
}
return 0
}
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
}
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
}