]> git.seodisparate.com - UDPConnection/commitdiff
Add function to change heartbeat interval
authorStephen Seo <seo.disparate@gmail.com>
Wed, 12 Mar 2025 06:21:57 +0000 (15:21 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 12 Mar 2025 06:35:02 +0000 (15:35 +0900)
src/UDPC.h
src/UDPC_Defines.hpp
src/UDPConnection.cpp

index 7c87c69fb331b8b4ce959a38cc688bf1f6762143..f2ac87f545ec91b8d5cddb4d3bcc9de341fef82a 100644 (file)
@@ -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
 
index a9cca9787903354c79a03bdb17622031acb990e2..cefaa9e6b8598c9b528e27962d5e025642faed2b 100644 (file)
@@ -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);
index b3edfc71ad9cd5b76e6e4109dcf5cb7f07346eed..0e118f444370ec71c12adfe7217aad16a5602712 100644 (file)
@@ -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<std::shared_mutex> 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<std::shared_mutex> 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;