]> git.seodisparate.com - UDPConnection/commitdiff
Add way to specify sleep time for threaded update
authorStephen Seo <seo.disparate@gmail.com>
Wed, 13 Nov 2019 05:06:48 +0000 (14:06 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 13 Nov 2019 05:06:48 +0000 (14:06 +0900)
src/UDPC_Defines.hpp
src/UDPConnection.cpp
src/UDPConnection.h

index 785d9d59ec066520ad20e5f2e8f4c3cae1a9ea0e..20098e2100dde87fba671b8ae9ac70a044b9a136 100644 (file)
@@ -214,6 +214,8 @@ public:
     std::atomic_bool threadRunning;
     std::mutex mutex;
 
+    std::chrono::milliseconds threadedSleepTime;
+
 }; // struct Context
 
 Context *verifyContext(UDPC_HContext ctx);
index d75646edc9c40999e450f2f669375da4c78cf625..6ed6ac814ef797442fcd0dacc7a23d852e862406 100644 (file)
@@ -1205,7 +1205,7 @@ void UDPC::threadedUpdate(Context *ctx) {
         ctx->update_impl();
         ctx->mutex.unlock();
         nextNow = std::chrono::steady_clock::now();
-        std::this_thread::sleep_for(std::chrono::milliseconds(8) - (nextNow - now));
+        std::this_thread::sleep_for(ctx->threadedSleepTime - (nextNow - now));
     }
 }
 
@@ -1336,6 +1336,28 @@ UDPC_HContext UDPC_init_threaded_update(UDPC_ConnectionId listenId,
         return nullptr;
     }
     ctx->flags.set(0);
+    ctx->threadedSleepTime = std::chrono::milliseconds(8);
+    ctx->thread = std::thread(UDPC::threadedUpdate, ctx);
+
+    UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_INFO, "Initialized threaded UDPC");
+
+    return (UDPC_HContext) ctx;
+}
+
+UDPC_HContext UDPC_init_threaded_update_ms(
+        UDPC_ConnectionId listenId, int isClient, int updateMS) {
+    UDPC::Context *ctx = (UDPC::Context *)UDPC_init(listenId, isClient);
+    if(!ctx) {
+        return nullptr;
+    }
+    ctx->flags.set(0);
+    if(updateMS < 4) {
+        ctx->threadedSleepTime = std::chrono::milliseconds(4);
+    } else if(updateMS > 333) {
+        ctx->threadedSleepTime = std::chrono::milliseconds(333);
+    } else {
+        ctx->threadedSleepTime = std::chrono::milliseconds(updateMS);
+    }
     ctx->thread = std::thread(UDPC::threadedUpdate, ctx);
 
     UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_INFO, "Initialized threaded UDPC");
index e9d41bd6ec7dd096925949db100280bc76cbf96d..ea3dba80442d8088c428959796e13afccb78a278 100644 (file)
@@ -114,8 +114,13 @@ UDPC_ConnectionId UDPC_create_id_anyaddr(uint16_t port);
 UDPC_ConnectionId UDPC_create_id_easy(const char *addrString, uint16_t port);
 
 UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient);
-UDPC_HContext UDPC_init_threaded_update(UDPC_ConnectionId listenId,
-                                int isClient);
+UDPC_HContext UDPC_init_threaded_update(
+    UDPC_ConnectionId listenId,
+    int isClient);
+UDPC_HContext UDPC_init_threaded_update_ms(
+    UDPC_ConnectionId listenId,
+    int isClient,
+    int updateMS);
 
 void UDPC_destroy(UDPC_HContext ctx);