From 4b7ef79f383991da59fa8d38e3b7ff19b76b06c1 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 10 Apr 2024 12:06:11 +0200 Subject: [PATCH] improve tox client (prevent excessive saving) and reorder construction/destruction order of plugin manager in main --- src/main.cpp | 5 +++-- src/tox_client.cpp | 9 +++++++-- src/tox_client.hpp | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 235bb43..c999f85 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -198,7 +198,6 @@ int main(int argc, char** argv) { } - PluginManager pm; ToxEventLogger tel{std::cout}; // TODO: config @@ -222,6 +221,8 @@ int main(int argc, char** argv) { ToxMessageManager tmm{rmm, cr, tcm, tc, tc}; ToxTransferManager ttm{rmm, cr, tcm, tc, tc}; + PluginManager pm; + { // setup plugin instances g_provideInstance("ConfigModelI", "host", &conf); g_provideInstance("Contact3Registry", "1", "host", &cr); @@ -313,7 +314,7 @@ int main(int argc, char** argv) { const bool tick = time_delta_tick >= last_min_interval; if (tick) { - quit = !tc.iterate(); + quit = !tc.iterate(time_delta_tick); tcm.iterate(time_delta_tick); ttm.iterate(); diff --git a/src/tox_client.cpp b/src/tox_client.cpp index adaa172..5ff40dd 100644 --- a/src/tox_client.cpp +++ b/src/tox_client.cpp @@ -155,10 +155,13 @@ ToxClient::ToxClient(ConfigModelI& conf, std::string_view save_path, std::string } ToxClient::~ToxClient(void) { + if (_tox_profile_dirty) { + saveToxProfile(); + } tox_kill(_tox); } -bool ToxClient::iterate(void) { +bool ToxClient::iterate(float time_delta) { Tox_Err_Events_Iterate err_e_it = TOX_ERR_EVENTS_ITERATE_OK; auto* events = tox_events_iterate(_tox, false, &err_e_it); if (err_e_it == TOX_ERR_EVENTS_ITERATE_OK && events != nullptr) { @@ -170,7 +173,8 @@ bool ToxClient::iterate(void) { tox_events_free(events); - if (_tox_profile_dirty) { + _save_heat -= time_delta; + if (_tox_profile_dirty && _save_heat <= 0.f) { saveToxProfile(); } @@ -214,5 +218,6 @@ void ToxClient::saveToxProfile(void) { } _tox_profile_dirty = false; + _save_heat = 10.f; } diff --git a/src/tox_client.hpp b/src/tox_client.hpp index 12d5750..66cf99f 100644 --- a/src/tox_client.hpp +++ b/src/tox_client.hpp @@ -24,6 +24,7 @@ class ToxClient : public ToxDefaultImpl, public ToxEventProviderBase { std::string _tox_profile_path; std::string _tox_profile_password; bool _tox_profile_dirty {true}; // set in callbacks + float _save_heat {0.f}; public: ToxClient(ConfigModelI& conf, std::string_view save_path, std::string_view save_password); @@ -35,7 +36,7 @@ class ToxClient : public ToxDefaultImpl, public ToxEventProviderBase { void setDirty(void) { _tox_profile_dirty = true; } // returns false when we shoul stop the program - bool iterate(void); + bool iterate(float time_delta); void stop(void); // let it know it should exit void setToxProfilePath(const std::string& new_path) { _tox_profile_path = new_path; } @@ -48,4 +49,3 @@ class ToxClient : public ToxDefaultImpl, public ToxEventProviderBase { void saveToxProfile(void); }; -