Add NetworkTest executable

To be used to debug UDPConnection.
This commit is contained in:
Stephen Seo 2019-03-04 15:20:22 +09:00
parent 89afc58bc7
commit d38c7ac105
3 changed files with 75 additions and 0 deletions

View 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()

View 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(

View file

@ -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;
}