From 0ff6bd13a7d715fa62777cf49a72782b115c547c Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 5 Mar 2019 12:11:57 +0900 Subject: [PATCH] Change NetworkTest to exit after disconnect --- src/test/UDPC_NetworkTest.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/test/UDPC_NetworkTest.c b/src/test/UDPC_NetworkTest.c index 960d0fc..c5b4d12 100644 --- a/src/test/UDPC_NetworkTest.c +++ b/src/test/UDPC_NetworkTest.c @@ -5,6 +5,12 @@ #include +typedef struct +{ + int isConnected; + int hasConnectedOnce; +} TestContext; + void printUsage() { printf("Usage: [-c] -a -p -l \n"); @@ -12,12 +18,17 @@ void printUsage() void conCallback(void *userdata, uint32_t addr) { - *((int*)userdata) = 1; + TestContext *ctx = userdata; + ctx->isConnected = 1; + ctx->hasConnectedOnce = 1; + printf("Connected callback called\n"); } void discCallback(void *userdata, uint32_t addr) { - *((int*)userdata) = 0; + TestContext *ctx = userdata; + ctx->isConnected = 0; + printf("Disconnected callback called\n"); } void recCallback(void *userdata, char *data, uint32_t size) @@ -30,7 +41,7 @@ int main(int argc, char** argv) uint32_t targetAddress = 0; uint16_t targetPort = 0; uint16_t listenPort = 0; - int isConnected = 0; + TestContext testCtx = {0, 0}; --argc; ++argv; while(argc > 0) @@ -73,18 +84,22 @@ int main(int argc, char** argv) if(UDPC_get_error(ctx) == UDPC_SUCCESS) { UDPC_set_logging_type(ctx, 4); - UDPC_set_callback_connected(ctx, conCallback, &isConnected); - UDPC_set_callback_disconnected(ctx, discCallback, &isConnected); + UDPC_set_callback_connected(ctx, conCallback, &testCtx); + UDPC_set_callback_disconnected(ctx, discCallback, &testCtx); UDPC_set_callback_received(ctx, recCallback, NULL); while(UDPC_get_error(ctx) == UDPC_SUCCESS) { - if(isClient && isConnected == 0) + if(isClient && testCtx.isConnected == 0) { UDPC_client_initiate_connection(ctx, targetAddress, targetPort); } UDPC_update(ctx); UDPC_check_events(ctx); thrd_sleep(&(struct timespec){0, 16666666}, NULL); + if(testCtx.hasConnectedOnce != 0 && testCtx.isConnected == 0) + { + break; + } } } UDPC_destroy(ctx);