*/
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
std::mutex setThreadedUpdateMutex;
std::atomic_uint32_t enableDisableFuncRunningCount;
+ std::chrono::milliseconds heartbeatDuration;
+ std::shared_mutex heartbeatMutex;
+
}; // struct Context
Context *verifyContext(UDPC_HContext ctx);
atostrBufIndexMutex(),
atostrBufIndex(0),
setThreadedUpdateMutex(),
-enableDisableFuncRunningCount(0)
+enableDisableFuncRunningCount(0),
+heartbeatDuration(UDPC::HEARTBEAT_PKT_INTERVAL_DT),
+heartbeatMutex()
{
std::memset(atostrBuf, 0, UDPC_ATOSTR_SIZE);
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;
}
}
+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;