-
-
Notifications
You must be signed in to change notification settings - Fork 36.5k
stream_base: expose bytesRead getter
#6284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,7 +97,6 @@ exports._normalizeConnectArgs = normalizeConnectArgs; | |
| // called when creating new Socket, or when re-using a closed Socket | ||
| function initSocketHandle(self) { | ||
| self.destroyed = false; | ||
| self.bytesRead = 0; | ||
| self._bytesDispatched = 0; | ||
| self._sockname = null; | ||
|
|
||
|
|
@@ -179,6 +178,9 @@ function Socket(options) { | |
| // Reserve properties | ||
| this.server = null; | ||
| this._server = null; | ||
|
|
||
| // Used after `.destroy()` | ||
| this._bytesRead = 0; | ||
| } | ||
| util.inherits(Socket, stream.Duplex); | ||
|
|
||
|
|
@@ -470,6 +472,9 @@ Socket.prototype._destroy = function(exception, cb) { | |
| if (this !== process.stderr) | ||
| debug('close handle'); | ||
| var isException = exception ? true : false; | ||
| // `bytesRead` should be accessible after `.destroy()` | ||
| this._bytesRead = this._handle.bytesRead; | ||
|
|
||
| this._handle.close(() => { | ||
| debug('emit close'); | ||
| this.emit('close', isException); | ||
|
|
@@ -521,10 +526,6 @@ function onread(nread, buffer) { | |
| // will prevent this from being called again until _read() gets | ||
| // called again. | ||
|
|
||
| // if it's not enough data, we'll just call handle.readStart() | ||
| // again right away. | ||
| self.bytesRead += nread; | ||
|
|
||
| // Optimization: emit the original buffer with end points | ||
| var ret = self.push(buffer); | ||
|
|
||
|
|
@@ -580,6 +581,9 @@ Socket.prototype._getpeername = function() { | |
| return this._peername; | ||
| }; | ||
|
|
||
| Socket.prototype.__defineGetter__('bytesRead', function() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since e.g.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack. |
||
| return this._handle ? this._handle.bytesRead : this._bytesRead; | ||
| }); | ||
|
|
||
| Socket.prototype.__defineGetter__('remoteAddress', function() { | ||
| return this._getpeername().address; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -136,7 +136,7 @@ class StreamResource { | |
| uv_handle_type pending, | ||
| void* ctx); | ||
|
|
||
| StreamResource() { | ||
| StreamResource() : bytes_read_(0) { | ||
| } | ||
| virtual ~StreamResource() = default; | ||
|
|
||
|
|
@@ -160,9 +160,11 @@ class StreamResource { | |
| alloc_cb_.fn(size, buf, alloc_cb_.ctx); | ||
| } | ||
|
|
||
| inline void OnRead(size_t nread, | ||
| inline void OnRead(ssize_t nread, | ||
| const uv_buf_t* buf, | ||
| uv_handle_type pending = UV_UNKNOWN_HANDLE) { | ||
| if (nread > 0) | ||
| bytes_read_ += nread; | ||
| if (!read_cb_.is_empty()) | ||
| read_cb_.fn(nread, buf, pending, read_cb_.ctx); | ||
| } | ||
|
|
@@ -182,6 +184,9 @@ class StreamResource { | |
| Callback<AfterWriteCb> after_write_cb_; | ||
| Callback<AllocCb> alloc_cb_; | ||
| Callback<ReadCb> read_cb_; | ||
| int bytes_read_; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why int instead of size_t?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Come to think of it, it should be
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 to
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack, with some explicit conversions. |
||
|
|
||
| friend class StreamBase; | ||
| }; | ||
|
|
||
| class StreamBase : public StreamResource { | ||
|
|
@@ -249,6 +254,10 @@ class StreamBase : public StreamResource { | |
| static void GetExternal(v8::Local<v8::String> key, | ||
| const v8::PropertyCallbackInfo<v8::Value>& args); | ||
|
|
||
| template <class Base> | ||
| static void GetBytesRead(v8::Local<v8::String> key, | ||
| const v8::PropertyCallbackInfo<v8::Value>& args); | ||
|
|
||
| template <class Base, | ||
| int (StreamBase::*Method)( // NOLINT(whitespace/parens) | ||
| const v8::FunctionCallbackInfo<v8::Value>& args)> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const net = require('net'); | ||
|
|
||
| const big = new Buffer(1024 * 1024); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
actually...
and you can remove the fill on the next line
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmm... this may give us some headache during backport to v4
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nevertheless, ack.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood. We're going to have that problem in general anyway. Best to use the new APIs moving forward tho
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that's why I fixed it ;) |
||
| big.fill('-'); | ||
|
|
||
| const server = net.createServer((socket) => { | ||
| socket.end(big); | ||
| server.close(); | ||
| }).listen(common.PORT, () => { | ||
| let prev = 0; | ||
|
|
||
| function checkRaise(value) { | ||
| assert(value > prev); | ||
| prev = value; | ||
| } | ||
|
|
||
| const socket = net.connect(common.PORT, () => { | ||
| socket.on('data', (chunk) => { | ||
| checkRaise(socket.bytesRead); | ||
| }); | ||
|
|
||
| socket.on('end', common.mustCall(() => { | ||
| assert.equal(socket.bytesRead, prev); | ||
| assert.equal(big.length, prev); | ||
| })); | ||
|
|
||
| socket.on('close', common.mustCall(() => { | ||
| assert(!socket._handle); | ||
| assert.equal(socket.bytesRead, prev); | ||
| assert.equal(big.length, prev); | ||
| })); | ||
| }); | ||
| socket.end(); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider making this a symbol.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack.