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

Commit 2c1cdd9

Browse files
addaleaxjasnell
authored andcommitted
quic: use custom BaseObject smart pointers
PR-URL: #141 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent c588393 commit 2c1cdd9

File tree

7 files changed

+111
-106
lines changed

7 files changed

+111
-106
lines changed

src/node_quic_session-inl.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

66
#include "node_quic_session.h"
7+
#include "node_quic_socket.h"
78

89
#include <algorithm>
910

@@ -47,14 +48,6 @@ bool QuicSession::IsInDrainingPeriod() {
4748
return ngtcp2_conn_is_in_draining_period(Connection());
4849
}
4950

50-
// Locate the QuicStream with the given id or return nullptr
51-
QuicStream* QuicSession::FindStream(int64_t id) {
52-
auto it = streams_.find(id);
53-
if (it == std::end(streams_))
54-
return nullptr;
55-
return it->second.get();
56-
}
57-
5851
bool QuicSession::HasStream(int64_t id) {
5952
return streams_.find(id) != std::end(streams_);
6053
}
@@ -74,6 +67,10 @@ void QuicSession::StartGracefulClose() {
7467
session_stats_.closing_at = uv_hrtime();
7568
}
7669

70+
QuicSocket* QuicSession::Socket() const {
71+
return socket_.get();
72+
}
73+
7774
} // namespace quic
7875
} // namespace node
7976

src/node_quic_session.cc

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,14 @@ std::string QuicSession::diagnostic_name() const {
284284
" (" + std::to_string(static_cast<int64_t>(get_async_id())) + ")";
285285
}
286286

