15
15
#include " database/src/desktop/connection/persistent_connection.h"
16
16
17
17
#include < algorithm>
18
- #include < cassert>
19
18
#include < sstream>
20
19
21
20
#include " app/src/app_common.h"
@@ -118,9 +117,9 @@ PersistentConnection::PersistentConnection(
118
117
next_listen_id_(0 ),
119
118
next_write_id_(0 ),
120
119
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_);
124
123
125
124
// Create log id like "[pc_0]" for debugging
126
125
std::stringstream log_id_stream;
@@ -208,7 +207,7 @@ void PersistentConnection::OnReady(int64_t timestamp,
208
207
209
208
// Restore Auth
210
209
logger_->LogDebug (" %s calling restore state" , log_id_.c_str ());
211
- assert (connection_state_ == kConnecting );
210
+ FIREBASE_DEV_ASSERT (connection_state_ == kConnecting );
212
211
213
212
// Try to retrieve auth token synchronously when connection is ready.
214
213
GetAuthToken (&auth_token_);
@@ -247,26 +246,26 @@ void PersistentConnection::HandleConnectStatsResponse(
247
246
}
248
247
249
248
void PersistentConnection::OnDataMessage (const Variant& message) {
250
- assert (message.is_map ());
249
+ FIREBASE_DEV_ASSERT (message.is_map ());
251
250
252
251
SAFE_REFERENCE_RETURN_VOID_IF_INVALID (ThisRefLock, lock, safe_this_);
253
252
254
253
if (HasKey (message, kRequestNumber )) {
255
254
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 ());
257
256
uint64_t rn = it_request_number->second .int64_value ();
258
257
259
258
RequestDataPtr request_ptr;
260
259
auto it_request = request_map_.find (rn);
261
- assert (it_request != request_map_.end ());
260
+ FIREBASE_DEV_ASSERT (it_request != request_map_.end ());
262
261
if (it_request != request_map_.end ()) {
263
262
request_ptr = Move (it_request->second );
264
263
request_map_.erase (it_request);
265
264
}
266
- assert (request_ptr);
265
+ FIREBASE_DEV_ASSERT (request_ptr);
267
266
if (request_ptr) {
268
267
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 ());
270
269
if (it_response_message != message.map ().end ()) {
271
270
logger_->LogDebug (" %s Trigger handler for request %llu" ,
272
271
log_id_.c_str (), rn);
@@ -510,33 +509,41 @@ void PersistentConnection::TryScheduleReconnect() {
510
509
return ;
511
510
}
512
511
513
- assert (connection_state_ == kDisconnected );
512
+ FIREBASE_DEV_ASSERT (connection_state_ == kDisconnected );
514
513
bool force_refresh = force_auth_refresh_;
515
514
force_auth_refresh_ = false ;
516
515
logger_->LogDebug (" %s Scheduling connection attempt" , log_id_.c_str ());
517
516
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
+ }));
535
542
}
536
543
537
544
void PersistentConnection::OnTokenFutureComplete (
538
545
const Future<std::string>& result_data, void * user_data) {
539
- assert (user_data);
546
+ FIREBASE_DEV_ASSERT (user_data);
540
547
PersistentConnection* connection =
541
548
static_cast <PersistentConnection*>(user_data);
542
549
ThisRefLock lock (&connection->safe_this_ );
@@ -568,7 +575,7 @@ void PersistentConnection::HandleTokenFuture(Future<std::string> future) {
568
575
auth_token_ = *future.result ();
569
576
OpenNetworkConnection ();
570
577
} else {
571
- assert (connection_state_ == kDisconnected );
578
+ FIREBASE_DEV_ASSERT (connection_state_ == kDisconnected );
572
579
logger_->LogDebug (
573
580
" %s Not opening connection after token refresh, because "
574
581
" connection was set to disconnected" ,
@@ -583,7 +590,7 @@ void PersistentConnection::HandleTokenFuture(Future<std::string> future) {
583
590
}
584
591
585
592
void PersistentConnection::OpenNetworkConnection () {
586
- assert (connection_state_ == kGettingToken );
593
+ FIREBASE_DEV_ASSERT (connection_state_ == kGettingToken );
587
594
588
595
// User might have logged out. Positive auth status is handled after
589
596
// authenticating with the server
@@ -823,10 +830,10 @@ void PersistentConnection::PutInternal(const char* action, const Path& path,
823
830
}
824
831
825
832
void PersistentConnection::SendPut (uint64_t write_id) {
826
- assert (CanSendWrites ());
833
+ FIREBASE_DEV_ASSERT (CanSendWrites ());
827
834
828
835
auto it_put = outstanding_puts_.find (write_id);
829
- assert (it_put != outstanding_puts_.end ());
836
+ FIREBASE_DEV_ASSERT (it_put != outstanding_puts_.end ());
830
837
831
838
it_put->second ->MarkSent ();
832
839
SendSensitive (it_put->second ->action .c_str (), false , it_put->second ->data ,
@@ -908,7 +915,7 @@ void PersistentConnection::SendSensitive(const char* action, bool sensitive,
908
915
ResponsePtr response,
909
916
ConnectionResponseHandler callback,
910
917
uint64_t outstanding_id) {
911
- assert (realtime_);
918
+ FIREBASE_DEV_ASSERT (realtime_);
912
919
913
920
// Varient only accept int64_t
914
921
int64_t rn = ++next_request_id_;
@@ -924,7 +931,7 @@ void PersistentConnection::SendSensitive(const char* action, bool sensitive,
924
931
}
925
932
926
933
void PersistentConnection::RestoreOutstandingRequests () {
927
- assert (connection_state_ == kConnected );
934
+ FIREBASE_DEV_ASSERT (connection_state_ == kConnected );
928
935
929
936
// Restore listens
930
937
logger_->LogDebug (" %s Restoring outstanding listens" , log_id_.c_str ());
@@ -950,7 +957,7 @@ void PersistentConnection::RestoreOutstandingRequests() {
950
957
}
951
958
952
959
void PersistentConnection::GetAuthToken (std::string* out) {
953
- assert (out);
960
+ FIREBASE_DEV_ASSERT (out);
954
961
app_->function_registry ()->CallFunction (
955
962
::firebase::internal::FnAuthGetCurrentToken, app_, nullptr , out);
956
963
}
@@ -1010,7 +1017,7 @@ void PersistentConnection::SendUnauth() {
1010
1017
void PersistentConnection::HandleAuthTokenResponse (const Variant& message,
1011
1018
const ResponsePtr& response,
1012
1019
uint64_t outstanding_id) {
1013
- assert (response);
1020
+ FIREBASE_DEV_ASSERT (response);
1014
1021
1015
1022
connection_state_ = kConnected ;
1016
1023
0 commit comments