From 923fb5cc1861422291d135177770f94f473f4d6f Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 17 Mar 2018 17:52:57 +0100 Subject: 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 --- src/stream_base-inl.h | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src/stream_base-inl.h') diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h index f4c228d7c5..35e49dfea2 100644 --- a/src/stream_base-inl.h +++ b/src/stream_base-inl.h @@ -193,6 +193,10 @@ inline StreamWriteResult StreamBase::Write( v8::Local 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 get_bytes_written_templ = + FunctionTemplate::New(env->isolate(), + GetBytesWritten, + env->as_external(), + signature); + t->PrototypeTemplate()->SetAccessorProperty(env->fd_string(), get_fd_templ, Local(), @@ -316,6 +326,11 @@ void StreamBase::AddMethods(Environment* env, Local(), attributes); + t->PrototypeTemplate()->SetAccessorProperty(env->bytes_written_string(), + get_bytes_written_templ, + Local(), + attributes); + env->SetProtoMethod(t, "readStart", JSMethod); env->SetProtoMethod(t, "readStop", JSMethod); if ((flags & kFlagNoShutdown) == 0) @@ -357,7 +372,6 @@ void StreamBase::GetFD(const FunctionCallbackInfo& args) { template void StreamBase::GetBytesRead(const FunctionCallbackInfo& args) { - // The handle instance hasn't been set. So no bytes could have been read. Base* handle; ASSIGN_OR_RETURN_UNWRAP(&handle, args.This(), @@ -368,6 +382,18 @@ void StreamBase::GetBytesRead(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(static_cast(wrap->bytes_read_)); } +template +void StreamBase::GetBytesWritten(const FunctionCallbackInfo& args) { + Base* handle; + ASSIGN_OR_RETURN_UNWRAP(&handle, + args.This(), + args.GetReturnValue().Set(0)); + + StreamBase* wrap = static_cast(handle); + // uint64_t -> double. 53bits is enough for all real cases. + args.GetReturnValue().Set(static_cast(wrap->bytes_written_)); +} + template void StreamBase::GetExternal(const FunctionCallbackInfo& args) { Base* handle; -- cgit v1.2.3