287+
// Locate the QuicStream with the given id or return nullptr
288+
QuicStream* QuicSession::FindStream(int64_t id) {
289+
auto it = streams_.find(id);
290+
if (it == std::end(streams_))
291+
return nullptr;
292+
return it->second.get();
293+
}
294+
287295
void QuicSession::AckedCryptoOffset(size_t datalen) {
288296
// It is possible for the QuicSession to have been destroyed but not yet
289297
// deconstructed. In such cases, we want to ignore the callback as there
@@ -331,7 +339,7 @@ void QuicSession::AckedStreamDataOffset(
331339

332340
// Add the given QuicStream to this QuicSession's collection of streams. All
333341
// streams added must be removed before the QuicSession instance is freed.
334-
void QuicSession::AddStream(std::shared_ptr<QuicStream> stream) {
342+
void QuicSession::AddStream(BaseObjectPtr<QuicStream> stream) {
335343
DCHECK(!IsFlagSet(QUICSESSION_FLAG_GRACEFUL_CLOSING));
336344
Debug(this, "Adding stream %" PRId64 " to session.", stream->GetID());
337345
streams_.emplace(stream->GetID(), stream);
@@ -400,7 +408,7 @@ void QuicSession::ImmediateClose() {
400408

401409
// Grab a shared pointer to this to prevent the QuicSession
402410
// from being freed while the MakeCallback is running.
403-
std::shared_ptr<QuicSession> ptr(shared_from_this());
411+
BaseObjectPtr<QuicSession> ptr(this);
404412
MakeCallback(env()->quic_on_session_close_function(), arraysize(argv), argv);
405413
}
406414

@@ -411,16 +419,16 @@ QuicStream* QuicSession::CreateStream(int64_t stream_id) {
411419
CHECK(!IsFlagSet(QUICSESSION_FLAG_GRACEFUL_CLOSING));
412420
CHECK(!IsFlagSet(QUICSESSION_FLAG_CLOSING));
413421

414-
std::shared_ptr<QuicStream> stream = QuicStream::New(this, stream_id);
415-
CHECK_NOT_NULL(stream);
422+
BaseObjectPtr<QuicStream> stream = QuicStream::New(this, stream_id);
423+
CHECK(stream);
416424
Local<Value> argv[] = {
417425
stream->object(),
418426
Number::New(env()->isolate(), static_cast<double>(stream_id))
419427
};
420428

421429
// Grab a shared pointer to this to prevent the QuicSession
422430
// from being freed while the MakeCallback is running.
423-
std::shared_ptr<QuicSession> ptr(shared_from_this());
431+
BaseObjectPtr<QuicSession> ptr(this);
424432
MakeCallback(env()->quic_on_stream_ready_function(), arraysize(argv), argv);
425433
return stream.get();
426434
}
@@ -462,7 +470,7 @@ void QuicSession::Destroy() {
462470
StopRetransmitTimer();
463471

464472
// The QuicSession instances are kept alive using
465-
// std::shared_ptr. The only persistent shared_ptr
473+
// BaseObjectPtr. The only persistent BaseObjectPtr
466474
// is the map in the associated QuicSocket. Removing
467475
// the QuicSession from the QuicSocket will free
468476
// that pointer, allowing the QuicSession to be
@@ -726,7 +734,7 @@ void QuicSession::HandshakeCompleted() {
726734

727735
// Grab a shared pointer to this to prevent the QuicSession
728736
// from being freed while the MakeCallback is running.
729-
std::shared_ptr<QuicSession> ptr(shared_from_this());
737+
BaseObjectPtr<QuicSession> ptr(this);
730738
MakeCallback(env()->quic_on_session_handshake_function(),
731739
arraysize(argv),
732740
argv);
@@ -789,7 +797,7 @@ void QuicSession::Keylog(const char* line) {
789797

790798
// Grab a shared pointer to this to prevent the QuicSession
791799
// from being freed while the MakeCallback is running.
792-
std::shared_ptr<QuicSession> ptr(shared_from_this());
800+
BaseObjectPtr<QuicSession> ptr(this);
793801
MakeCallback(env()->quic_on_session_keylog_function(), 1, &line_bf);
794802
}
795803

@@ -966,7 +974,7 @@ void QuicSession::PathValidation(
966974
};
967975
// Grab a shared pointer to this to prevent the QuicSession
968976
// from being freed while the MakeCallback is running.
969-
std::shared_ptr<QuicSession> ptr(shared_from_this());
977+
BaseObjectPtr<QuicSession> ptr(this);
970978
MakeCallback(
971979
env()->quic_on_session_path_validation_function(),
972980
arraysize(argv),
@@ -1244,7 +1252,7 @@ void QuicSession::RemoveConnectionID(const ngtcp2_cid* cid) {
12441252
// Removes the QuicSession from the current socket. This is
12451253
// done with when the session is being destroyed or being
12461254
// migrated to another QuicSocket. It is important to keep in mind
1247-
// that the QuicSocket uses a shared_ptr for the QuicSession.
1255+
// that the QuicSocket uses a BaseObjectPtr for the QuicSession.
12481256
// If the session is removed and there are no other references held,
12491257
// the session object will be destroyed automatically.
12501258
void QuicSession::RemoveFromSocket() {
@@ -1259,7 +1267,7 @@ void QuicSession::RemoveFromSocket() {
12591267
Debug(this, "Removed from the QuicSocket.");
12601268
QuicCID scid(scid_);
12611269
socket_->RemoveSession(&scid, **GetRemoteAddress());
1262-
socket_ = nullptr;
1270+
socket_.reset();
12631271
}
12641272

12651273
// Removes the given stream from the QuicSession. All streams must
@@ -1472,7 +1480,7 @@ bool QuicSession::SendPacket(const char* diagnostic_label) {
14721480
int err = Socket()->SendPacket(
14731481
*remote_address_,
14741482
&txbuf_,
1475-
shared_from_this(),
1483+
BaseObjectPtr<QuicSession>(this),
14761484
diagnostic_label);
14771485
if (err != 0) {
14781486
SetLastError(QUIC_ERROR_SESSION, err);
@@ -1597,7 +1605,7 @@ void QuicSession::SilentClose(bool stateless_reset) {
15971605

15981606
// Grab a shared pointer to this to prevent the QuicSession
15991607
// from being freed while the MakeCallback is running.
1600-
std::shared_ptr<QuicSession> ptr(shared_from_this());
1608+
BaseObjectPtr<QuicSession> ptr(this);
16011609
MakeCallback(
16021610
env()->quic_on_session_silent_close_function(), arraysize(argv), argv);
16031611
}
@@ -1625,7 +1633,7 @@ void QuicSession::StreamClose(int64_t stream_id, uint64_t app_error_code) {
16251633

16261634
// Grab a shared pointer to this to prevent the QuicSession
16271635
// from being freed while the MakeCallback is running.
1628-
std::shared_ptr<QuicSession> ptr(shared_from_this());
1636+
BaseObjectPtr<QuicSession> ptr(this);
16291637
MakeCallback(env()->quic_on_stream_close_function(), arraysize(argv), argv);
16301638
}
16311639

@@ -1699,7 +1707,7 @@ void QuicSession::StreamReset(
16991707
};
17001708
// Grab a shared pointer to this to prevent the QuicSession
17011709
// from being freed while the MakeCallback is running.
1702-
std::shared_ptr<QuicSession> ptr(shared_from_this());
1710+
BaseObjectPtr<QuicSession> ptr(this);
17031711
MakeCallback(env()->quic_on_stream_reset_function(), arraysize(argv), argv);
17041712
}
17051713

@@ -1924,7 +1932,7 @@ QuicServerSession::QuicServerSession(
19241932
Init(config, addr, dcid, ocid, version);
19251933
}
19261934

1927-
std::shared_ptr<QuicSession> QuicServerSession::New(
1935+
BaseObjectPtr<QuicSession> QuicServerSession::New(
19281936
QuicSocket* socket,
19291937
QuicSessionConfig* config,
19301938
const ngtcp2_cid* rcid,
@@ -1941,18 +1949,19 @@ std::shared_ptr<QuicSession> QuicServerSession::New(
19411949
->NewInstance(socket->env()->context()).ToLocal(&obj)) {
19421950
return {};
19431951
}
1944-
std::shared_ptr<QuicSession> session { new QuicServerSession(
1945-
socket,
1946-
config,
1947-
obj,
1948-
rcid,
1949-
addr,
1950-
dcid,
1951-
ocid,
1952-
version,
1953-
alpn,
1954-
options,
1955-
initial_connection_close) };
1952+
BaseObjectPtr<QuicSession> session =
1953+
MakeDetachedBaseObject<QuicServerSession>(
1954+
socket,
1955+
config,
1956+
obj,
1957+
rcid,
1958+
addr,
1959+
dcid,
1960+
ocid,
1961+
version,
1962+
alpn,
1963+
options,
1964+
initial_connection_close);
19561965

19571966
session->AddToSocket(socket);
19581967
return session;
@@ -1962,7 +1971,7 @@ std::shared_ptr<QuicSession> QuicServerSession::New(
19621971
void QuicServerSession::AddToSocket(QuicSocket* socket) {
19631972
QuicCID scid(scid_);
19641973
QuicCID rcid(rcid_);
1965-
socket->AddSession(&scid, shared_from_this());
1974+
socket->AddSession(&scid, BaseObjectPtr<QuicSession>(this));
19661975
socket->AssociateCID(&rcid, &scid);
19671976

19681977
if (pscid_.datalen) {
@@ -2121,7 +2130,7 @@ int QuicServerSession::OnClientHello() {
21212130

21222131
// Grab a shared pointer to this to prevent the QuicSession
21232132
// from being freed while the MakeCallback is running.
2124-
std::shared_ptr<QuicSession> ptr(shared_from_this());
2133+
BaseObjectPtr<QuicSession> ptr(this);
21252134
MakeCallback(
21262135
env()->quic_on_session_client_hello_function(),
21272136
arraysize(argv), argv);
@@ -2223,7 +2232,7 @@ int QuicServerSession::OnCert() {
22232232

22242233
// Grab a shared pointer to this to prevent the QuicSession
22252234
// from being freed while the MakeCallback is running.
2226-
std::shared_ptr<QuicSession> ptr(shared_from_this());
2235+
BaseObjectPtr<QuicSession> ptr(this);
22272236
MakeCallback(env()->quic_on_session_cert_function(), arraysize(argv), argv);
22282237

22292238
return IsFlagSet(QUICSESSION_FLAG_CERT_CB_RUNNING) ? -1 : 1;
@@ -2268,7 +2277,7 @@ void QuicSession::UpdateRecoveryStats() {
22682277
recovery_stats_.smoothed_rtt = static_cast<double>(stat.smoothed_rtt);
22692278
}
22702279

2271-
// The QuicSocket maintains a map of std::shared_ptr's that keep
2280+
// The QuicSocket maintains a map of BaseObjectPtr's that keep
22722281
// the QuicSession instance alive. Once socket_->RemoveSession()
22732282
// is called, the QuicSession instance will be freed if there are
22742283
// no other references being held.
@@ -2402,7 +2411,7 @@ QuicClientSession::QuicClientSession(
24022411
CHECK(Init(addr, version, early_transport_params, session_ticket, dcid));
24032412
}
24042413

2405-
std::shared_ptr<QuicSession> QuicClientSession::New(
2414+
BaseObjectPtr<QuicSession> QuicClientSession::New(
24062415
QuicSocket* socket,
24072416
const struct sockaddr* addr,
24082417
uint32_t version,
@@ -2422,20 +2431,21 @@ std::shared_ptr<QuicSession> QuicClientSession::New(
24222431
return {};
24232432
}
24242433

2425-
std::shared_ptr<QuicSession> session { new QuicClientSession(
2426-
socket,
2427-
obj,
2428-
addr,
2429-
version,
2430-
context,
2431-
hostname,
2432-
port,
2433-
early_transport_params,
2434-
session_ticket,
2435-
dcid,
2436-
select_preferred_address_policy,
2437-
alpn,
2438-
options) };
2434+
BaseObjectPtr<QuicSession> session =
2435+
MakeDetachedBaseObject<QuicClientSession>(
2436+
socket,
2437+
obj,
2438+
addr,
2439+
version,
2440+
context,
2441+
hostname,
2442+
port,
2443+
early_transport_params,
2444+
session_ticket,
2445+
dcid,
2446+
select_preferred_address_policy,
2447+
alpn,
2448+
options);
24392449

24402450
session->AddToSocket(socket);
24412451
session->TLSHandshake();
@@ -2444,7 +2454,7 @@ std::shared_ptr<QuicSession> QuicClientSession::New(
24442454

24452455
void QuicClientSession::AddToSocket(QuicSocket* socket) {
24462456
QuicCID scid(scid_);
2447-
socket->AddSession(&scid, shared_from_this());
2457+
socket->AddSession(&scid, BaseObjectPtr<QuicSession>(this));
24482458

24492459
std::vector<ngtcp2_cid> cids(ngtcp2_conn_get_num_scid(Connection()));
24502460
ngtcp2_conn_get_scid(Connection(), cids.data());
@@ -2484,7 +2494,7 @@ void QuicClientSession::VersionNegotiation(
24842494

24852495
// Grab a shared pointer to this to prevent the QuicSession
24862496
// from being freed while the MakeCallback is running.
2487-
std::shared_ptr<QuicSession> ptr(shared_from_this());
2497+
BaseObjectPtr<QuicSession> ptr(this);
24882498
MakeCallback(
24892499
env()->quic_on_session_version_negotiation_function(),
24902500
arraysize(argv), argv);
@@ -2642,7 +2652,7 @@ int QuicClientSession::SetSession(SSL_SESSION* session) {
26422652
}
26432653
// Grab a shared pointer to this to prevent the QuicSession
26442654
// from being freed while the MakeCallback is running.
2645-
std::shared_ptr<QuicSession> ptr(shared_from_this());
2655+
BaseObjectPtr<QuicSession> ptr(this);
26462656
MakeCallback(env()->quic_on_session_ticket_function(), arraysize(argv), argv);
26472657

26482658
return 1;
@@ -2651,7 +2661,7 @@ int QuicClientSession::SetSession(SSL_SESSION* session) {
26512661
bool QuicClientSession::SetSocket(QuicSocket* socket, bool nat_rebinding) {
26522662
CHECK(!IsFlagSet(QUICSESSION_FLAG_DESTROYED));
26532663
CHECK(!IsFlagSet(QUICSESSION_FLAG_GRACEFUL_CLOSING));
2654-
if (socket == nullptr || socket == socket_)
2664+
if (socket == nullptr || socket == socket_.get())
26552665
return true;
26562666

26572667
// Step 1: Add this Session to the given Socket
@@ -2661,7 +2671,7 @@ bool QuicClientSession::SetSocket(QuicSocket* socket, bool nat_rebinding) {
26612671
RemoveFromSocket();
26622672

26632673
// Step 3: Update the internal references
2664-
socket_ = socket;
2674+
socket_.reset(socket);
26652675
socket->ReceiveStart();
26662676

26672677
// Step 4: Update ngtcp2
@@ -2737,7 +2747,7 @@ int QuicClientSession::OnTLSStatus() {
27372747
}
27382748
// Grab a shared pointer to this to prevent the QuicSession
27392749
// from being freed while the MakeCallback is running.
2740-
std::shared_ptr<QuicSession> ptr(shared_from_this());
2750+
BaseObjectPtr<QuicSession> ptr(this);
27412751
MakeCallback(env()->quic_on_session_status_function(), 1, &arg);
27422752
return 1;
27432753
}
@@ -3616,7 +3626,7 @@ void NewQuicClientSession(const FunctionCallbackInfo<Value>& args) {
36163626

36173627
socket->ReceiveStart();
36183628

3619-
std::shared_ptr<QuicSession> session =
3629+
BaseObjectPtr<QuicSession> session =
36203630
QuicClientSession::New(
36213631
socket,
36223632
const_cast<const sockaddr*>(reinterpret_cast<sockaddr*>(&addr)),

0 commit comments

Comments
 (0)