From ac377f0bb844b116ed909ebea99b435b34b2559a Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 12 Mar 2025 15:21:57 +0900 Subject: [PATCH] Add function to change heartbeat interval --- src/UDPC.h | 19 +++++++++++++++++++ src/UDPC_Defines.hpp | 3 +++ src/UDPConnection.cpp | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/UDPC.h b/src/UDPC.h index 7c87c69..f2ac87f 100644 --- a/src/UDPC.h +++ b/src/UDPC.h @@ -915,6 +915,25 @@ UDPC_EXPORT void UDPC_atostr_unsafe_free(const char *addrBuf); */ UDPC_EXPORT void UDPC_atostr_unsafe_free_ptr(const char **addrBuf); +/*! + * \brief Sets the amount of time between heartbeat packets. + * + * By default, UDPC sends a heartbeat packet every 150 milliseconds. + * This function can be used to increase the heartbeat interval time. + * Valid values are between 150 to 5000 milliseconds. Any value outside of this + * range will be clamped to within this range. + * + * This function affects all connections associated with the given UDPC Context. + * + * This is useful for cases where low-latency is not required. Also note that + * increasing the heartbeat interval may prevent UDPC from entering "good mode" + * for any connection. + * + * \return 0 on success, 1 if clamped to minimum, 2 if clamped to maximum, -1 + * if the given context is invalid. + */ +UDPC_EXPORT int UDPC_set_heartbeat_millis(UDPC_HContext ctx, unsigned int millis); + // ============================================================================= // Helpers diff --git a/src/UDPC_Defines.hpp b/src/UDPC_Defines.hpp index a9cca97..cefaa9e 100644 --- a/src/UDPC_Defines.hpp +++ b/src/UDPC_Defines.hpp @@ -279,6 +279,9 @@ public: std::mutex setThreadedUpdateMutex; std::atomic_uint32_t enableDisableFuncRunningCount; + std::chrono::milliseconds heartbeatDuration; + std::shared_mutex heartbeatMutex; + }; // struct Context Context *verifyContext(UDPC_HContext ctx); diff --git a/src/UDPConnection.cpp b/src/UDPConnection.cpp index b3edfc7..0e118f4 100644 --- a/src/UDPConnection.cpp +++ b/src/UDPConnection.cpp @@ -259,7 +259,9 @@ keysSet(), atostrBufIndexMutex(), atostrBufIndex(0), setThreadedUpdateMutex(), -enableDisableFuncRunningCount(0) +enableDisableFuncRunningCount(0), +heartbeatDuration(UDPC::HEARTBEAT_PKT_INTERVAL_DT), +heartbeatMutex() { std::memset(atostrBuf, 0, UDPC_ATOSTR_SIZE); @@ -894,8 +896,11 @@ void UDPC::Context::update_impl() { if(iter->second.sendPkts.empty() && iter->second.priorityPkts.empty()) { // nothing in queues, send heartbeat packet auto sentDT = now - iter->second.sent; - if(sentDT < UDPC::HEARTBEAT_PKT_INTERVAL_DT) { - continue; + { + std::shared_lock lock(heartbeatMutex); + if(sentDT < heartbeatDuration) { + continue; + } } unsigned int sendSize = 0; @@ -2821,6 +2826,28 @@ void UDPC_atostr_unsafe_free_ptr(const char **addrBuf) { } } +int UDPC_set_heartbeat_millis(UDPC_HContext ctx, unsigned int millis) { + UDPC::Context *c = UDPC::verifyContext(ctx); + if (!c) { + return -1; + } + + int ret = 0; + + if (millis < 150) { + millis = 150; + ret = 1; + } else if (millis > 5000) { + millis = 5000; + ret = 2; + } + + std::unique_lock lock(c->heartbeatMutex); + c->heartbeatDuration = std::chrono::milliseconds(millis); + + return ret; +} + UDPC_IPV6_ADDR_TYPE UDPC_strtoa(const char *addrStr) { UDPC_IPV6_ADDR_TYPE result = in6addr_loopback; std::cmatch matchResults; -- 2.49.0