]> git.seodisparate.com - UDPConnection/commitdiff
Add getters (heartbeat and timeout), unit tests
authorStephen Seo <seo.disparate@gmail.com>
Thu, 13 Mar 2025 05:50:30 +0000 (14:50 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 13 Mar 2025 05:50:30 +0000 (14:50 +0900)
src/UDPC.h
src/UDPConnection.cpp
src/test/TestUDPC.cpp

index ba08061558cb53761e3be526e57d85be4d572985..d328261105f70668d442184388be82f1859fd92f 100644 (file)
@@ -929,6 +929,14 @@ UDPC_EXPORT void UDPC_atostr_unsafe_free(const char *addrBuf);
  */
 UDPC_EXPORT void UDPC_atostr_unsafe_free_ptr(const char **addrBuf);
 
+/*!
+ * \brief Gets the currently set amount of time between heartbeat packets in
+ * milliseconds.
+ *
+ * \return 0 if context is invalid, otherwise the interval time in milliseconds.
+ */
+UDPC_EXPORT uint32_t UDPC_get_heartbeat_millis(UDPC_HContext ctx);
+
 /*!
  * \brief Sets the amount of time between heartbeat packets.
  *
@@ -954,6 +962,13 @@ UDPC_EXPORT void UDPC_atostr_unsafe_free_ptr(const char **addrBuf);
  */
 UDPC_EXPORT int UDPC_set_heartbeat_millis(UDPC_HContext ctx, unsigned int millis);
 
+/*!
+ * \brief Gets the currently set connection timeout time in milliseconds.
+ *
+ * \return 0 if context is invalid, otherwise the timeout time in milliseconds.
+ */
+UDPC_EXPORT uint64_t UDPC_get_con_timeout_millis(UDPC_HContext ctx);
+
 /*!
  * \brief Sets the connection timeout time.
  *
index 3ea7d857e85a494d814f45b55df55dafd7086cd7..06f62862757262d52ff2c22cb3f8a6fc7048c585 100644 (file)
@@ -2846,6 +2846,18 @@ void UDPC_atostr_unsafe_free_ptr(const char **addrBuf) {
     }
 }
 
+uint32_t UDPC_get_heartbeat_millis(UDPC_HContext ctx) {
+    UDPC::Context *c = UDPC::verifyContext(ctx);
+    if (!c) {
+        return 0;
+    }
+
+    std::shared_lock<std::shared_mutex> lock(c->heartbeatMutex);
+    // The milliseconds are clamped to a maximum for heartbeat, so it should
+    // fit in a 32-bit unsigned integer.
+    return c->heartbeatDuration.count();
+}
+
 int UDPC_set_heartbeat_millis(UDPC_HContext ctx, unsigned int millis) {
     UDPC::Context *c = UDPC::verifyContext(ctx);
     if (!c) {
@@ -2878,6 +2890,16 @@ int UDPC_set_heartbeat_millis(UDPC_HContext ctx, unsigned int millis) {
     return ret;
 }
 
+uint64_t UDPC_get_con_timeout_millis(UDPC_HContext ctx) {
+    UDPC::Context *c = UDPC::verifyContext(ctx);
+    if (!c) {
+        return 0;
+    }
+
+    std::shared_lock<std::shared_mutex> lock(c->conTimeoutMutex);
+    return c->conTimeoutDuration.count();
+}
+
 int UDPC_set_con_timeout_millis(UDPC_HContext ctx, unsigned int millis) {
     UDPC::Context *c = UDPC::verifyContext(ctx);
     if (!c) {
index 0bb3dfa96b481ec55c3cf47cf473e3e65c137cbf..9a7a3de6128c023c81faa7b613f242f5ff3ccba1 100644 (file)
@@ -475,4 +475,34 @@ void TEST_UDPC() {
             thread_array[i].join();
         }
     }
+
+    // Test get/set heartbeat millis
+    {
+        UDPC_ConnectionId id = UDPC_create_id_anyaddr(0);
+        UDPC_HContext ctx = UDPC_init(id, 0, 0);
+
+        CHECK_TRUE(UDPC_get_heartbeat_millis(ctx) == UDPC::HEARTBEAT_PKT_INT_MIN_MILLIS);
+        CHECK_TRUE(UDPC_set_heartbeat_millis(ctx, 1) == 1);
+        CHECK_TRUE(UDPC_get_heartbeat_millis(ctx) == UDPC::HEARTBEAT_PKT_INT_MIN_MILLIS);
+        CHECK_TRUE(UDPC_set_heartbeat_millis(ctx, 10000) == 2);
+        CHECK_TRUE(UDPC_get_heartbeat_millis(ctx) == UDPC::HEARTBEAT_PKT_INT_MAX_MILLIS);
+        CHECK_TRUE(UDPC_set_heartbeat_millis(ctx, 2000) == 0);
+        CHECK_TRUE(UDPC_get_heartbeat_millis(ctx) == 2000);
+
+        UDPC_destroy(ctx);
+    }
+
+    // Test get/set connection timeout millis
+    {
+        UDPC_ConnectionId id = UDPC_create_id_anyaddr(0);
+        UDPC_HContext ctx = UDPC_init(id, 0, 0);
+
+        CHECK_TRUE(UDPC_get_con_timeout_millis(ctx) == UDPC_CON_TIMEOUT_DEFAULT);
+        CHECK_TRUE(UDPC_set_con_timeout_millis(ctx, 1) == -2);
+        CHECK_TRUE(UDPC_get_con_timeout_millis(ctx) == UDPC_CON_TIMEOUT_DEFAULT);
+        CHECK_TRUE(UDPC_set_con_timeout_millis(ctx, 7000) == 0);
+        CHECK_TRUE(UDPC_get_con_timeout_millis(ctx) == 7000);
+
+        UDPC_destroy(ctx);
+    }
 }