@@ -284,6 +284,14 @@ std::string QuicSession::diagnostic_name() const {
284
284
" (" + std::to_string (static_cast <int64_t >(get_async_id ())) + " )" ;
285
285
}
286
286
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
+
287
295
void QuicSession::AckedCryptoOffset (size_t datalen) {
288
296
// It is possible for the QuicSession to have been destroyed but not yet
289
297
// deconstructed. In such cases, we want to ignore the callback as there
@@ -331,7 +339,7 @@ void QuicSession::AckedStreamDataOffset(
331
339
332
340
// Add the given QuicStream to this QuicSession's collection of streams. All
333
341
// 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) {
335
343
DCHECK (!IsFlagSet (QUICSESSION_FLAG_GRACEFUL_CLOSING));
336
344
Debug (this , " Adding stream %" PRId64 " to session." , stream->GetID ());
337
345
streams_.emplace (stream->GetID (), stream);
@@ -400,7 +408,7 @@ void QuicSession::ImmediateClose() {
400
408
401
409
// Grab a shared pointer to this to prevent the QuicSession
402
410
// from being freed while the MakeCallback is running.
403
- std::shared_ptr <QuicSession> ptr (shared_from_this () );
411
+ BaseObjectPtr <QuicSession> ptr (this );
404
412
MakeCallback (env ()->quic_on_session_close_function (), arraysize (argv), argv);
405
413
}
406
414
@@ -411,16 +419,16 @@ QuicStream* QuicSession::CreateStream(int64_t stream_id) {
411
419
CHECK (!IsFlagSet (QUICSESSION_FLAG_GRACEFUL_CLOSING));
412
420
CHECK (!IsFlagSet (QUICSESSION_FLAG_CLOSING));
413
421
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);
416
424
Local<Value> argv[] = {
417
425
stream->object (),
418
426
Number::New (env ()->isolate (), static_cast <double >(stream_id))
419
427
};
420
428
421
429
// Grab a shared pointer to this to prevent the QuicSession
422
430
// from being freed while the MakeCallback is running.
423
- std::shared_ptr <QuicSession> ptr (shared_from_this () );
431
+ BaseObjectPtr <QuicSession> ptr (this );
424
432
MakeCallback (env ()->quic_on_stream_ready_function (), arraysize (argv), argv);
425
433
return stream.get ();
426
434
}
@@ -462,7 +470,7 @@ void QuicSession::Destroy() {
462
470
StopRetransmitTimer ();
463
471
464
472
// The QuicSession instances are kept alive using
465
- // std::shared_ptr . The only persistent shared_ptr
473
+ // BaseObjectPtr . The only persistent BaseObjectPtr
466
474
// is the map in the associated QuicSocket. Removing
467
475
// the QuicSession from the QuicSocket will free
468
476
// that pointer, allowing the QuicSession to be
@@ -726,7 +734,7 @@ void QuicSession::HandshakeCompleted() {
726
734
727
735
// Grab a shared pointer to this to prevent the QuicSession
728
736
// from being freed while the MakeCallback is running.
729
- std::shared_ptr <QuicSession> ptr (shared_from_this () );
737
+ BaseObjectPtr <QuicSession> ptr (this );
730
738
MakeCallback (env ()->quic_on_session_handshake_function (),
731
739
arraysize (argv),
732
740
argv);
@@ -789,7 +797,7 @@ void QuicSession::Keylog(const char* line) {
789
797
790
798
// Grab a shared pointer to this to prevent the QuicSession
791
799
// from being freed while the MakeCallback is running.
792
- std::shared_ptr <QuicSession> ptr (shared_from_this () );
800
+ BaseObjectPtr <QuicSession> ptr (this );
793
801
MakeCallback (env ()->quic_on_session_keylog_function (), 1 , &line_bf);
794
802
}
795
803
@@ -966,7 +974,7 @@ void QuicSession::PathValidation(
966
974
};
967
975
// Grab a shared pointer to this to prevent the QuicSession
968
976
// from being freed while the MakeCallback is running.
969
- std::shared_ptr <QuicSession> ptr (shared_from_this () );
977
+ BaseObjectPtr <QuicSession> ptr (this );
970
978
MakeCallback (
971
979
env ()->quic_on_session_path_validation_function (),
972
980
arraysize (argv),
@@ -1244,7 +1252,7 @@ void QuicSession::RemoveConnectionID(const ngtcp2_cid* cid) {
1244
1252
// Removes the QuicSession from the current socket. This is
1245
1253
// done with when the session is being destroyed or being
1246
1254
// 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.
1248
1256
// If the session is removed and there are no other references held,
1249
1257
// the session object will be destroyed automatically.
1250
1258
void QuicSession::RemoveFromSocket () {
@@ -1259,7 +1267,7 @@ void QuicSession::RemoveFromSocket() {
1259
1267
Debug (this , " Removed from the QuicSocket." );
1260
1268
QuicCID scid (scid_);
1261
1269
socket_->RemoveSession (&scid, **GetRemoteAddress ());
1262
- socket_ = nullptr ;
1270
+ socket_. reset () ;
1263
1271
}
1264
1272
1265
1273
// Removes the given stream from the QuicSession. All streams must
@@ -1472,7 +1480,7 @@ bool QuicSession::SendPacket(const char* diagnostic_label) {
1472
1480
int err = Socket ()->SendPacket (
1473
1481
*remote_address_,
1474
1482
&txbuf_,
1475
- shared_from_this ( ),
1483
+ BaseObjectPtr<QuicSession>( this ),
1476
1484
diagnostic_label);
1477
1485
if (err != 0 ) {
1478
1486
SetLastError (QUIC_ERROR_SESSION, err);
@@ -1597,7 +1605,7 @@ void QuicSession::SilentClose(bool stateless_reset) {
1597
1605
1598
1606
// Grab a shared pointer to this to prevent the QuicSession
1599
1607
// from being freed while the MakeCallback is running.
1600
- std::shared_ptr <QuicSession> ptr (shared_from_this () );
1608
+ BaseObjectPtr <QuicSession> ptr (this );
1601
1609
MakeCallback (
1602
1610
env ()->quic_on_session_silent_close_function (), arraysize (argv), argv);
1603
1611
}
@@ -1625,7 +1633,7 @@ void QuicSession::StreamClose(int64_t stream_id, uint64_t app_error_code) {
1625
1633
1626
1634
// Grab a shared pointer to this to prevent the QuicSession
1627
1635
// from being freed while the MakeCallback is running.
1628
- std::shared_ptr <QuicSession> ptr (shared_from_this () );
1636
+ BaseObjectPtr <QuicSession> ptr (this );
1629
1637
MakeCallback (env ()->quic_on_stream_close_function (), arraysize (argv), argv);
1630
1638
}
1631
1639
@@ -1699,7 +1707,7 @@ void QuicSession::StreamReset(
1699
1707
};
1700
1708
// Grab a shared pointer to this to prevent the QuicSession
1701
1709
// from being freed while the MakeCallback is running.
1702
- std::shared_ptr <QuicSession> ptr (shared_from_this () );
1710
+ BaseObjectPtr <QuicSession> ptr (this );
1703
1711
MakeCallback (env ()->quic_on_stream_reset_function (), arraysize (argv), argv);
1704
1712
}
1705
1713
@@ -1924,7 +1932,7 @@ QuicServerSession::QuicServerSession(
1924
1932
Init (config, addr, dcid, ocid, version);
1925
1933
}
1926
1934
1927
- std::shared_ptr <QuicSession> QuicServerSession::New (
1935
+ BaseObjectPtr <QuicSession> QuicServerSession::New (
1928
1936
QuicSocket* socket,
1929
1937
QuicSessionConfig* config,
1930
1938
const ngtcp2_cid* rcid,
@@ -1941,18 +1949,19 @@ std::shared_ptr<QuicSession> QuicServerSession::New(
1941
1949
->NewInstance (socket->env ()->context ()).ToLocal (&obj)) {
1942
1950
return {};
1943
1951
}
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);
1956
1965
1957
1966
session->AddToSocket (socket);
1958
1967
return session;
@@ -1962,7 +1971,7 @@ std::shared_ptr<QuicSession> QuicServerSession::New(
1962
1971
void QuicServerSession::AddToSocket (QuicSocket* socket) {
1963
1972
QuicCID scid (scid_);
1964
1973
QuicCID rcid (rcid_);
1965
- socket->AddSession (&scid, shared_from_this ( ));
1974
+ socket->AddSession (&scid, BaseObjectPtr<QuicSession>( this ));
1966
1975
socket->AssociateCID (&rcid, &scid);
1967
1976
1968
1977
if (pscid_.datalen ) {
@@ -2121,7 +2130,7 @@ int QuicServerSession::OnClientHello() {
2121
2130
2122
2131
// Grab a shared pointer to this to prevent the QuicSession
2123
2132
// from being freed while the MakeCallback is running.
2124
- std::shared_ptr <QuicSession> ptr (shared_from_this () );
2133
+ BaseObjectPtr <QuicSession> ptr (this );
2125
2134
MakeCallback (
2126
2135
env ()->quic_on_session_client_hello_function (),
2127
2136
arraysize (argv), argv);
@@ -2223,7 +2232,7 @@ int QuicServerSession::OnCert() {
2223
2232
2224
2233
// Grab a shared pointer to this to prevent the QuicSession
2225
2234
// from being freed while the MakeCallback is running.
2226
- std::shared_ptr <QuicSession> ptr (shared_from_this () );
2235
+ BaseObjectPtr <QuicSession> ptr (this );
2227
2236
MakeCallback (env ()->quic_on_session_cert_function (), arraysize (argv), argv);
2228
2237
2229
2238
return IsFlagSet (QUICSESSION_FLAG_CERT_CB_RUNNING) ? -1 : 1 ;
@@ -2268,7 +2277,7 @@ void QuicSession::UpdateRecoveryStats() {
2268
2277
recovery_stats_.smoothed_rtt = static_cast <double >(stat.smoothed_rtt );
2269
2278
}
2270
2279
2271
- // The QuicSocket maintains a map of std::shared_ptr 's that keep
2280
+ // The QuicSocket maintains a map of BaseObjectPtr 's that keep
2272
2281
// the QuicSession instance alive. Once socket_->RemoveSession()
2273
2282
// is called, the QuicSession instance will be freed if there are
2274
2283
// no other references being held.
@@ -2402,7 +2411,7 @@ QuicClientSession::QuicClientSession(
2402
2411
CHECK (Init (addr, version, early_transport_params, session_ticket, dcid));
2403
2412
}
2404
2413
2405
- std::shared_ptr <QuicSession> QuicClientSession::New (
2414
+ BaseObjectPtr <QuicSession> QuicClientSession::New (
2406
2415
QuicSocket* socket,
2407
2416
const struct sockaddr * addr,
2408
2417
uint32_t version,
@@ -2422,20 +2431,21 @@ std::shared_ptr<QuicSession> QuicClientSession::New(
2422
2431
return {};
2423
2432
}
2424
2433
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);
2439
2449
2440
2450
session->AddToSocket (socket);
2441
2451
session->TLSHandshake ();
@@ -2444,7 +2454,7 @@ std::shared_ptr<QuicSession> QuicClientSession::New(
2444
2454
2445
2455
void QuicClientSession::AddToSocket (QuicSocket* socket) {
2446
2456
QuicCID scid (scid_);
2447
- socket->AddSession (&scid, shared_from_this ( ));
2457
+ socket->AddSession (&scid, BaseObjectPtr<QuicSession>( this ));
2448
2458
2449
2459
std::vector<ngtcp2_cid> cids (ngtcp2_conn_get_num_scid (Connection ()));
2450
2460
ngtcp2_conn_get_scid (Connection (), cids.data ());
@@ -2484,7 +2494,7 @@ void QuicClientSession::VersionNegotiation(
2484
2494
2485
2495
// Grab a shared pointer to this to prevent the QuicSession
2486
2496
// from being freed while the MakeCallback is running.
2487
- std::shared_ptr <QuicSession> ptr (shared_from_this () );
2497
+ BaseObjectPtr <QuicSession> ptr (this );
2488
2498
MakeCallback (
2489
2499
env ()->quic_on_session_version_negotiation_function (),
2490
2500
arraysize (argv), argv);
@@ -2642,7 +2652,7 @@ int QuicClientSession::SetSession(SSL_SESSION* session) {
2642
2652
}
2643
2653
// Grab a shared pointer to this to prevent the QuicSession
2644
2654
// from being freed while the MakeCallback is running.
2645
- std::shared_ptr <QuicSession> ptr (shared_from_this () );
2655
+ BaseObjectPtr <QuicSession> ptr (this );
2646
2656
MakeCallback (env ()->quic_on_session_ticket_function (), arraysize (argv), argv);
2647
2657
2648
2658
return 1 ;
@@ -2651,7 +2661,7 @@ int QuicClientSession::SetSession(SSL_SESSION* session) {
2651
2661
bool QuicClientSession::SetSocket (QuicSocket* socket, bool nat_rebinding) {
2652
2662
CHECK (!IsFlagSet (QUICSESSION_FLAG_DESTROYED));
2653
2663
CHECK (!IsFlagSet (QUICSESSION_FLAG_GRACEFUL_CLOSING));
2654
- if (socket == nullptr || socket == socket_)
2664
+ if (socket == nullptr || socket == socket_. get () )
2655
2665
return true ;
2656
2666
2657
2667
// Step 1: Add this Session to the given Socket
@@ -2661,7 +2671,7 @@ bool QuicClientSession::SetSocket(QuicSocket* socket, bool nat_rebinding) {
2661
2671
RemoveFromSocket ();
2662
2672
2663
2673
// Step 3: Update the internal references
2664
- socket_ = socket;
2674
+ socket_. reset ( socket) ;
2665
2675
socket->ReceiveStart ();
2666
2676
2667
2677
// Step 4: Update ngtcp2
@@ -2737,7 +2747,7 @@ int QuicClientSession::OnTLSStatus() {
2737
2747
}
2738
2748
// Grab a shared pointer to this to prevent the QuicSession
2739
2749
// from being freed while the MakeCallback is running.
2740
- std::shared_ptr <QuicSession> ptr (shared_from_this () );
2750
+ BaseObjectPtr <QuicSession> ptr (this );
2741
2751
MakeCallback (env ()->quic_on_session_status_function (), 1 , &arg);
2742
2752
return 1 ;
2743
2753
}
@@ -3616,7 +3626,7 @@ void NewQuicClientSession(const FunctionCallbackInfo<Value>& args) {
3616
3626
3617
3627
socket->ReceiveStart ();
3618
3628
3619
- std::shared_ptr <QuicSession> session =
3629
+ BaseObjectPtr <QuicSession> session =
3620
3630
QuicClientSession::New (
3621
3631
socket,
3622
3632
const_cast <const sockaddr*>(reinterpret_cast <sockaddr*>(&addr)),
0 commit comments