]> git.seodisparate.com - UDPConnection/commitdiff
Fix threaded update
authorStephen Seo <seo.disparate@gmail.com>
Tue, 5 Mar 2019 03:56:05 +0000 (12:56 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Tue, 5 Mar 2019 03:56:05 +0000 (12:56 +0900)
src/UDPConnection.c
src/test/UDPC_NetworkTest.c

index a3f810bc2d4604f8b74e81efd9418bb2fe8f5398..488ba1605372159bd6be0573522a8290e3c78bca 100644 (file)
@@ -126,7 +126,7 @@ UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort, int isClient)
     context->error = UDPC_SUCCESS;
 
     context->error = thrd_create(
-        &context->threadHandle, UDPC_INTERNAL_threadfn, &context);
+        &context->threadHandle, UDPC_INTERNAL_threadfn, context);
     if(context->error != thrd_success)
     {
         CleanupSocket(context->socketHandle);
@@ -304,6 +304,11 @@ void UDPC_check_events(UDPC_Context *ctx)
 
 void UDPC_client_initiate_connection(UDPC_Context *ctx, uint32_t addr, uint16_t port)
 {
+    if((ctx->flags & 0x1) != 0)
+    {
+        mtx_lock(&ctx->tCVMtx);
+    }
+
     if((ctx->flags & 0x2) == 0 || UDPC_HashMap_has(ctx->conMap, addr) != 0)
     {
         // must be client or no already-existing connection to same address
@@ -335,6 +340,11 @@ void UDPC_client_initiate_connection(UDPC_Context *ctx, uint32_t addr, uint16_t
     cd.sent.tv_sec -= UDPC_INIT_PKT_INTERVAL + 1;
 
     UDPC_HashMap_insert(ctx->conMap, addr, &cd);
+
+    if((ctx->flags & 0x1) != 0)
+    {
+        mtx_unlock(&ctx->tCVMtx);
+    }
 }
 
 int UDPC_queue_send(UDPC_Context *ctx, uint32_t addr, uint32_t isChecked, void *data, uint32_t size)
index c5b4d12017a02493879e38a1abe6b8a2809eac40..4e8475dabc0a31d7d83dc34beca73f127b3ccdcd 100644 (file)
@@ -13,7 +13,7 @@ typedef struct
 
 void printUsage()
 {
-    printf("Usage: [-c] -a <addr> -p <target_port> -l <listen_port>\n");
+    printf("Usage: [-c] [-t] -a <addr> -p <target_port> -l <listen_port>\n");
 }
 
 void conCallback(void *userdata, uint32_t addr)
@@ -38,6 +38,7 @@ void recCallback(void *userdata, char *data, uint32_t size)
 int main(int argc, char** argv)
 {
     int isClient = 0;
+    int isThreaded = 0;
     uint32_t targetAddress = 0;
     uint16_t targetPort = 0;
     uint16_t listenPort = 0;
@@ -50,6 +51,10 @@ int main(int argc, char** argv)
         {
             isClient = 1;
         }
+        else if(strcmp("-t", argv[0]) == 0)
+        {
+            isThreaded = 1;
+        }
         else if(strcmp("-a", argv[0]) == 0 && argc > 1)
         {
             targetAddress = UDPC_strtoa(argv[1]);
@@ -73,7 +78,15 @@ int main(int argc, char** argv)
         --argc; ++argv;
     }
 
-    UDPC_Context *ctx = UDPC_init(listenPort, isClient);
+    UDPC_Context *ctx;
+    if(isThreaded == 0)
+    {
+        ctx = UDPC_init(listenPort, isClient);
+    }
+    else
+    {
+        ctx = UDPC_init_threaded_update(listenPort, isClient);
+    }
 
     printf("isClient: %s, targetAddr: %s, targetPort: %u, listenPort: %u\n",
         isClient == 0 ? "false" : "true",
@@ -93,7 +106,7 @@ int main(int argc, char** argv)
             {
                 UDPC_client_initiate_connection(ctx, targetAddress, targetPort);
             }
-            UDPC_update(ctx);
+            if(isThreaded == 0) { UDPC_update(ctx); }
             UDPC_check_events(ctx);
             thrd_sleep(&(struct timespec){0, 16666666}, NULL);
             if(testCtx.hasConnectedOnce != 0 && testCtx.isConnected == 0)