]> git.seodisparate.com - UDPConnection/commitdiff
Add NetworkTest executable
authorStephen Seo <seo.disparate@gmail.com>
Mon, 4 Mar 2019 06:20:22 +0000 (15:20 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 4 Mar 2019 06:20:22 +0000 (15:20 +0900)
To be used to debug UDPConnection.

CMakeLists.txt
src/UDPConnection.c
src/test/UDPC_NetworkTest.c [new file with mode: 0644]

index a8bd9907fee568ae947c5f26a8694a0dddc96eda..08a24091fb28762f249a737c7726cbf502ddb3d5 100644 (file)
@@ -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()
index 66a24894f4bc9e441bc77d452baa97a20fecc6ee..609cddcd266b69b8dffa6b5ed87d3790f1b42f75 100644 (file)
@@ -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 (file)
index 0000000..87666c3
--- /dev/null
@@ -0,0 +1,68 @@
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <threads.h>
+
+#include <UDPConnection.h>
+
+void printUsage()
+{
+    printf("Usage: [-c] -a <addr> -p <target_port> -l <listen_port>\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;
+}