From d38c7ac105249f022dc48c37f8a7741239e8b7a9 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 4 Mar 2019 15:20:22 +0900 Subject: [PATCH] Add NetworkTest executable To be used to debug UDPConnection. --- CMakeLists.txt | 6 ++++ src/UDPConnection.c | 1 + src/test/UDPC_NetworkTest.c | 68 +++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/test/UDPC_NetworkTest.c diff --git a/CMakeLists.txt b/CMakeLists.txt index a8bd990..08a2409 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,4 +28,10 @@ if(CMAKE_BUILD_TYPE MATCHES "Debug") add_executable(UnitTest ${UDPC_UnitTest_SOURCES}) target_link_libraries(UnitTest PUBLIC UDPConnection) target_include_directories(UnitTest PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src) + + set(UDPC_NetworkTest_SOURCES + src/test/UDPC_NetworkTest.c) + add_executable(NetworkTest ${UDPC_NetworkTest_SOURCES}) + target_link_libraries(NetworkTest PUBLIC UDPConnection) + target_include_directories(NetworkTest PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src) endif() diff --git a/src/UDPConnection.c b/src/UDPConnection.c index 66a2489..609cddc 100644 --- a/src/UDPConnection.c +++ b/src/UDPConnection.c @@ -43,6 +43,7 @@ UDPC_Context* UDPC_init(uint16_t listenPort, int isClient) // bind socket context->socketInfo.sin_family = AF_INET; + // TODO specify what addr to listen on context->socketInfo.sin_addr.s_addr = INADDR_ANY; context->socketInfo.sin_port = listenPort; if(bind( diff --git a/src/test/UDPC_NetworkTest.c b/src/test/UDPC_NetworkTest.c new file mode 100644 index 0000000..87666c3 --- /dev/null +++ b/src/test/UDPC_NetworkTest.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +#include + +void printUsage() +{ + printf("Usage: [-c] -a -p -l \n"); +} + +int main(int argc, char** argv) +{ + int isClient = 0; + uint32_t targetAddress = 0; + uint16_t targetPort = 0; + uint16_t listenPort = 0; + + --argc; ++argv; + while(argc > 0) + { + if(strcmp("-c", argv[0]) == 0) + { + isClient = 1; + } + else if(strcmp("-a", argv[0]) == 0 && argc > 1) + { + targetAddress = UDPC_strtoa(argv[1]); + --argc; ++argv; + } + else if(strcmp("-p", argv[0]) == 0 && argc > 1) + { + targetPort = strtoul(argv[1], NULL, 10); + --argc; ++argv; + } + else if(strcmp("-l", argv[0]) == 0 && argc > 1) + { + listenPort = strtoul(argv[1], NULL, 10); + --argc; ++argv; + } + else if(strcmp("-h", argv[0]) == 0 || strcmp("--help", argv[0]) == 0) + { + printUsage(); + return 0; + } + --argc; ++argv; + } + + UDPC_Context *ctx = UDPC_init(listenPort, isClient); + if(UDPC_get_error(ctx) == UDPC_SUCCESS) + { + UDPC_set_logging_type(ctx, 4); + while(UDPC_get_error(ctx) == UDPC_SUCCESS) + { + if(isClient) + { + UDPC_client_initiate_connection(ctx, targetAddress, targetPort); + } + UDPC_update(ctx); + UDPC_check_events(ctx); + thrd_sleep(&(struct timespec){0, 16666666}, NULL); + } + } + UDPC_destroy(ctx); + + return 0; +}