Convert NetworkTest to C from C++

This commit is contained in:
Stephen Seo 2019-11-13 11:47:53 +09:00
parent 7b5cf3b6f8
commit 90b36e3061
2 changed files with 69 additions and 69 deletions

View file

@ -56,7 +56,7 @@ if(CMAKE_BUILD_TYPE MATCHES "Debug")
endif() endif()
set(UDPC_NetworkTest_SOURCES set(UDPC_NetworkTest_SOURCES
src/test/UDPC_NetworkTest.cpp) src/test/UDPC_NetworkTest.c)
add_executable(NetworkTest ${UDPC_NetworkTest_SOURCES}) add_executable(NetworkTest ${UDPC_NetworkTest_SOURCES})
target_link_libraries(NetworkTest PUBLIC UDPConnection) target_link_libraries(NetworkTest PUBLIC UDPConnection)
target_include_directories(NetworkTest PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src) target_include_directories(NetworkTest PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)

View file

@ -1,16 +1,12 @@
#include <cstring> #include <stdlib.h>
#include <string> #include <string.h>
#include <cstdio> #include <stdio.h>
#include <thread> #include <threads.h>
#include <chrono>
#include <regex>
#include <vector>
#include <UDPConnection.h> #include <UDPConnection.h>
#define QUEUED_MAX_SIZE 32 #define QUEUED_MAX_SIZE 32
#define SEND_IDS_SIZE 64
static const std::regex ipv6_regex_linkonly = std::regex(R"d(fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,})d");
void usage() { void usage() {
puts("[-c | -s] - client or server (default server)"); puts("[-c | -s] - client or server (default server)");
@ -24,6 +20,13 @@ void usage() {
puts("-e - enable receiving events"); puts("-e - enable receiving events");
} }
void sleep_seconds(unsigned int seconds) {
struct timespec duration;
duration.tv_sec = seconds;
duration.tv_nsec = 0;
thrd_sleep(&duration, NULL);
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
--argc; ++argv; --argc; ++argv;
if(argc == 0) { if(argc == 0) {
@ -31,61 +34,61 @@ int main(int argc, char **argv) {
return 0; return 0;
} }
bool isClient = false; int isClient = 0;
const char *listenAddr = nullptr; const char *listenAddr = NULL;
const char *listenPort = nullptr; const char *listenPort = NULL;
const char *connectionAddr = nullptr; const char *connectionAddr = NULL;
const char *connectionPort = nullptr; const char *connectionPort = NULL;
unsigned int tickLimit = 15; unsigned int tickLimit = 15;
bool noPayload = false; int noPayload = 0;
UDPC_LoggingType logLevel = UDPC_LoggingType::UDPC_DEBUG; UDPC_LoggingType logLevel = UDPC_DEBUG;
bool isReceivingEvents = false; int isReceivingEvents = 0;
while(argc > 0) { while(argc > 0) {
if(std::strcmp(argv[0], "-c") == 0) { if(strcmp(argv[0], "-c") == 0) {
isClient = true; isClient = 1;
} else if(std::strcmp(argv[0], "-s") == 0) { } else if(strcmp(argv[0], "-s") == 0) {
isClient = false; isClient = 0;
} else if(std::strcmp(argv[0], "-ll") == 0 && argc > 1) { } else if(strcmp(argv[0], "-ll") == 0 && argc > 1) {
--argc; ++argv; --argc; ++argv;
listenAddr = argv[0]; listenAddr = argv[0];
} else if(std::strcmp(argv[0], "-lp") == 0 && argc > 1) { } else if(strcmp(argv[0], "-lp") == 0 && argc > 1) {
--argc; ++argv; --argc; ++argv;
listenPort = argv[0]; listenPort = argv[0];
} else if(std::strcmp(argv[0], "-cl") == 0 && argc > 1) { } else if(strcmp(argv[0], "-cl") == 0 && argc > 1) {
--argc; ++argv; --argc; ++argv;
connectionAddr = argv[0]; connectionAddr = argv[0];
} else if(std::strcmp(argv[0], "-cp") == 0 && argc > 1) { } else if(strcmp(argv[0], "-cp") == 0 && argc > 1) {
--argc; ++argv; --argc; ++argv;
connectionPort = argv[0]; connectionPort = argv[0];
} else if(std::strcmp(argv[0], "-t") == 0 && argc > 1) { } else if(strcmp(argv[0], "-t") == 0 && argc > 1) {
--argc; ++argv; --argc; ++argv;
tickLimit = std::atoi(argv[0]); tickLimit = atoi(argv[0]);
printf("Set tick limit to %u\n", tickLimit); printf("Set tick limit to %u\n", tickLimit);
} else if(std::strcmp(argv[0], "-n") == 0) { } else if(strcmp(argv[0], "-n") == 0) {
noPayload = true; noPayload = 1;
puts("Disabling sending payload"); puts("Disabling sending payload");
} else if(std::strcmp(argv[0], "-l") == 0) { } else if(strcmp(argv[0], "-l") == 0) {
--argc; ++argv; --argc; ++argv;
if(std::strcmp(argv[0], "silent") == 0) { if(strcmp(argv[0], "silent") == 0) {
logLevel = UDPC_LoggingType::UDPC_SILENT; logLevel = UDPC_SILENT;
} else if(std::strcmp(argv[0], "error") == 0) { } else if(strcmp(argv[0], "error") == 0) {
logLevel = UDPC_LoggingType::UDPC_ERROR; logLevel = UDPC_ERROR;
} else if(std::strcmp(argv[0], "warning") == 0) { } else if(strcmp(argv[0], "warning") == 0) {
logLevel = UDPC_LoggingType::UDPC_WARNING; logLevel = UDPC_WARNING;
} else if(std::strcmp(argv[0], "info") == 0) { } else if(strcmp(argv[0], "info") == 0) {
logLevel = UDPC_LoggingType::UDPC_INFO; logLevel = UDPC_INFO;
} else if(std::strcmp(argv[0], "verbose") == 0) { } else if(strcmp(argv[0], "verbose") == 0) {
logLevel = UDPC_LoggingType::UDPC_VERBOSE; logLevel = UDPC_VERBOSE;
} else if(std::strcmp(argv[0], "debug") == 0) { } else if(strcmp(argv[0], "debug") == 0) {
logLevel = UDPC_LoggingType::UDPC_DEBUG; logLevel = UDPC_DEBUG;
} else { } else {
printf("ERROR: invalid argument \"%s\", expected " printf("ERROR: invalid argument \"%s\", expected "
"silent|error|warning|info|verbose|debug", argv[0]); "silent|error|warning|info|verbose|debug", argv[0]);
usage(); usage();
return 1; return 1;
} }
} else if(std::strcmp(argv[0], "-e") == 0) { } else if(strcmp(argv[0], "-e") == 0) {
isReceivingEvents = true; isReceivingEvents = 1;
puts("Enabled isReceivingEvents"); puts("Enabled isReceivingEvents");
} else { } else {
printf("ERROR: invalid argument \"%s\"\n", argv[0]); printf("ERROR: invalid argument \"%s\"\n", argv[0]);
@ -112,51 +115,48 @@ int main(int argc, char **argv) {
UDPC_ConnectionId listenId; UDPC_ConnectionId listenId;
UDPC_ConnectionId connectionId; UDPC_ConnectionId connectionId;
if(std::strcmp(listenAddr, "any") == 0) { if(strcmp(listenAddr, "any") == 0) {
listenId = UDPC_create_id_anyaddr(std::atoi(listenPort)); listenId = UDPC_create_id_anyaddr(atoi(listenPort));
} else if(std::regex_match(listenAddr, ipv6_regex_linkonly)) {
uint32_t scope_id;
auto addr = UDPC_strtoa_link(listenAddr, &scope_id);
listenId = UDPC_create_id_full(addr, scope_id, std::atoi(listenPort));
} else { } else {
listenId = UDPC_create_id(UDPC_strtoa(listenAddr), std::atoi(listenPort)); listenId = UDPC_create_id(UDPC_strtoa(listenAddr), atoi(listenPort));
} }
if(isClient) { if(isClient) {
if(std::regex_match(connectionAddr, ipv6_regex_linkonly)) { connectionId = UDPC_create_id(UDPC_strtoa(connectionAddr), atoi(connectionPort));
uint32_t scope_id;
auto addr = UDPC_strtoa_link(connectionAddr, &scope_id);
connectionId = UDPC_create_id_full(addr, scope_id, std::atoi(connectionPort));
} else {
connectionId = UDPC_create_id(UDPC_strtoa(connectionAddr), std::atoi(connectionPort));
} }
} UDPC_HContext context = UDPC_init_threaded_update(listenId, isClient);
auto context = UDPC_init_threaded_update(listenId, isClient ? 1 : 0);
if(!context) { if(!context) {
puts("ERROR: context is NULL"); puts("ERROR: context is NULL");
return 1; return 1;
} }
UDPC_set_logging_type(context, logLevel); UDPC_set_logging_type(context, logLevel);
UDPC_set_receiving_events(context, isReceivingEvents ? 1 : 0); UDPC_set_receiving_events(context, isReceivingEvents);
unsigned int tick = 0; unsigned int tick = 0;
unsigned int temp = 0; unsigned int temp = 0;
unsigned int temp2, temp3; unsigned int temp2, temp3;
unsigned long size; unsigned long size;
UDPC_ConnectionId *list = nullptr; UDPC_ConnectionId *list = NULL;
std::vector<unsigned int> sendIds; unsigned int sendIds[SEND_IDS_SIZE];
unsigned int sendIdsSize = 0;
UDPC_PacketInfo received; UDPC_PacketInfo received;
UDPC_Event event; UDPC_Event event;
while(true) { while(1) {
std::this_thread::sleep_for(std::chrono::seconds(1)); sleep_seconds(1);
if(isClient && UDPC_has_connection(context, connectionId) == 0) { if(isClient && UDPC_has_connection(context, connectionId) == 0) {
UDPC_client_initiate_connection(context, connectionId); UDPC_client_initiate_connection(context, connectionId);
} }
if(!noPayload) { if(!noPayload) {
list = UDPC_get_list_connected(context, &temp); list = UDPC_get_list_connected(context, &temp);
if(list) { if(list) {
if(sendIds.size() < temp) { if(sendIdsSize < temp) {
sendIds.resize(temp, 0); while(sendIdsSize < temp) {
} else if(sendIds.size() > temp) { if(sendIdsSize == SEND_IDS_SIZE) {
sendIds.resize(temp); temp = SEND_IDS_SIZE;
break;
}
sendIds[sendIdsSize++] = 0;
}
} else if(sendIdsSize > temp) {
sendIdsSize = temp;
} }
size = UDPC_get_queue_send_current_size(context); size = UDPC_get_queue_send_current_size(context);
temp2 = size < QUEUED_MAX_SIZE ? QUEUED_MAX_SIZE - size : 0; temp2 = size < QUEUED_MAX_SIZE ? QUEUED_MAX_SIZE - size : 0;