diff options
-rw-r--r-- | main.c | 9 | ||||
-rw-r--r-- | network.h | 3 | ||||
-rw-r--r-- | server_network.c | 2 |
3 files changed, 13 insertions, 1 deletions
@@ -39,16 +39,20 @@ #include "types.h" void *client_loop(void *ign) { + pthread_mutex_lock(&send_lock); while (1) { struct string full_msg = {.data = malloc(0), .len = 0}; - WRITES(1, STRING("Yay\n")); + pthread_mutex_unlock(&send_lock); client_fd = accept(client_listen_fd, NULL, NULL); + pthread_mutex_lock(&send_lock); listen(client_listen_fd, 0); client_connected = 0; client_nick.data = malloc(0); while (1) { char data[512]; + pthread_mutex_unlock(&send_lock); // TODO: proper locking, this works for now but is certainly inefficient uint64_t new_len = read(client_fd, data, 512); + pthread_mutex_lock(&send_lock); if (new_len == 0) { goto disconnect_client; @@ -210,10 +214,12 @@ int main(void) { pthread_create(&client_thread_id, NULL, client_loop, NULL); + pthread_mutex_lock(&send_lock); struct string full_msg = {malloc(0), 0}; while (1) { char data[512]; uint64_t new_len; + pthread_mutex_unlock(&send_lock); { int len; do { @@ -224,6 +230,7 @@ int main(void) { else new_len = len; } + pthread_mutex_lock(&send_lock); if (new_len == 0) { WRITES(1, STRING("Disconnected.\n")); @@ -29,6 +29,7 @@ #pragma once #include <netinet/in.h> +#include <pthread.h> #include "types.h" #include "table.h" @@ -64,6 +65,8 @@ extern struct table client_network_commands; extern struct table server_list; extern struct table user_list; +extern pthread_mutex_t send_lock; + extern int client_fd; extern int client_listen_fd; extern struct string client_nick; diff --git a/server_network.c b/server_network.c index 8125a45..0a471bd 100644 --- a/server_network.c +++ b/server_network.c @@ -44,6 +44,8 @@ #include "utils.h" #include "commands.h" +pthread_mutex_t send_lock = PTHREAD_MUTEX_INITIALIZER; + int resolve(char *address, char *port, struct sockaddr *sockaddr) { int success; struct addrinfo hints = {0}, *info; |