| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c7268c4 commit f0be053
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -105,7 +105,10 @@ const { | |||
| 105 | 105 | const { | |
| 106 | 106 | createWriteWrap, | |
| 107 | 107 | writeGeneric, | |
| 108 | - writevGeneric | ||
| 108 | + writevGeneric, | ||
| 109 | + onStreamRead, | ||
| 110 | + kMaybeDestroy, | ||
| 111 | + kUpdateTimer | ||
| 109 | 112 | } = require('internal/stream_base_commons'); | |
| 110 | 113 | const { | |
| 111 | 114 | kTimeout, | |
@@ -142,7 +145,6 @@ const kHandle = Symbol('handle'); | |||
| 142 | 145 | const kID = Symbol('id'); | |
| 143 | 146 | const kInit = Symbol('init'); | |
| 144 | 147 | const kInfoHeaders = Symbol('sent-info-headers'); | |
| 145 | - const kMaybeDestroy = Symbol('maybe-destroy'); | ||
| 146 | 148 | const kLocalSettings = Symbol('local-settings'); | |
| 147 | 149 | const kOptions = Symbol('options'); | |
| 148 | 150 | const kOwner = owner_symbol; | |
@@ -156,7 +158,6 @@ const kServer = Symbol('server'); | |||
| 156 | 158 | const kSession = Symbol('session'); | |
| 157 | 159 | const kState = Symbol('state'); | |
| 158 | 160 | const kType = Symbol('type'); | |
| 159 | - const kUpdateTimer = Symbol('update-timer'); | ||
| 160 | 161 | const kWriteGeneric = Symbol('write-generic'); | |
| 161 | 162 | ||
| 162 | 163 | const kDefaultSocketTimeout = 2 * 60 * 1000; | |
@@ -374,36 +375,6 @@ function onStreamClose(code) { | |||
| 374 | 375 | } | |
| 375 | 376 | } | |
| 376 | 377 | ||
| 377 | - // Receives a chunk of data for a given stream and forwards it on | ||
| 378 | - // to the Http2Stream Duplex for processing. | ||
| 379 | - function onStreamRead(nread, buf) { | ||
| 380 | - const stream = this[kOwner]; | ||
| 381 | - if (nread >= 0 && !stream.destroyed) { | ||
| 382 | - debug(`Http2Stream ${stream[kID]} [Http2Session ` + | ||
| 383 | - `${sessionName(stream[kSession][kType])}]: receiving data chunk ` + | ||
| 384 | - `of size ${nread}`); | ||
| 385 | - stream[kUpdateTimer](); | ||
| 386 | - if (!stream.push(buf)) { | ||
| 387 | - if (!stream.destroyed) // we have to check a second time | ||
| 388 | - this.readStop(); | ||
| 389 | - } | ||
| 390 | - return; | ||
| 391 | - } | ||
| 392 | - | ||
| 393 | - // Last chunk was received. End the readable side. | ||
| 394 | - debug(`Http2Stream ${stream[kID]} [Http2Session ` + | ||
| 395 | - `${sessionName(stream[kSession][kType])}]: ending readable.`); | ||
| 396 | - | ||
| 397 | - // defer this until we actually emit end | ||
| 398 | - if (!stream.readable) { | ||
| 399 | - stream[kMaybeDestroy](); | ||
| 400 | - } else { | ||
| 401 | - stream.on('end', stream[kMaybeDestroy]); | ||
| 402 | - stream.push(null); | ||
| 403 | - stream.read(0); | ||
| 404 | - } | ||
| 405 | - } | ||
| 406 | - | ||
| 407 | 378 | // Called when the remote peer settings have been updated. | |
| 408 | 379 | // Resets the cached settings. | |
| 409 | 380 | function onSettings() { | |
@@ -2145,6 +2116,7 @@ function afterOpen(session, options, headers, streamOptions, err, fd) { | |||
| 2145 | 2116 | class ServerHttp2Stream extends Http2Stream { | |
| 2146 | 2117 | constructor(session, handle, id, options, headers) { | |
| 2147 | 2118 | super(session, options); | |
| 2119 | + handle.owner = this; | ||
| 2148 | 2120 | this[kInit](id, handle); | |
| 2149 | 2121 | this[kProtocol] = headers[HTTP2_HEADER_SCHEME]; | |
| 2150 | 2122 | this[kAuthority] = headers[HTTP2_HEADER_AUTHORITY]; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,10 +1,13 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | 3 | const { Buffer } = require('buffer'); | |
| 4 | - const errors = require('internal/errors'); | ||
| 5 | 4 | const { WriteWrap } = process.binding('stream_wrap'); | |
| 5 | + const { UV_EOF } = process.binding('uv'); | ||
| 6 | + const { errnoException } = require('internal/errors'); | ||
| 7 | + const { owner_symbol } = require('internal/async_hooks').symbols; | ||
| 6 | 8 | ||
| 7 | - const errnoException = errors.errnoException; | ||
| 9 | + const kMaybeDestroy = Symbol('kMaybeDestroy'); | ||
| 10 | + const kUpdateTimer = Symbol('kUpdateTimer'); | ||
| 8 | 11 | ||
| 9 | 12 | function handleWriteReq(req, data, encoding) { | |
| 10 | 13 | const { handle } = req; | |
@@ -81,8 +84,54 @@ function afterWriteDispatched(self, req, err, cb) { | |||
| 81 | 84 | } | |
| 82 | 85 | } | |
| 83 | 86 | ||
| 87 | + function onStreamRead(nread, buf) { | ||
| 88 | + const handle = this; | ||
| 89 | + const stream = this[owner_symbol]; | ||
| 90 | + | ||
| 91 | + stream[kUpdateTimer](); | ||
| 92 | + | ||
| 93 | + if (nread > 0 && !stream.destroyed) { | ||
| 94 | + if (!stream.push(buf)) { | ||
| 95 | + handle.reading = false; | ||
| 96 | + if (!stream.destroyed) { | ||
| 97 | + const err = handle.readStop(); | ||
| 98 | + if (err) | ||
| 99 | + stream.destroy(errnoException(err, 'read')); | ||
| 100 | + } | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + return; | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + if (nread === 0) { | ||
| 107 | + return; | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + if (nread !== UV_EOF) { | ||
| 111 | + return stream.destroy(errnoException(nread, 'read')); | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + // defer this until we actually emit end | ||
| 115 | + if (stream._readableState.endEmitted) { | ||
| 116 | + if (stream[kMaybeDestroy]) | ||
| 117 | + stream[kMaybeDestroy](); | ||
| 118 | + } else { | ||
| 119 | + if (stream[kMaybeDestroy]) | ||
| 120 | + stream.on('end', stream[kMaybeDestroy]); | ||
| 121 | + | ||
| 122 | + // push a null to signal the end of data. | ||
| 123 | + // Do it before `maybeDestroy` for correct order of events: | ||
| 124 | + // `end` -> `close` | ||
| 125 | + stream.push(null); | ||
| 126 | + stream.read(0); | ||
| 127 | + } | ||
| 128 | + } | ||
| 129 | + | ||
| 84 | 130 | module.exports = { | |
| 85 | 131 | createWriteWrap, | |
| 86 | 132 | writevGeneric, | |
| 87 | - writeGeneric | ||
| 133 | + writeGeneric, | ||
| 134 | + onStreamRead, | ||
| 135 | + kMaybeDestroy, | ||
| 136 | + kUpdateTimer, | ||
| 88 | 137 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,8 +36,7 @@ const { | |||
| 36 | 36 | const assert = require('assert'); | |
| 37 | 37 | const { | |
| 38 | 38 | UV_EADDRINUSE, | |
| 39 | - UV_EINVAL, | ||
| 40 | - UV_EOF | ||
| 39 | + UV_EINVAL | ||
| 41 | 40 | } = process.binding('uv'); | |
| 42 | 41 | ||
| 43 | 42 | const { Buffer } = require('buffer'); | |
@@ -61,7 +60,9 @@ const { | |||
| 61 | 60 | const { | |
| 62 | 61 | createWriteWrap, | |
| 63 | 62 | writevGeneric, | |
| 64 | - writeGeneric | ||
| 63 | + writeGeneric, | ||
| 64 | + onStreamRead, | ||
| 65 | + kUpdateTimer | ||
| 65 | 66 | } = require('internal/stream_base_commons'); | |
| 66 | 67 | const errors = require('internal/errors'); | |
| 67 | 68 | const { | |
@@ -208,7 +209,7 @@ function initSocketHandle(self) { | |||
| 208 | 209 | // Handle creation may be deferred to bind() or connect() time. | |
| 209 | 210 | if (self._handle) { | |
| 210 | 211 | self._handle[owner_symbol] = self; | |
| 211 | - self._handle.onread = onread; | ||
| 212 | + self._handle.onread = onStreamRead; | ||
| 212 | 213 | self[async_id_symbol] = getNewAsyncId(self._handle); | |
| 213 | 214 | } | |
| 214 | 215 | } | |
@@ -514,6 +515,12 @@ Object.defineProperty(Socket.prototype, 'bufferSize', { | |||
| 514 | 515 | } | |
| 515 | 516 | }); | |
| 516 | 517 | ||
| 518 | + Object.defineProperty(Socket.prototype, kUpdateTimer, { | ||
| 519 | + get: function() { | ||
| 520 | + return this._unrefTimer; | ||
| 521 | + } | ||
| 522 | + }); | ||
| 523 | + | ||
| 517 | 524 | ||
| 518 | 525 | // Just call handle.readStart until we have enough in the buffer | |
| 519 | 526 | Socket.prototype._read = function(n) { | |
@@ -615,61 +622,6 @@ Socket.prototype._destroy = function(exception, cb) { | |||
| 615 | 622 | } | |
| 616 | 623 | }; | |
| 617 | 624 | ||
| 618 | - | ||
| 619 | - // This function is called whenever the handle gets a | ||
| 620 | - // buffer, or when there's an error reading. | ||
| 621 | - function onread(nread, buffer) { | ||
| 622 | - var handle = this; | ||
| 623 | - var self = handle[owner_symbol]; | ||
| 624 | - assert(handle === self._handle, 'handle != self._handle'); | ||
| 625 | - | ||
| 626 | - self._unrefTimer(); | ||
| 627 | - | ||
| 628 | - debug('onread', nread); | ||
| 629 | - | ||
| 630 | - if (nread > 0) { | ||
| 631 | - debug('got data'); | ||
| 632 | - | ||
| 633 | - // read success. | ||
| 634 | - // In theory (and in practice) calling readStop right now | ||
| 635 | - // will prevent this from being called again until _read() gets | ||
| 636 | - // called again. | ||
| 637 | - | ||
| 638 | - // Optimization: emit the original buffer with end points | ||
| 639 | - var ret = self.push(buffer); | ||
| 640 | - | ||
| 641 | - if (handle.reading && !ret) { | ||
| 642 | - handle.reading = false; | ||
| 643 | - debug('readStop'); | ||
| 644 | - var err = handle.readStop(); | ||
| 645 | - if (err) | ||
| 646 | - self.destroy(errnoException(err, 'read')); | ||
| 647 | - } | ||
| 648 | - return; | ||
| 649 | - } | ||
| 650 | - | ||
| 651 | - // if we didn't get any bytes, that doesn't necessarily mean EOF. | ||
| 652 | - // wait for the next one. | ||
| 653 | - if (nread === 0) { | ||
| 654 | - debug('not any data, keep waiting'); | ||
| 655 | - return; | ||
| 656 | - } | ||
| 657 | - | ||
| 658 | - // Error, possibly EOF. | ||
| 659 | - if (nread !== UV_EOF) { | ||
| 660 | - return self.destroy(errnoException(nread, 'read')); | ||
| 661 | - } | ||
| 662 | - | ||
| 663 | - debug('EOF'); | ||
| 664 | - | ||
| 665 | - // push a null to signal the end of data. | ||
| 666 | - // Do it before `maybeDestroy` for correct order of events: | ||
| 667 | - // `end` -> `close` | ||
| 668 | - self.push(null); | ||
| 669 | - self.read(0); | ||
| 670 | - } | ||
| 671 | - | ||
| 672 | - | ||
| 673 | 625 | Socket.prototype._getpeername = function() { | |
| 674 | 626 | if (!this._peername) { | |
| 675 | 627 | if (!this._handle || !this._handle.getpeername) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments