From aab8cfe407a3bda5d9d210010c8b1a29fc52d66d Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 5 Mar 2019 12:56:05 +0900 Subject: [PATCH] Fix threaded update --- src/UDPConnection.c | 12 +++++++++++- src/test/UDPC_NetworkTest.c | 19 ++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/UDPConnection.c b/src/UDPConnection.c index a3f810b..488ba16 100644 --- a/src/UDPConnection.c +++ b/src/UDPConnection.c @@ -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) diff --git a/src/test/UDPC_NetworkTest.c b/src/test/UDPC_NetworkTest.c index c5b4d12..4e8475d 100644 --- a/src/test/UDPC_NetworkTest.c +++ b/src/test/UDPC_NetworkTest.c @@ -13,7 +13,7 @@ typedef struct void printUsage() { - printf("Usage: [-c] -a -p -l \n"); + printf("Usage: [-c] [-t] -a -p -l \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)