From 209743fd554df410d5001d79c7a1915c329d736e Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 11 Jun 2018 05:44:06 +0200 Subject: [PATCH] Check for new messages every 2 seconds (was: 5), made the connection-brokenness-detection easier to understand --- CMakeLists.txt | 2 +- src/main.cpp | 2 +- src/masto.cpp | 20 ++++++++++---------- src/url.cpp | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f484caf..b47e369 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (expandurl-mastodon - VERSION 0.9.6 + VERSION 0.9.7 LANGUAGES CXX ) diff --git a/src/main.cpp b/src/main.cpp index d79486d..2e4bb62 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -71,7 +71,7 @@ int main(int argc, char *argv[]) while (running) { - std::this_thread::sleep_for(std::chrono::seconds(5)); + std::this_thread::sleep_for(std::chrono::seconds(2)); if (!listener.stillrunning()) { listener.stop(); diff --git a/src/masto.cpp b/src/masto.cpp index 0d776a5..9bbcd02 100644 --- a/src/masto.cpp +++ b/src/masto.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include "version.hpp" #include "expandurl-mastodon.hpp" @@ -139,16 +140,15 @@ const void Listener::stop() const std::vector Listener::get_new_messages() { + using namespace std::chrono; + std::vector v; - static std::uint_fast8_t count_empty = 0; + static system_clock::time_point lastping = system_clock::now(); std::lock_guard lock(_ptr->get_mutex()); if (!_stream.empty()) { - const string buffer = _stream; - _stream.clear(); - - for (const Easy::stream_event &event : Easy::parse_stream(buffer)) + for (const Easy::stream_event &event : Easy::parse_stream(_stream)) { if (event.first == Easy::event_type::Notification) { @@ -159,15 +159,15 @@ const std::vector Listener::get_new_messages() } } } - count_empty = 0; + _stream.clear(); + lastping = system_clock::now(); } else { - // If we got an empty message 5 times, set state to not running - ++count_empty; - if (count_empty > 5) + // If the last keep-alive packet was received 25 seconds or more ago + if (duration_cast(lastping - system_clock::now()).count() >= 25) { - count_empty = 0; + lastping = system_clock::now(); syslog(LOG_NOTICE, "Detected broken connection."); _running = false; } diff --git a/src/url.cpp b/src/url.cpp index efc376f..7e36294 100644 --- a/src/url.cpp +++ b/src/url.cpp @@ -110,7 +110,7 @@ const void init_replacements() const std::array replace_array = {{ { "[\\?&]utm_[^&]+", "" }, // Google - { "[\\?&]wt_?[^&]+", "" }, // Twitter? + { "[\\?&]wt_?[^&]+", "" }, // Twitter? { "[\\?&]__twitter_impression=[^&]+", "" }, // Twitter? { "//amp\\.", "//" } // AMP }};