@@ -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 ();
@@ -240,6 +242,7 @@ void Countly::stop() {
240242 log (Countly::LogLevel::WARNING, " Could not join thread" );
241243 }
242244 delete thread;
245+ thread = nullptr ;
243246 }
244247 if (began_session) {
245248 endSession ();
@@ -400,7 +403,7 @@ bool Countly::beginSession() {
400403 }
401404
402405 if (sendHTTP (" /i" , Countly::serializeForm (data)).success ) {
403- last_sent = Countly::getTimestamp ();
406+ last_sent_session_request = Countly::getTimestamp ();
404407 began_session = true ;
405408 }
406409
@@ -415,8 +418,6 @@ bool Countly::beginSession() {
415418}
416419
417420bool Countly::updateSession () {
418- log (Countly::LogLevel::INFO, " [Countly][updateSession]" );
419-
420421 mutex.lock ();
421422 if (!began_session) {
422423 mutex.unlock ();
@@ -484,39 +485,35 @@ bool Countly::updateSession() {
484485 auto duration = std::chrono::duration_cast<std::chrono::seconds>(getSessionDuration ());
485486 mutex.lock ();
486487
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;
488+ if (duration.count () >= _auto_session_update_interval) {
489+ log (Countly::LogLevel::DEBUG, " [Countly][updateSession] sending session update." );
490+ std::map<std::string, std::string> data = {
491+ {" app_key" , session_params[" app_key" ].get <std::string>()},
492+ {" device_id" , session_params[" device_id" ].get <std::string>()},
493+ {" session_duration" , std::to_string (duration.count ())}
494+ };
495+ if (!sendHTTP (" /i" , Countly::serializeForm (data)).success ) {
496+ mutex.unlock ();
497+ return false ;
500498 }
501499
502- mutex.unlock ();
503- return true ;
500+ last_sent_session_request += duration;
504501 }
505502
506- std::map<std::string, std::string> data = {
503+ if (!no_events) {
504+ log (Countly::LogLevel::DEBUG, " [Countly][updateSession] sending event." );
505+ std::map<std::string, std::string> data = {
507506 {" app_key" , session_params[" app_key" ].get <std::string>()},
508507 {" device_id" , session_params[" device_id" ].get <std::string>()},
509- {" session_duration" , std::to_string (duration.count ())},
510508 {" events" , events.dump ()}
511- };
509+ };
512510
513- if (!sendHTTP (" /i" , Countly::serializeForm (data)).success ) {
514- mutex.unlock ();
515- return false ;
511+ if (!sendHTTP (" /i" , Countly::serializeForm (data)).success ) {
512+ mutex.unlock ();
513+ return false ;
514+ }
516515 }
517516
518- last_sent += duration;
519-
520517#ifndef COUNTLY_USE_SQLITE
521518 event_queue.clear ();
522519#else
@@ -554,7 +551,7 @@ bool Countly::endSession() {
554551 {" end_session" , " 1" }
555552 };
556553 if (sendHTTP (" /i" , Countly::serializeForm (data)).success ) {
557- last_sent = now;
554+ last_sent_session_request = now;
558555 began_session = false ;
559556 mutex.unlock ();
560557 return true ;
@@ -659,20 +656,21 @@ log(Countly::LogLevel::DEBUG, "[Countly][sendHTTP] data: "+ data);
659656 data += " checksum256=" ;
660657 data += checksum_stream.str ();
661658 }
659+
660+ Countly::HTTPResponse response;
661+ response.success = false ;
662+
662663#ifdef COUNTLY_USE_CUSTOM_HTTP
663664 if (http_client_function == nullptr ) {
664665 log (Countly::LogLevel::FATAL, " Missing HTTP client function" );
665- return false ;
666+ return response ;
666667 }
667668
668669 return http_client_function (use_post, path, data);
669670#else
670671 if (http_client_function != nullptr ) {
671672 return http_client_function (use_post, path, data);
672673 }
673-
674- Countly::HTTPResponse response;
675- response.success = false ;
676674#ifdef _WIN32
677675 HINTERNET hSession;
678676 HINTERNET hConnect;
@@ -804,7 +802,7 @@ log(Countly::LogLevel::DEBUG, "[Countly][sendHTTP] data: "+ data);
804802
805803std::chrono::system_clock::duration Countly::getSessionDuration (std::chrono::system_clock::time_point now) {
806804 mutex.lock ();
807- std::chrono::system_clock::duration duration = now - last_sent ;
805+ std::chrono::system_clock::duration duration = now - last_sent_session_request ;
808806 mutex.unlock ();
809807 return duration;
810808}
@@ -827,9 +825,8 @@ void Countly::updateLoop() {
827825 }
828826 size_t last_wait_milliseconds = wait_milliseconds;
829827 mutex.unlock ();
830- updateSession ();
831828 std::this_thread::sleep_for (std::chrono::milliseconds (last_wait_milliseconds));
832-
829+ updateSession ();
833830 }
834831 mutex.lock ();
835832 running = false ;
0 commit comments