@@ -149,9 +149,8 @@ ClientImpl::ClientImpl(ClientConfig config)
149149 , m_fix_up_object_ids{config.fix_up_object_ids }
150150 , m_roundtrip_time_handler{std::move (config.roundtrip_time_handler )}
151151 , m_socket_provider{std::move (config.socket_provider )}
152- , m_client_protocol{} // Throws
153152 , m_one_connection_per_session{config.one_connection_per_session }
154- , m_random{ }
153+ , m_actualize_and_finalize{* this , &ClientImpl::actualize_and_finalize_session_wrappers, this }
155154{
156155 // FIXME: Would be better if seeding was up to the application.
157156 util::seed_prng_nondeterministically (m_random); // Throws
@@ -213,14 +212,6 @@ ClientImpl::ClientImpl(ClientConfig config)
213212 logger.warn (" Testing/debugging feature 'disable_sync_to_disk' enabled - "
214213 " never do this in production" );
215214 }
216-
217- m_actualize_and_finalize = create_trigger ([this ](Status status) {
218- if (status == ErrorCodes::OperationAborted)
219- return ;
220- else if (!status.is_ok ())
221- throw Exception (status);
222- actualize_and_finalize_session_wrappers (); // Throws
223- });
224215}
225216
226217void ClientImpl::incr_outstanding_posts ()
@@ -290,25 +281,23 @@ void ClientImpl::drain_connections()
290281
291282
292283SyncSocketProvider::SyncTimer ClientImpl::create_timer (std::chrono::milliseconds delay,
293- SyncSocketProvider::FunctionHandler && handler)
284+ util::UniqueFunction< void ()> && handler)
294285{
295286 REALM_ASSERT (m_socket_provider);
296287 incr_outstanding_posts ();
297288 return m_socket_provider->create_timer (delay, [handler = std::move (handler), this ](Status status) {
298- auto decr_guard = util::make_scope_exit ([&]() noexcept {
289+ ScopeExit decr_guard ([&]() noexcept {
299290 decr_outstanding_posts ();
300291 });
301- handler (status);
292+ if (status == ErrorCodes::OperationAborted)
293+ return ;
294+ if (!status.is_ok ())
295+ throw Exception (status);
296+ handler ();
302297 });
303298}
304299
305300
306- ClientImpl::SyncTrigger ClientImpl::create_trigger (SyncSocketProvider::FunctionHandler&& handler)
307- {
308- REALM_ASSERT (m_socket_provider);
309- return std::make_unique<Trigger<ClientImpl>>(this , std::move (handler));
310- }
311-
312301Connection::~Connection ()
313302{
314303 if (m_websocket_sentinel) {
@@ -319,10 +308,9 @@ Connection::~Connection()
319308
320309void Connection::activate ()
321310{
322- REALM_ASSERT (m_on_idle);
323311 m_activated = true ;
324312 if (m_num_active_sessions == 0 )
325- m_on_idle-> trigger ();
313+ m_on_idle. trigger ();
326314 // We cannot in general connect immediately, because a prior failure to
327315 // connect may require a delay before reconnecting (see `m_reconnect_info`).
328316 initiate_reconnect_wait (); // Throws
@@ -364,7 +352,7 @@ void Connection::initiate_session_deactivation(Session* sess)
364352 }
365353 if (REALM_UNLIKELY (--m_num_active_sessions == 0 )) {
366354 if (m_activated && m_state == ConnectionState::disconnected)
367- m_on_idle-> trigger ();
355+ m_on_idle. trigger ();
368356 }
369357}
370358
@@ -679,22 +667,14 @@ void Connection::initiate_reconnect_wait()
679667 // We create a timer for the reconnect_disconnect timer even if the delay is zero because
680668 // we need it to be cancelable in case the connection is terminated before the timer
681669 // callback is run.
682- m_reconnect_disconnect_timer = m_client.create_timer (delay, [this ](Status status) {
683- // If the operation is aborted, the connection object may have been
684- // destroyed.
685- if (status != ErrorCodes::OperationAborted)
686- handle_reconnect_wait (status); // Throws
687- }); // Throws
670+ m_reconnect_disconnect_timer = m_client.create_timer (delay, [this ] {
671+ handle_reconnect_wait (); // Throws
672+ }); // Throws
688673}
689674
690675
691- void Connection::handle_reconnect_wait (Status status )
676+ void Connection::handle_reconnect_wait ()
692677{
693- if (!status.is_ok ()) {
694- REALM_ASSERT (status != ErrorCodes::OperationAborted);
695- throw Exception (status);
696- }
697-
698678 REALM_ASSERT (m_reconnect_delay_in_progress);
699679 m_reconnect_delay_in_progress = false ;
700680
@@ -806,24 +786,15 @@ void Connection::initiate_connect_wait()
806786 // fully establish the connection (including SSL and WebSocket
807787 // handshakes). Without such a watchdog, connect operations could take very
808788 // long, or even indefinite time.
809- milliseconds_type time = m_client.m_connect_timeout ;
810-
811- m_connect_timer = m_client.create_timer (std::chrono::milliseconds (time), [this ](Status status) {
812- // If the operation is aborted, the connection object may have been
813- // destroyed.
814- if (status != ErrorCodes::OperationAborted)
815- handle_connect_wait (status); // Throws
816- }); // Throws
789+ std::chrono::milliseconds time (m_client.m_connect_timeout );
790+ m_connect_timer = m_client.create_timer (time, [this ] {
791+ handle_connect_wait (); // Throws
792+ }); // Throws
817793}
818794
819795
820- void Connection::handle_connect_wait (Status status )
796+ void Connection::handle_connect_wait ()
821797{
822- if (!status.is_ok ()) {
823- REALM_ASSERT (status != ErrorCodes::OperationAborted);
824- throw Exception (status);
825- }
826-
827798 REALM_ASSERT_EX (m_state == ConnectionState::connecting, m_state);
828799 logger.info (" Connect timeout" ); // Throws
829800 SessionErrorInfo error_info ({ErrorCodes::SyncConnectTimeout, " Sync connection was not fully established in time" },
@@ -917,12 +888,7 @@ void Connection::initiate_ping_delay(milliseconds_type now)
917888
918889 m_ping_delay_in_progress = true ;
919890
920- m_heartbeat_timer = m_client.create_timer (std::chrono::milliseconds (delay), [this ](Status status) {
921- if (status == ErrorCodes::OperationAborted)
922- return ;
923- else if (!status.is_ok ())
924- throw Exception (status);
925-
891+ m_heartbeat_timer = m_client.create_timer (std::chrono::milliseconds (delay), [this ] {
926892 handle_ping_delay (); // Throws
927893 }); // Throws
928894 logger.debug (" Will emit a ping in %1 milliseconds" , delay); // Throws
@@ -952,12 +918,7 @@ void Connection::initiate_pong_timeout()
952918 m_pong_wait_started_at = monotonic_clock_now ();
953919
954920 milliseconds_type time = m_client.m_pong_keepalive_timeout ;
955- m_heartbeat_timer = m_client.create_timer (std::chrono::milliseconds (time), [this ](Status status) {
956- if (status == ErrorCodes::OperationAborted)
957- return ;
958- else if (!status.is_ok ())
959- throw Exception (status);
960-
921+ m_heartbeat_timer = m_client.create_timer (std::chrono::milliseconds (time), [this ] {
961922 handle_pong_timeout (); // Throws
962923 }); // Throws
963924}
@@ -1108,23 +1069,15 @@ void Connection::initiate_disconnect_wait()
11081069
11091070 milliseconds_type time = m_client.m_connection_linger_time ;
11101071
1111- m_reconnect_disconnect_timer = m_client.create_timer (std::chrono::milliseconds (time), [this ](Status status) {
1112- // If the operation is aborted, the connection object may have been
1113- // destroyed.
1114- if (status != ErrorCodes::OperationAborted)
1115- handle_disconnect_wait (status); // Throws
1116- }); // Throws
1072+ m_reconnect_disconnect_timer = m_client.create_timer (std::chrono::milliseconds (time), [this ] {
1073+ handle_disconnect_wait (); // Throws
1074+ }); // Throws
11171075 m_disconnect_delay_in_progress = true ;
11181076}
11191077
11201078
1121- void Connection::handle_disconnect_wait (Status status )
1079+ void Connection::handle_disconnect_wait ()
11221080{
1123- if (!status.is_ok ()) {
1124- REALM_ASSERT (status != ErrorCodes::OperationAborted);
1125- throw Exception (status);
1126- }
1127-
11281081 m_disconnect_delay_in_progress = false ;
11291082
11301083 REALM_ASSERT_EX (m_state != ConnectionState::disconnected, m_state);
@@ -2649,12 +2602,7 @@ void Session::begin_resumption_delay(const ProtocolErrorInfo& error_info)
26492602 try_again_interval = std::chrono::milliseconds{1000 };
26502603 }
26512604 logger.debug (" Will attempt to resume session after %1 milliseconds" , try_again_interval.count ());
2652- m_try_again_activation_timer = get_client ().create_timer (try_again_interval, [this ](Status status) {
2653- if (status == ErrorCodes::OperationAborted)
2654- return ;
2655- else if (!status.is_ok ())
2656- throw Exception (status);
2657-
2605+ m_try_again_activation_timer = get_client ().create_timer (try_again_interval, [this ] {
26582606 m_try_again_activation_timer.reset ();
26592607 cancel_resumption_delay ();
26602608 });
0 commit comments