From cab382db088d9f240253466a1c5a26c62f3967c8 Mon Sep 17 00:00:00 2001 From: justanothercatgirl Date: Sat, 8 Feb 2025 22:13:52 +0300 Subject: Implemented multiplexing in main thread (TCP loop). Removed CMake files. TODO: Implement multiplexing in worker threads (UDP loops), implement channel_pool interface. --- server/channel.c | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'server/channel.c') diff --git a/server/channel.c b/server/channel.c index a0e2779..63ef11a 100644 --- a/server/channel.c +++ b/server/channel.c @@ -2,10 +2,11 @@ #include #include +#include #include #include -#define decisive_push(array, index, elem) \ +#define maybe_push(array, index, elem) \ do { \ if (index >= array_size(array)) { \ array_push(array, elem); \ @@ -35,6 +36,7 @@ struct ch_user { static __THREAD struct hash_set users; static __THREAD int sockfd; + static int __user_hset_cmp(const void *a, const void *b) { struct ch_user *_a = (struct ch_user *)a, *_b = (struct ch_user *)b; return _a->id - _b->id; @@ -54,17 +56,17 @@ bool channel_init(void) { (sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) >= 0 && bind(sockfd, (struct sockaddr *)&thread_local_address, sizeof(thread_local_address)) == 0; if (!chain_result) { - perror("channel init failed"); + LERRP("channel init failed"); if (hset_ok(users)) hset_free(&users); if (sockfd >= 0) close(sockfd); return false; } - DEBUGF("Channel [%i] created\n", sockfd); + LDEBUGF("Channel [%i] created", sockfd); return true; } static void channel_uninit(void) { hset_free(&users); - if (close(sockfd) == -1) perror("could not gracefully uninitialize channel"); + if (close(sockfd) == -1) LERRP("couldPnot gracefully uninitialize channel"); } _Noreturn static inline void forced_cleanup(struct kv_packet **packets) { clear_packet_array(packets); @@ -118,7 +120,7 @@ static void send_packets_back(struct kv_packet **packets) { .sin_port = htons(current_user->port), .sin_addr = {htonl(current_user->ip)}}; for (size_t j = 0; packets[j] != NULL && j < array_size(packets); ++j) { - DEBUGF("sending packet with id %zu to destination: %u.%u.%u.%u:%hu\n", packets[j]->uid, + LDEBUGF("sending packet with id %zu to destination: %u.%u.%u.%u:%hu", packets[j]->uid, (destination.sin_addr.s_addr >> 24) & 0xFF, (destination.sin_addr.s_addr >> 16) & 0xFF, (destination.sin_addr.s_addr >> 8) & 0xFF, destination.sin_addr.s_addr & 0xFF, destination.sin_port); @@ -126,14 +128,21 @@ static void send_packets_back(struct kv_packet **packets) { int error_code = sendto( sockfd, packets[j], KV_PACKET_SIZE, 0, (struct sockaddr *)&destination, sizeof(destination)); - if (error_code) perror("could not send packets back"); + if (error_code) LWARNP("could not send packets back"); } } } /* * An example of how you should NOT write code - * Todo: please rewrite this shit + * Todo: please rewrite this shi + * Also, this started to busy-loop... + * As i've determined, this requires a complete re-write, so this is what i'm gonna be doing now + * + * Actually, am I reading this correct? Did I write this at the time when I thought + * UDP could magically broadcast my packets? Why it is this complex dude... + * + * I am abandoning this place in code and am implementing channel_pool */ void *thread_loop(void *arg) { if (!channel_init()) pthread_exit(NULL); @@ -143,6 +152,7 @@ void *thread_loop(void *arg) { size_t recvd_index = 0; int recv_flag = MSG_DONTWAIT; while (true) { + LDEBUGV("I am looping"); struct sockaddr_in client_addr; socklen_t client_addr_len = sizeof(client_addr); ssize_t recvlength = recvfrom( @@ -158,16 +168,21 @@ void *thread_loop(void *arg) { struct kv_packet *kv_copy = malloc(KV_PACKET_SIZE); memcpy(kv_copy, &work_buffer, KV_PACKET_SIZE); ++recvd_index; - decisive_push(recvd_data, recvd_index, kv_copy); + maybe_push(recvd_data, recvd_index, kv_copy); } else if (errno == EWOULDBLOCK) { - if (array_size(recvd_data) == 0) { - recv_flag &= ~MSG_DONTWAIT; - continue; - } + recv_flag &= ~MSG_DONTWAIT; + if (recvd_index == 0) continue; send_packets_back(recvd_data); clear_packet_array(recvd_data); + recvd_index = 0; } else { - perror("error in thread_loop"); + LWARNP("error in thread_loop"); } } } + +void* new_thread_loop(void* arg) { + return arg; +} + +/* vim: set ts=8 noet: */ -- cgit v1.2.3-70-g09d2