Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
quic: remove CleanupHook from Timer
Browse files Browse the repository at this point in the history
This means that we always need to use `DeleteFnPtr` to make sure
that the timer is actually cleaned up, but I guess that’s an
acceptable restriction for now.
  • Loading branch information
addaleax committed Oct 11, 2019
1 parent 134855f commit 4de6721
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 13 deletions.
95 changes: 95 additions & 0 deletions src/js_udp_wrap.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "udp_wrap.h"

namespace node {

class JSUDPWrap final : public UDPWrapBase, public AsyncWrap {
public:
JSUDPWrap(Environment* env, Local<Object> obj);

static void New(const FunctionCallbackInfo<Value>& args);
static void Receive(const FunctionCallbackInfo<Value>& args);
static void CreateSendWrap(const FunctionCallbackInfo<Value>& args);
static void SendDone(const FunctionCallbackInfo<Value>& args);
static void AfterBind(const FunctionCallbackInfo<Value>& args);

int RecvStart() override;
int RecvStop() override;
ssize_t Send(uv_buf_t* bufs,
size_t nbufs,
const sockaddr* addr) override;
int GetPeerName(sockaddr* name, int* namelen) override;
int GetSockName(sockaddr* name, int* namelen) override;
AsyncWrap* GetAsyncWrap() override;

static void Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv);
};

JSUDPWrap::JSUDPWrap(Environment* env, Local<Object> obj) {
}

void JSUDPWrap::New(const FunctionCallbackInfo<Value>& args) {
}

void JSUDPWrap::Receive(const FunctionCallbackInfo<Value>& args) {
}

void JSUDPWrap::CreateSendWrap(const FunctionCallbackInfo<Value>& args) {
}

void JSUDPWrap::SendDone(const FunctionCallbackInfo<Value>& args) {
}

void JSUDPWrap::AfterBind(const FunctionCallbackInfo<Value>& args) {
}

int JSUDPWrap::RecvStart() override {
}

int JSUDPWrap::RecvStop() override {
}

ssize_t JSUDPWrap::Send(uv_buf_t* bufs,
size_t nbufs,
const sockaddr* addr) override {
}

int JSUDPWrap::GetPeerName(sockaddr* name, int* namelen) override {
}

int JSUDPWrap::GetSockName(sockaddr* name, int* namelen) override {
}

AsyncWrap* JSUDPWrap::GetAsyncWrap() override {
return this;
}

void JSUDPWrap::Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Environment* env = Environment::GetCurrent(context);
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(kUDPWrapBaseField + 1);
Local<String> js_udp_string =
FIXED_ONE_BYTE_STRING(env->isolate(), "JSUDPWrap");
t->SetClassName(js_udp_string);

env->SetProtoMethod(t, "receive", Receive);
env->SetProtoMethod(t, "createSendWrap", CreateSendWrap);
env->SetProtoMethod(t, "sendDone", SendDone);
env->SetProtoMethod(t, "afterBind", AfterBind);

t->Inherit(HandleWrap::GetConstructorTemplate(env));

target->Set(env->context(),
js_udp_string,
t->GetFunction(env->context()).ToLocalChecked()).Check();
}

} // namespace node

NODE_MODULE_CONTEXT_AWARE_INTERNAL(js_udp_wrap, node::JSUDPWrap::Initialize)

4 changes: 0 additions & 4 deletions src/node_quic_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ void Timer::OnTimeout(uv_timer_t* timer) {
t->fn_();
}

void Timer::CleanupHook(void* data) {
Free(static_cast<Timer*>(data));
}

ngtcp2_crypto_level from_ossl_level(OSSL_ENCRYPTION_LEVEL ossl_level) {
switch (ossl_level) {
case ssl_encryption_initial:
Expand Down
11 changes: 2 additions & 9 deletions src/node_quic_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,16 +379,10 @@ void IncrementStat(
class Timer final : public MemoryRetainer {
public:
explicit Timer(Environment* env, std::function<void()> fn)
: stopped_(false),
env_(env),
: env_(env),
fn_(fn) {
uv_timer_init(env_->event_loop(), &timer_);
timer_.data = this;
env->AddCleanupHook(CleanupHook, this);
}

~Timer() override {
env_->RemoveCleanupHook(CleanupHook, this);
}

// Stops the timer with the side effect of the timer no longer being usable.
Expand Down Expand Up @@ -421,9 +415,8 @@ class Timer final : public MemoryRetainer {

private:
static void OnTimeout(uv_timer_t* timer);
static void CleanupHook(void* data);

bool stopped_;
bool stopped_ = false;
Environment* env_;
std::function<void()> fn_;
uv_timer_t timer_;
Expand Down

0 comments on commit 4de6721

Please sign in to comment.