1515#include " database/src/desktop/connection/persistent_connection.h"
1616
1717#include < algorithm>
18- #include < cassert>
1918#include < sstream>
2019
2120#include " app/src/app_common.h"
@@ -118,9 +117,9 @@ PersistentConnection::PersistentConnection(
118117 next_listen_id_(0 ),
119118 next_write_id_(0 ),
120119 logger_(logger) {
121- assert (app);
122- assert (scheduler);
123- assert (event_handler_);
120+ FIREBASE_DEV_ASSERT (app);
121+ FIREBASE_DEV_ASSERT (scheduler);
122+ FIREBASE_DEV_ASSERT (event_handler_);
124123
125124 // Create log id like "[pc_0]" for debugging
126125 std::stringstream log_id_stream;
@@ -208,7 +207,7 @@ void PersistentConnection::OnReady(int64_t timestamp,
208207
209208 // Restore Auth
210209 logger_->LogDebug (" %s calling restore state" , log_id_.c_str ());
211- assert (connection_state_ == kConnecting );
210+ FIREBASE_DEV_ASSERT (connection_state_ == kConnecting );
212211
213212 // Try to retrieve auth token synchronously when connection is ready.
214213 GetAuthToken (&auth_token_);
@@ -247,26 +246,26 @@ void PersistentConnection::HandleConnectStatsResponse(
247246}
248247
249248void PersistentConnection::OnDataMessage (const Variant& message) {
250- assert (message.is_map ());
249+ FIREBASE_DEV_ASSERT (message.is_map ());
251250
252251 SAFE_REFERENCE_RETURN_VOID_IF_INVALID (ThisRefLock, lock, safe_this_);
253252
254253 if (HasKey (message, kRequestNumber )) {
255254 auto it_request_number = message.map ().find (kRequestNumber );
256- assert (it_request_number->second .is_numeric ());
255+ FIREBASE_DEV_ASSERT (it_request_number->second .is_numeric ());
257256 uint64_t rn = it_request_number->second .int64_value ();
258257
259258 RequestDataPtr request_ptr;
260259 auto it_request = request_map_.find (rn);
261- assert (it_request != request_map_.end ());
260+ FIREBASE_DEV_ASSERT (it_request != request_map_.end ());
262261 if (it_request != request_map_.end ()) {
263262 request_ptr = Move (it_request->second );
264263 request_map_.erase (it_request);
265264 }
266- assert (request_ptr);
265+ FIREBASE_DEV_ASSERT (request_ptr);
267266 if (request_ptr) {
268267 auto it_response_message = message.map ().find (kResponseForRequest );
269- assert (it_response_message != message.map ().end ());
268+ FIREBASE_DEV_ASSERT (it_response_message != message.map ().end ());
270269 if (it_response_message != message.map ().end ()) {
271270 logger_->LogDebug (" %s Trigger handler for request %llu" ,
272271 log_id_.c_str (), rn);
@@ -510,33 +509,41 @@ void PersistentConnection::TryScheduleReconnect() {
510509 return ;
511510 }
512511
513- assert (connection_state_ == kDisconnected );
512+ FIREBASE_DEV_ASSERT (connection_state_ == kDisconnected );
514513 bool force_refresh = force_auth_refresh_;
515514 force_auth_refresh_ = false ;
516515 logger_->LogDebug (" %s Scheduling connection attempt" , log_id_.c_str ());
517516
518- // TODO(chkuang): Implement Exponential Backoff Retry
519- connection_state_ = kGettingToken ;
520- logger_->LogDebug (" %s Trying to fetch auth token" , log_id_.c_str ());
521-
522- // Get Token Asynchronously to make sure the token is not expired.
523- Future<std::string> future;
524- bool succeeded = app_->function_registry ()->CallFunction (
525- ::firebase::internal::FnAuthGetTokenAsync, app_, &force_refresh, &future);
526- if (succeeded && future.status () != kFutureStatusInvalid ) {
527- // Set pending future
528- MutexLock future_lock (pending_token_future_mutex_);
529- pending_token_future_ = future;
530- future.OnCompletion (OnTokenFutureComplete, this );
531- } else {
532- // Auth is not available now. Start the connection anyway.
533- OpenNetworkConnection ();
534- }
517+ scheduler_->Schedule (new callback::CallbackValue2<ThisRef, bool >(
518+ safe_this_, force_refresh, [](ThisRef ref, bool force_refresh) {
519+ ThisRefLock lock (&ref);
520+ auto * connection = lock.GetReference ();
521+ if (!connection) return ;
522+ // TODO(chkuang): Implement Exponential Backoff Retry
523+ connection->connection_state_ = kGettingToken ;
524+ connection->logger_ ->LogDebug (" %s Trying to fetch auth token" ,
525+ connection->log_id_ .c_str ());
526+
527+ // Get Token Asynchronously to make sure the token is not expired.
528+ Future<std::string> future;
529+ bool succeeded = connection->app_ ->function_registry ()->CallFunction (
530+ ::firebase::internal::FnAuthGetTokenAsync, connection->app_ ,
531+ &force_refresh, &future);
532+ if (succeeded && future.status () != kFutureStatusInvalid ) {
533+ // Set pending future
534+ MutexLock future_lock (connection->pending_token_future_mutex_ );
535+ connection->pending_token_future_ = future;
536+ future.OnCompletion (OnTokenFutureComplete, connection);
537+ } else {
538+ // Auth is not available now. Start the connection anyway.
539+ connection->OpenNetworkConnection ();
540+ }
541+ }));
535542}
536543
537544void PersistentConnection::OnTokenFutureComplete (
538545 const Future<std::string>& result_data, void * user_data) {
539- assert (user_data);
546+ FIREBASE_DEV_ASSERT (user_data);
540547 PersistentConnection* connection =
541548 static_cast <PersistentConnection*>(user_data);
542549 ThisRefLock lock (&connection->safe_this_ );
@@ -568,7 +575,7 @@ void PersistentConnection::HandleTokenFuture(Future<std::string> future) {
568575 auth_token_ = *future.result ();
569576 OpenNetworkConnection ();
570577 } else {
571- assert (connection_state_ == kDisconnected );
578+ FIREBASE_DEV_ASSERT (connection_state_ == kDisconnected );
572579 logger_->LogDebug (
573580 " %s Not opening connection after token refresh, because "
574581 " connection was set to disconnected" ,
@@ -583,7 +590,7 @@ void PersistentConnection::HandleTokenFuture(Future<std::string> future) {
583590}
584591
585592void PersistentConnection::OpenNetworkConnection () {
586- assert (connection_state_ == kGettingToken );
593+ FIREBASE_DEV_ASSERT (connection_state_ == kGettingToken );
587594
588595 // User might have logged out. Positive auth status is handled after
589596 // authenticating with the server
@@ -823,10 +830,10 @@ void PersistentConnection::PutInternal(const char* action, const Path& path,
823830}
824831
825832void PersistentConnection::SendPut (uint64_t write_id) {
826- assert (CanSendWrites ());
833+ FIREBASE_DEV_ASSERT (CanSendWrites ());
827834
828835 auto it_put = outstanding_puts_.find (write_id);
829- assert (it_put != outstanding_puts_.end ());
836+ FIREBASE_DEV_ASSERT (it_put != outstanding_puts_.end ());
830837
831838 it_put->second ->MarkSent ();
832839 SendSensitive (it_put->second ->action .c_str (), false , it_put->second ->data ,
@@ -908,7 +915,7 @@ void PersistentConnection::SendSensitive(const char* action, bool sensitive,
908915 ResponsePtr response,
909916 ConnectionResponseHandler callback,
910917 uint64_t outstanding_id) {
911- assert (realtime_);
918+ FIREBASE_DEV_ASSERT (realtime_);
912919
913920 // Varient only accept int64_t
914921 int64_t rn = ++next_request_id_;
@@ -924,7 +931,7 @@ void PersistentConnection::SendSensitive(const char* action, bool sensitive,
924931}
925932
926933void PersistentConnection::RestoreOutstandingRequests () {
927- assert (connection_state_ == kConnected );
934+ FIREBASE_DEV_ASSERT (connection_state_ == kConnected );
928935
929936 // Restore listens
930937 logger_->LogDebug (" %s Restoring outstanding listens" , log_id_.c_str ());
@@ -950,7 +957,7 @@ void PersistentConnection::RestoreOutstandingRequests() {
950957}
951958
952959void PersistentConnection::GetAuthToken (std::string* out) {
953- assert (out);
960+ FIREBASE_DEV_ASSERT (out);
954961 app_->function_registry ()->CallFunction (
955962 ::firebase::internal::FnAuthGetCurrentToken, app_, nullptr , out);
956963}
@@ -1010,7 +1017,7 @@ void PersistentConnection::SendUnauth() {
10101017void PersistentConnection::HandleAuthTokenResponse (const Variant& message,
10111018 const ResponsePtr& response,
10121019 uint64_t outstanding_id) {
1013- assert (response);
1020+ FIREBASE_DEV_ASSERT (response);
10141021
10151022 connection_state_ = kConnected ;
10161023
0 commit comments