diff options
author | pupilTong <pupiltong@outlook.com> | 2022-05-15 16:54:06 +0300 |
---|---|---|
committer | Bryan English <bryan@bryanenglish.com> | 2022-05-30 19:33:54 +0300 |
commit | 8f5b4570e50f3b8333f70065dac7b09148c02dc4 (patch) | |
tree | 7439870f198cecd2ebca5f411f7cd6f3037cd8c5 /src | |
parent | 41b69e3cf469ea683592e20278f0b04c413ff34f (diff) |
net: add ability to reset a tcp socket
Fixes: https://github.com/nodejs/node/issues/27428
PR-URL: https://github.com/nodejs/node/pull/43112
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/handle_wrap.h | 2 | ||||
-rw-r--r-- | src/tcp_wrap.cc | 24 | ||||
-rw-r--r-- | src/tcp_wrap.h | 2 |
3 files changed, 27 insertions, 1 deletions
diff --git a/src/handle_wrap.h b/src/handle_wrap.h index 2e06829b7bd..a86f8b41c44 100644 --- a/src/handle_wrap.h +++ b/src/handle_wrap.h @@ -97,6 +97,7 @@ class HandleWrap : public AsyncWrap { } static void OnClose(uv_handle_t* handle); + enum { kInitialized, kClosing, kClosed } state_; private: friend class Environment; @@ -109,7 +110,6 @@ class HandleWrap : public AsyncWrap { // refer to `doc/contributing/node-postmortem-support.md` friend int GenDebugSymbols(); ListNode<HandleWrap> handle_wrap_queue_; - enum { kInitialized, kClosing, kClosed } state_; uv_handle_t* const handle_; }; diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 747d3e028c2..f3163fc84cd 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -97,6 +97,7 @@ void TCPWrap::Initialize(Local<Object> target, GetSockOrPeerName<TCPWrap, uv_tcp_getpeername>); env->SetProtoMethod(t, "setNoDelay", SetNoDelay); env->SetProtoMethod(t, "setKeepAlive", SetKeepAlive); + env->SetProtoMethod(t, "reset", Reset); #ifdef _WIN32 env->SetProtoMethod(t, "setSimultaneousAccepts", SetSimultaneousAccepts); @@ -134,6 +135,7 @@ void TCPWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(GetSockOrPeerName<TCPWrap, uv_tcp_getpeername>); registry->Register(SetNoDelay); registry->Register(SetKeepAlive); + registry->Register(Reset); #ifdef _WIN32 registry->Register(SetSimultaneousAccepts); #endif @@ -339,7 +341,29 @@ void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args, args.GetReturnValue().Set(err); } +void TCPWrap::Reset(const FunctionCallbackInfo<Value>& args) { + TCPWrap* wrap; + ASSIGN_OR_RETURN_UNWRAP( + &wrap, args.Holder(), args.GetReturnValue().Set(UV_EBADF)); + + int err = wrap->Reset(args[0]); + + args.GetReturnValue().Set(err); +} + +int TCPWrap::Reset(Local<Value> close_callback) { + if (state_ != kInitialized) return 0; + int err = uv_tcp_close_reset(&handle_, OnClose); + state_ = kClosing; + if (!err & !close_callback.IsEmpty() && close_callback->IsFunction() && + !persistent().IsEmpty()) { + object() + ->Set(env()->context(), env()->handle_onclose_symbol(), close_callback) + .Check(); + } + return err; +} // also used by udp_wrap.cc MaybeLocal<Object> AddressToJS(Environment* env, diff --git a/src/tcp_wrap.h b/src/tcp_wrap.h index 3abf4ded19f..b561fef0d31 100644 --- a/src/tcp_wrap.h +++ b/src/tcp_wrap.h @@ -88,6 +88,8 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> { const v8::FunctionCallbackInfo<v8::Value>& args, int family, std::function<int(const char* ip_address, int port, T* addr)> uv_ip_addr); + static void Reset(const v8::FunctionCallbackInfo<v8::Value>& args); + int Reset(v8::Local<v8::Value> close_callback = v8::Local<v8::Value>()); #ifdef _WIN32 static void SetSimultaneousAccepts( |