From d8ce016076eea3894f889bf52426798cd72ba3cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Stoj=C5=A1in?= Date: Sat, 22 Jun 2024 08:54:12 +0200 Subject: [PATCH] Sync clock periodically - sync cycle is clock_sync_freq_sec setting --- .../client/client_comp_system_message.hpp | 33 +++++++++++++++++-- .../net/core/client/client_context.hpp | 1 + 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/include/libnetwrk/net/core/client/client_comp_system_message.hpp b/include/libnetwrk/net/core/client/client_comp_system_message.hpp index 1f56bf7..6f6993c 100644 --- a/include/libnetwrk/net/core/client/client_comp_system_message.hpp +++ b/include/libnetwrk/net/core/client/client_comp_system_message.hpp @@ -1,7 +1,10 @@ #pragma once +#include "asio.hpp" +#include "asio/experimental/awaitable_operators.hpp" #include "libnetwrk/net/misc/authentication.hpp" #include "libnetwrk/net/core/client/client_comp_message.hpp" +#include "libnetwrk/net/misc/coroutine_cv.hpp" namespace libnetwrk { template @@ -36,6 +39,30 @@ namespace libnetwrk { context_t& m_context; comp_message_t& m_comp_message; + private: + + private: + void start_clock_syncing() { + using namespace asio::experimental::awaitable_operators; + + asio::co_spawn(m_context.io_context, co_clock_sync() || m_context.cancel_cv.wait(), [this](auto, auto) { + LIBNETWRK_DEBUG(m_context.name, "Stopped clock syncing."); + }); + } + + asio::awaitable co_clock_sync() { + asio::steady_timer timer(m_context.io_context, std::chrono::seconds(m_context.settings.clock_sync_freq_sec)); + + while (true) { + send_clock_sync_request(0U); + + timer.expires_after(std::chrono::seconds(m_context.settings.clock_sync_freq_sec)); + + auto [ec] = co_await timer.async_wait(asio::as_tuple(asio::use_awaitable)); + if (ec) break; + } + } + private: void ev_system_message(system_command command, owned_message_t* message) { switch (command) { @@ -71,12 +98,10 @@ namespace libnetwrk { connection->is_authenticated = true; connection->write_cv.notify_all(); - send_clock_sync_request(0U); + start_clock_syncing(); } void on_system_clock_sync_message(owned_message_t* message) { - LIBNETWRK_DEBUG(m_context.name, "Received clock sync response."); - uint8_t sample_index = 0U; uint64_t client_timestamp = 0U; uint64_t service_timestamp = 0U; @@ -131,6 +156,8 @@ namespace libnetwrk { m_context.clock_drift_samples_received = 0U; m_context.clock_drift_samples = {}; + + LIBNETWRK_DEBUG(m_context.name, "Clock synced."); } }; } diff --git a/include/libnetwrk/net/core/client/client_context.hpp b/include/libnetwrk/net/core/client/client_context.hpp index 61863b7..cdb4575 100644 --- a/include/libnetwrk/net/core/client/client_context.hpp +++ b/include/libnetwrk/net/core/client/client_context.hpp @@ -6,6 +6,7 @@ namespace libnetwrk { struct client_settings { + uint16_t clock_sync_freq_sec = 120U; }; template