@@ -346,7 +346,7 @@ void Countly::_changeDeviceIdWithoutMerge(const std::string &value) {
346346
347347 // send all event to server and end current session of old user
348348 flushEvents ();
349- if (! configuration->manualSessionControl ){
349+ if (configuration->manualSessionControl == false ){
350350 endSession ();
351351 }
352352
@@ -355,7 +355,7 @@ void Countly::_changeDeviceIdWithoutMerge(const std::string &value) {
355355 mutex->unlock ();
356356
357357 // start a new session for new user
358- if (! configuration->manualSessionControl ){
358+ if (configuration->manualSessionControl == false ){
359359 beginSession ();
360360 }
361361
@@ -403,7 +403,7 @@ void Countly::start(const std::string &app_key, const std::string &host, int por
403403 log (LogLevel::INFO, " [Countly][start] '_WIN32' is not defined" );
404404#endif
405405
406- enable_automatic_session = start_thread && !configuration-> manualSessionControl ;
406+ enable_automatic_session = start_thread;
407407 start_thread = true ;
408408
409409 if (port < 0 || port > 65535 ) {
@@ -438,7 +438,7 @@ void Countly::start(const std::string &app_key, const std::string &host, int por
438438
439439 if (!running) {
440440
441- if (! configuration->manualSessionControl ){
441+ if (configuration->manualSessionControl == false ){
442442 mutex->unlock ();
443443 beginSession ();
444444 mutex->lock ();
@@ -469,7 +469,7 @@ void Countly::startOnCloud(const std::string &app_key) {
469469
470470void Countly::stop () {
471471 _deleteThread ();
472- if (! configuration->manualSessionControl ) {
472+ if (configuration->manualSessionControl == false ) {
473473 endSession ();
474474 }
475475}
@@ -611,16 +611,12 @@ bool Countly::attemptSessionUpdateEQ() {
611611 return false ;
612612 }
613613#endif
614- bool result;
615- if (!configuration->manualSessionControl ){
616- result = !updateSession ();
614+ if (configuration->manualSessionControl == false ){
615+ return !updateSession ();
617616 } else {
618- log (LogLevel::WARNING, " [Countly][attemptSessionUpdateEQ] SDK is in manual session control mode. Please start a session first. " );
619- result = false ;
617+ packEvents ( );
618+ return false ;
620619 }
621-
622- packEvents ();
623- return result;
624620}
625621
626622void Countly::clearEQInternal () {
@@ -680,7 +676,7 @@ std::vector<std::string> Countly::debugReturnStateOfEQ() {
680676bool Countly::beginSession () {
681677 mutex->lock ();
682678 log (LogLevel::INFO, " [Countly][beginSession]" );
683- if (began_session) {
679+ if (began_session == true ) {
684680 mutex->unlock ();
685681 log (LogLevel::DEBUG, " [Countly][beginSession] Session is already active." );
686682 return true ;
@@ -736,9 +732,9 @@ bool Countly::updateSession() {
736732 try {
737733 // Check if there was a session, if not try to start one
738734 mutex->lock ();
739- if (! began_session) {
735+ if (began_session == false ) {
740736 mutex->unlock ();
741- if (configuration->manualSessionControl ){
737+ if (configuration->manualSessionControl == true ){
742738 log (LogLevel::WARNING, " [Countly][updateSession] SDK is in manual session control mode and there is no active session. Please start a session first." );
743739 return false ;
744740 }
@@ -749,7 +745,28 @@ bool Countly::updateSession() {
749745 mutex->lock ();
750746 began_session = true ;
751747 }
752-
748+
749+ // events array
750+ nlohmann::json events = nlohmann::json::array ();
751+ std::string event_ids;
752+ mutex->unlock ();
753+ bool no_events = checkEQSize () > 0 ? false : true ;
754+ mutex->lock ();
755+
756+ if (!no_events) {
757+ #ifndef COUNTLY_USE_SQLITE
758+ for (const auto &event_json : event_queue) {
759+ events.push_back (nlohmann::json::parse (event_json));
760+ }
761+ #else
762+ // TODO: If database_path was empty there was return false here
763+ mutex->unlock ();
764+ fillEventsIntoJson (events, event_ids);
765+ mutex->lock ();
766+ #endif
767+ } else {
768+ log (LogLevel::DEBUG, " [Countly][updateSession] EQ empty." );
769+ }
753770 mutex->unlock ();
754771 auto duration = std::chrono::duration_cast<std::chrono::seconds>(getSessionDuration ());
755772 mutex->lock ();
@@ -762,6 +779,22 @@ bool Countly::updateSession() {
762779
763780 last_sent_session_request += duration;
764781 }
782+
783+ // report events if there are any to request queue
784+ if (!no_events) {
785+ sendEventsToRQ (events);
786+ }
787+
788+ // clear event queue
789+ // TODO: check if we want to totally wipe the event queue in memory but not in database
790+ #ifndef COUNTLY_USE_SQLITE
791+ event_queue.clear ();
792+ #else
793+ if (!event_ids.empty ()) {
794+ // this is a partial clearance, we only remove the events that were sent
795+ removeEventWithId (event_ids);
796+ }
797+ #endif
765798 } catch (const std::system_error &e) {
766799 std::ostringstream log_message;
767800 log_message << " update session, error: " << e.what ();
@@ -792,7 +825,7 @@ void Countly::packEvents() {
792825 mutex->lock ();
793826#endif
794827 } else {
795- log (LogLevel::DEBUG, " [Countly][updateSession ] EQ empty." );
828+ log (LogLevel::DEBUG, " [Countly][packEvents ] EQ empty." );
796829 }
797830 // report events if there are any to request queue
798831 if (!no_events) {
@@ -811,7 +844,7 @@ void Countly::packEvents() {
811844#endif
812845 } catch (const std::system_error &e) {
813846 std::ostringstream log_message;
814- log_message << " update session , error: " << e.what ();
847+ log_message << " packEvents , error: " << e.what ();
815848 log (LogLevel::FATAL, log_message.str ());
816849 }
817850 mutex->unlock ();
@@ -826,7 +859,7 @@ void Countly::sendEventsToRQ(const nlohmann::json &events) {
826859
827860bool Countly::endSession () {
828861 log (LogLevel::INFO, " [Countly][endSession]" );
829- if (! began_session) {
862+ if (began_session == false ) {
830863 log (LogLevel::DEBUG, " [Countly][endSession] There is no active session to end." );
831864 return true ;
832865 }
@@ -1167,10 +1200,11 @@ void Countly::updateLoop() {
11671200 size_t last_wait_milliseconds = wait_milliseconds;
11681201 mutex->unlock ();
11691202 std::this_thread::sleep_for (std::chrono::milliseconds (last_wait_milliseconds));
1170- if (enable_automatic_session) {
1203+ if (enable_automatic_session == true && configuration-> manualSessionControl == false ) {
11711204 updateSession ();
1205+ } else if (configuration->manualSessionControl == true ) {
1206+ packEvents ();
11721207 }
1173- packEvents ();
11741208 requestModule->processQueue (mutex);
11751209 }
11761210 mutex->lock ();
0 commit comments