Change NetworkTest to exit after disconnect

This commit is contained in:
Stephen Seo 2019-03-05 12:11:57 +09:00
parent 6597b552a7
commit 0ff6bd13a7

View file

@ -5,6 +5,12 @@
#include <UDPConnection.h> #include <UDPConnection.h>
typedef struct
{
int isConnected;
int hasConnectedOnce;
} TestContext;
void printUsage() void printUsage()
{ {
printf("Usage: [-c] -a <addr> -p <target_port> -l <listen_port>\n"); printf("Usage: [-c] -a <addr> -p <target_port> -l <listen_port>\n");
@ -12,12 +18,17 @@ void printUsage()
void conCallback(void *userdata, uint32_t addr) 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) 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) void recCallback(void *userdata, char *data, uint32_t size)
@ -30,7 +41,7 @@ int main(int argc, char** argv)
uint32_t targetAddress = 0; uint32_t targetAddress = 0;
uint16_t targetPort = 0; uint16_t targetPort = 0;
uint16_t listenPort = 0; uint16_t listenPort = 0;
int isConnected = 0; TestContext testCtx = {0, 0};
--argc; ++argv; --argc; ++argv;
while(argc > 0) while(argc > 0)
@ -73,18 +84,22 @@ int main(int argc, char** argv)
if(UDPC_get_error(ctx) == UDPC_SUCCESS) if(UDPC_get_error(ctx) == UDPC_SUCCESS)
{ {
UDPC_set_logging_type(ctx, 4); UDPC_set_logging_type(ctx, 4);
UDPC_set_callback_connected(ctx, conCallback, &isConnected); UDPC_set_callback_connected(ctx, conCallback, &testCtx);
UDPC_set_callback_disconnected(ctx, discCallback, &isConnected); UDPC_set_callback_disconnected(ctx, discCallback, &testCtx);
UDPC_set_callback_received(ctx, recCallback, NULL); UDPC_set_callback_received(ctx, recCallback, NULL);
while(UDPC_get_error(ctx) == UDPC_SUCCESS) while(UDPC_get_error(ctx) == UDPC_SUCCESS)
{ {
if(isClient && isConnected == 0) if(isClient && testCtx.isConnected == 0)
{ {
UDPC_client_initiate_connection(ctx, targetAddress, targetPort); UDPC_client_initiate_connection(ctx, targetAddress, targetPort);
} }
UDPC_update(ctx); 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)
{
break;
}
} }
} }
UDPC_destroy(ctx); UDPC_destroy(ctx);