Fix threaded update

This commit is contained in:
Stephen Seo 2019-03-05 12:56:05 +09:00
parent 0ff6bd13a7
commit aab8cfe407
2 changed files with 27 additions and 4 deletions

View file

@ -126,7 +126,7 @@ UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort, int isClient)
context->error = UDPC_SUCCESS; context->error = UDPC_SUCCESS;
context->error = thrd_create( context->error = thrd_create(
&context->threadHandle, UDPC_INTERNAL_threadfn, &context); &context->threadHandle, UDPC_INTERNAL_threadfn, context);
if(context->error != thrd_success) if(context->error != thrd_success)
{ {
CleanupSocket(context->socketHandle); 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) 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) if((ctx->flags & 0x2) == 0 || UDPC_HashMap_has(ctx->conMap, addr) != 0)
{ {
// must be client or no already-existing connection to same address // 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; cd.sent.tv_sec -= UDPC_INIT_PKT_INTERVAL + 1;
UDPC_HashMap_insert(ctx->conMap, addr, &cd); 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) int UDPC_queue_send(UDPC_Context *ctx, uint32_t addr, uint32_t isChecked, void *data, uint32_t size)

View file

@ -13,7 +13,7 @@ typedef struct
void printUsage() 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) 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 main(int argc, char** argv)
{ {
int isClient = 0; int isClient = 0;
int isThreaded = 0;
uint32_t targetAddress = 0; uint32_t targetAddress = 0;
uint16_t targetPort = 0; uint16_t targetPort = 0;
uint16_t listenPort = 0; uint16_t listenPort = 0;
@ -50,6 +51,10 @@ int main(int argc, char** argv)
{ {
isClient = 1; isClient = 1;
} }
else if(strcmp("-t", argv[0]) == 0)
{
isThreaded = 1;
}
else if(strcmp("-a", argv[0]) == 0 && argc > 1) else if(strcmp("-a", argv[0]) == 0 && argc > 1)
{ {
targetAddress = UDPC_strtoa(argv[1]); targetAddress = UDPC_strtoa(argv[1]);
@ -73,7 +78,15 @@ int main(int argc, char** argv)
--argc; ++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", printf("isClient: %s, targetAddr: %s, targetPort: %u, listenPort: %u\n",
isClient == 0 ? "false" : "true", isClient == 0 ? "false" : "true",
@ -93,7 +106,7 @@ int main(int argc, char** argv)
{ {
UDPC_client_initiate_connection(ctx, targetAddress, targetPort); UDPC_client_initiate_connection(ctx, targetAddress, targetPort);
} }
UDPC_update(ctx); if(isThreaded == 0) { UDPC_update(ctx); }
UDPC_check_events(ctx); UDPC_check_events(ctx);
thrd_sleep(&(struct timespec){0, 16666666}, NULL); thrd_sleep(&(struct timespec){0, 16666666}, NULL);
if(testCtx.hasConnectedOnce != 0 && testCtx.isConnected == 0) if(testCtx.hasConnectedOnce != 0 && testCtx.isConnected == 0)