Skip to content

Commit 6546af0

Browse files
committed
Merge branch 'staging' of https://github.com/Countly/countly-sdk-cpp into sdk-621
# Conflicts: # src/countly.cpp
2 parents 5cb9a5c + 090a931 commit 6546af0

File tree

4 files changed

+49
-41
lines changed

4 files changed

+49
-41
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
21.11.1
2+
* ! Minor breaking change ! Default automatic session update duration changed to 60 seconds.
3+
* Added a call to change the automatic session update duration.
4+
* Fixed a bug where session duration was reported wrong.
5+
16
21.11.0
27
* Fixed session duration issue.
38
* Added functionality to report event duration manually.

examples/example_integration.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ int main() {
9393
}
9494
}
9595

96-
ct.updateSession();
97-
ct.stop();
96+
//ct.updateSession();
97+
ct.endSession();
9898

9999
return 0;
100100
}

include/countly.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ class Countly {
207207

208208
addEvent(event);
209209
}
210+
211+
/* Provide 'updateInterval' in seconds. */
212+
inline void setAutomaticSessionUpdateInterval(unsigned short updateInterval) {
213+
_auto_session_update_interval = updateInterval;
214+
}
210215
private:
211216
void log(LogLevel level, const std::string& message);
212217

@@ -225,7 +230,7 @@ class Countly {
225230
int port;
226231
bool use_https;
227232
bool always_use_post;
228-
std::chrono::system_clock::time_point last_sent;
233+
std::chrono::system_clock::time_point last_sent_session_request;
229234
bool began_session;
230235

231236
json session_params;
@@ -236,6 +241,7 @@ class Countly {
236241
bool stop_thread;
237242
bool running;
238243
size_t wait_milliseconds;
244+
unsigned short _auto_session_update_interval = 60; // value is in seconds;
239245

240246
size_t max_events;
241247
#ifndef COUNTLY_USE_SQLITE

src/countly.cpp

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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

417420
bool 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

805803
std::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

Comments
 (0)