diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-03-17 19:52:57 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-03-30 15:20:40 +0300 |
commit | 923fb5cc1861422291d135177770f94f473f4d6f (patch) | |
tree | 7900acd97fa0c6c8f42b56382afb1597f026f384 /src/stream_base-inl.h | |
parent | abc87862ff14c1571f008aa1a9cbf812bea9790c (diff) |
net: track bytesWritten in C++ land
Move tracking of `socket.bytesWritten` to C++ land.
This makes it easier to provide this functionality for all
`StreamBase` instances, and in particular should keep working
when they have been 'consumed' in C++ in some way (e.g. for
the network sockets that are underlying to TLS or HTTP2 streams).
Also, this parallels `socket.bytesRead` a lot more now.
PR-URL: https://github.com/nodejs/node/pull/19551
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/stream_base-inl.h')
-rw-r--r-- | src/stream_base-inl.h | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h index f4c228d7c59..35e49dfea2c 100644 --- a/src/stream_base-inl.h +++ b/src/stream_base-inl.h @@ -193,6 +193,10 @@ inline StreamWriteResult StreamBase::Write( v8::Local<v8::Object> req_wrap_obj) { Environment* env = stream_env(); int err; + + for (size_t i = 0; i < count; ++i) + bytes_written_ += bufs[i].len; + if (send_handle == nullptr) { err = DoTryWrite(&bufs, &count); if (err != 0 || count == 0) { @@ -301,6 +305,12 @@ void StreamBase::AddMethods(Environment* env, env->as_external(), signature); + Local<FunctionTemplate> get_bytes_written_templ = + FunctionTemplate::New(env->isolate(), + GetBytesWritten<Base>, + env->as_external(), + signature); + t->PrototypeTemplate()->SetAccessorProperty(env->fd_string(), get_fd_templ, Local<FunctionTemplate>(), @@ -316,6 +326,11 @@ void StreamBase::AddMethods(Environment* env, Local<FunctionTemplate>(), attributes); + t->PrototypeTemplate()->SetAccessorProperty(env->bytes_written_string(), + get_bytes_written_templ, + Local<FunctionTemplate>(), + attributes); + env->SetProtoMethod(t, "readStart", JSMethod<Base, &StreamBase::ReadStartJS>); env->SetProtoMethod(t, "readStop", JSMethod<Base, &StreamBase::ReadStopJS>); if ((flags & kFlagNoShutdown) == 0) @@ -357,7 +372,6 @@ void StreamBase::GetFD(const FunctionCallbackInfo<Value>& args) { template <class Base> void StreamBase::GetBytesRead(const FunctionCallbackInfo<Value>& args) { - // The handle instance hasn't been set. So no bytes could have been read. Base* handle; ASSIGN_OR_RETURN_UNWRAP(&handle, args.This(), @@ -369,6 +383,18 @@ void StreamBase::GetBytesRead(const FunctionCallbackInfo<Value>& args) { } template <class Base> +void StreamBase::GetBytesWritten(const FunctionCallbackInfo<Value>& args) { + Base* handle; + ASSIGN_OR_RETURN_UNWRAP(&handle, + args.This(), + args.GetReturnValue().Set(0)); + + StreamBase* wrap = static_cast<StreamBase*>(handle); + // uint64_t -> double. 53bits is enough for all real cases. + args.GetReturnValue().Set(static_cast<double>(wrap->bytes_written_)); +} + +template <class Base> void StreamBase::GetExternal(const FunctionCallbackInfo<Value>& args) { Base* handle; ASSIGN_OR_RETURN_UNWRAP(&handle, args.This()); |