@@ -202,19 +202,21 @@ void Countly::start(const std::string& app_key, const std::string& host, int por
202202 }
203203
204204 if (!running) {
205+
206+ mutex.unlock ();
207+ beginSession ();
208+ mutex.lock ();
209+
205210 if (start_thread) {
206211 stop_thread = false ;
212+
207213 try {
208214 thread = new std::thread (&Countly::updateLoop, this );
209215 } catch (const std::system_error& e) {
210216 std::ostringstream log_message;
211217 log_message << " Could not create thread: " << e.what ();
212218 log (Countly::LogLevel::FATAL, log_message.str ());
213219 }
214- } else {
215- mutex.unlock ();
216- beginSession ();
217- mutex.lock ();
218220 }
219221 }
220222 mutex.unlock ();
@@ -400,7 +402,7 @@ bool Countly::beginSession() {
400402 }
401403
402404 if (sendHTTP (" /i" , Countly::serializeForm (data)).success ) {
403- last_sent = Countly::getTimestamp ();
405+ last_sent_session_request = Countly::getTimestamp ();
404406 began_session = true ;
405407 }
406408
@@ -415,8 +417,6 @@ bool Countly::beginSession() {
415417}
416418
417419bool Countly::updateSession () {
418- log (Countly::LogLevel::INFO, " [Countly][updateSession]" );
419-
420420 mutex.lock ();
421421 if (!began_session) {
422422 mutex.unlock ();
@@ -484,39 +484,35 @@ bool Countly::updateSession() {
484484 auto duration = std::chrono::duration_cast<std::chrono::seconds>(getSessionDuration ());
485485 mutex.lock ();
486486
487- if (no_events) {
488- if (duration.count () > COUNTLY_KEEPALIVE_INTERVAL) {
489- std::map<std::string, std::string> data = {
490- {" app_key" , session_params[" app_key" ].get <std::string>()},
491- {" device_id" , session_params[" device_id" ].get <std::string>()},
492- {" session_duration" , std::to_string (duration.count ())}
493- };
494- if (!sendHTTP (" /i" , Countly::serializeForm (data)).success ) {
495- mutex.unlock ();
496- return false ;
497- }
498-
499- last_sent += duration;
487+ if (duration.count () >= _auto_session_update_interval) {
488+ log (Countly::LogLevel::DEBUG, " [Countly][updateSession] sending session update." );
489+ std::map<std::string, std::string> data = {
490+ {" app_key" , session_params[" app_key" ].get <std::string>()},
491+ {" device_id" , session_params[" device_id" ].get <std::string>()},
492+ {" session_duration" , std::to_string (duration.count ())}
493+ };
494+ if (!sendHTTP (" /i" , Countly::serializeForm (data)).success ) {
495+ mutex.unlock ();
496+ return false ;
500497 }
501498
502- mutex.unlock ();
503- return true ;
499+ last_sent_session_request += duration;
504500 }
505501
506- std::map<std::string, std::string> data = {
502+ if (!no_events) {
503+ log (Countly::LogLevel::DEBUG, " [Countly][updateSession] sending event." );
504+ std::map<std::string, std::string> data = {
507505 {" app_key" , session_params[" app_key" ].get <std::string>()},
508506 {" device_id" , session_params[" device_id" ].get <std::string>()},
509- {" session_duration" , std::to_string (duration.count ())},
510507 {" events" , events.dump ()}
511- };
508+ };
512509
513- if (!sendHTTP (" /i" , Countly::serializeForm (data)).success ) {
514- mutex.unlock ();
515- return false ;
510+ if (!sendHTTP (" /i" , Countly::serializeForm (data)).success ) {
511+ mutex.unlock ();
512+ return false ;
513+ }
516514 }
517515
518- last_sent += duration;
519-
520516#ifndef COUNTLY_USE_SQLITE
521517 event_queue.clear ();
522518#else
@@ -554,7 +550,7 @@ bool Countly::endSession() {
554550 {" end_session" , " 1" }
555551 };
556552 if (sendHTTP (" /i" , Countly::serializeForm (data)).success ) {
557- last_sent = now;
553+ last_sent_session_request = now;
558554 began_session = false ;
559555 mutex.unlock ();
560556 return true ;
@@ -804,7 +800,7 @@ log(Countly::LogLevel::DEBUG, "[Countly][sendHTTP] data: "+ data);
804800
805801std::chrono::system_clock::duration Countly::getSessionDuration (std::chrono::system_clock::time_point now) {
806802 mutex.lock ();
807- std::chrono::system_clock::duration duration = now - last_sent ;
803+ std::chrono::system_clock::duration duration = now - last_sent_session_request ;
808804 mutex.unlock ();
809805 return duration;
810806}
@@ -827,8 +823,8 @@ void Countly::updateLoop() {
827823 }
828824 size_t last_wait_milliseconds = wait_milliseconds;
829825 mutex.unlock ();
830- updateSession ();
831826 std::this_thread::sleep_for (std::chrono::milliseconds (last_wait_milliseconds));
827+ updateSession ();
832828 }
833829 mutex.lock ();
834830 running = false ;
0 commit comments