summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTest_User <hax@andrewyu.org>2023-05-06 18:13:30 -0400
committerTest_User <hax@andrewyu.org>2023-05-06 18:13:30 -0400
commitd11d6c901e8dcbc1e008d3856260255056adb0cd (patch)
tree6f8819eb93ba2c639fc016c1b5809e31d7322717
parentb98cc288211baa7d98a7e1ccac538f32129a061f (diff)
downloadcoupserv-d11d6c901e8dcbc1e008d3856260255056adb0cd.tar.gz
coupserv-d11d6c901e8dcbc1e008d3856260255056adb0cd.zip
Add locks so it's actually thread-safe, will probably make more efficient use of them later
-rw-r--r--main.c9
-rw-r--r--network.h3
-rw-r--r--server_network.c2
3 files changed, 13 insertions, 1 deletions
diff --git a/main.c b/main.c
index 76b22e6..b0f7233 100644
--- a/main.c
+++ b/main.c
@@ -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"));
diff --git a/network.h b/network.h
index ced1d4d..7c70df7 100644
--- a/network.h
+++ b/network.h
@@ -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;