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

Commit 54e9596

Browse files
committed
quic: remove opaque pointer from Timer callback
Use the more C++-y `std::function` approach which renders opaque pointers unnecessary. PR-URL: #126 Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 930fb13 commit 54e9596

5 files changed

+9
-31
lines changed

src/node_quic_session-inl.h

-10
Original file line numberDiff line numberDiff line change
@@ -652,16 +652,6 @@ inline int Empty(const ngtcp2_vec* vec, size_t cnt) {
652652
return i == cnt;
653653
}
654654

655-
inline void QuicSession::OnIdleTimeoutCB(void* data) {
656-
QuicSession* session = static_cast<QuicSession*>(data);
657-
session->OnIdleTimeout();
658-
}
659-
660-
inline void QuicSession::OnRetransmitTimeoutCB(void* data) {
661-
QuicSession* session = static_cast<QuicSession*>(data);
662-
session->MaybeTimeout();
663-
}
664-
665655
} // namespace quic
666656
} // namespace node
667657

src/node_quic_session.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ QuicSession::QuicSession(
6464
alpn_(alpn),
6565
options_(options),
6666
initial_connection_close_(initial_connection_close),
67-
idle_(new Timer(socket->env(), OnIdleTimeoutCB, this)),
68-
retransmit_(new Timer(socket->env(), OnRetransmitTimeoutCB, this)),
67+
idle_(new Timer(socket->env(), [this]() { OnIdleTimeout(); })),
68+
retransmit_(new Timer(socket->env(), [this]() { MaybeTimeout(); })),
6969
state_(env()->isolate(), IDX_QUIC_SESSION_STATE_COUNT),
7070
allocator_(this),
7171
crypto_rx_ack_(

src/node_quic_session.h

-3
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,6 @@ class QuicSession : public AsyncWrap,
736736
const ngtcp2_pkt_stateless_reset* sr,
737737
void* user_data);
738738

739-
static inline void OnIdleTimeoutCB(void* data);
740-
static inline void OnRetransmitTimeoutCB(void* data);
741-
742739
void UpdateIdleTimer();
743740
void UpdateRetransmitTimer(uint64_t timeout);
744741
void StopRetransmitTimer();

src/node_quic_util.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void Timer::Free(Timer* timer) {
1919

2020
void Timer::OnTimeout(uv_timer_t* timer) {
2121
Timer* t = ContainerOf(&Timer::timer_, timer);
22-
t->OnTimeout();
22+
t->fn_();
2323
}
2424

2525
void Timer::CleanupHook(void* data) {

src/node_quic_util.h

+6-15
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,10 @@ void IncrementStat(
393393
// reset the timer; Stop to halt the timer.
394394
class Timer {
395395
public:
396-
inline explicit Timer(
397-
Environment* env,
398-
std::function<void(void* data)> fn,
399-
void* data = nullptr) :
400-
stopped_(false),
396+
explicit Timer(Environment* env, std::function<void()> fn)
397+
: stopped_(false),
401398
env_(env),
402-
fn_(fn),
403-
data_(data) {
399+
fn_(fn) {
404400
uv_timer_init(env_->event_loop(), &timer_);
405401
timer_.data = this;
406402
env->AddCleanupHook(CleanupHook, this);
@@ -412,7 +408,7 @@ class Timer {
412408

413409
// Stops the timer with the side effect of the timer no longer being usable.
414410
// It will be cleaned up and the Timer object will be destroyed.
415-
inline void Stop() {
411+
void Stop() {
416412
if (stopped_)
417413
return;
418414
stopped_ = true;
@@ -425,7 +421,7 @@ class Timer {
425421

426422
// If the timer is not currently active, interval must be either 0 or greater.
427423
// If the timer is already active, interval is ignored.
428-
inline void Update(uint64_t interval) {
424+
void Update(uint64_t interval) {
429425
if (stopped_)
430426
return;
431427
uv_timer_start(&timer_, OnTimeout, interval, interval);
@@ -435,18 +431,13 @@ class Timer {
435431
static void Free(Timer* timer);
436432

437433
private:
438-
inline void OnTimeout() {
439-
fn_(data_);
440-
}
441-
442434
static void OnTimeout(uv_timer_t* timer);
443435
static void CleanupHook(void* data);
444436

445437
bool stopped_;
446438
Environment* env_;
447-
std::function<void(void* data)> fn_;
439+
std::function<void()> fn_;
448440
uv_timer_t timer_;
449-
void* data_;
450441
};
451442

452443
using TimerPointer = DeleteFnPtr<Timer, Timer::Free>;

0 commit comments

Comments
 (0)