diff --git a/src/client/client.h b/src/client/client.h index 9eb97ce..60a64e1 100644 --- a/src/client/client.h +++ b/src/client/client.h @@ -10,6 +10,6 @@ #include "screen.h" #include "ui.h" -void client(); +void client(void); #endif diff --git a/src/client/screen.h b/src/client/screen.h index 9802311..14a3116 100644 --- a/src/client/screen.h +++ b/src/client/screen.h @@ -22,12 +22,12 @@ #define SCR_PAIR(X, Y) COLOR_PAIR(__SCR_PAIRS[X][Y]) short __SCR_PAIRS[8][8]; -void scr_ctx_init(); +void scr_ctx_init(void); -void scr_fill(); +void scr_fill(void); void scr_char_fill(char); void scr_wchar_fill(wchar_t); -void scr_hide_cursor(); +void scr_hide_cursor(void); void scr_center_write(char*, int, int); void scr_box(int, int, int, int); diff --git a/src/client/ui.h b/src/client/ui.h index 99010f3..698ad18 100644 --- a/src/client/ui.h +++ b/src/client/ui.h @@ -5,9 +5,9 @@ #include #include -void ui_init(); +void ui_init(void); -void ui_free(); +void ui_free(void); #endif diff --git a/src/common.h b/src/common.h index 5b263de..291ce7a 100644 --- a/src/common.h +++ b/src/common.h @@ -29,6 +29,6 @@ uint64_t ntohll(uint64_t); #define SPAWN_CLIENT 1 #define SPAWN_SERVER 2 void register_spawn_type(uint8_t); -uint8_t get_spawn_type(); +uint8_t get_spawn_type(void); #endif \ No newline at end of file diff --git a/src/list.h b/src/list.h index 7531ff9..ae65ad4 100644 --- a/src/list.h +++ b/src/list.h @@ -24,31 +24,31 @@ struct list_t { int iter_pos; }; -list_t* list_init(); +list_t* list_init(void); void list_append(list_t*, void*); void list_prepend(list_t*, void*); void list_insert(list_t*, void*, int); -int list_size(list_t*); +int list_size(list_t*); void* list_get(list_t*, int); void* list_front(list_t*); void* list_back(list_t*); -int list_find(list_t*, void*); -int list_func_find(list_t*, listfindfptr); +int list_find(list_t*, void*); +int list_func_find(list_t*, listfindfptr); void* list_remove(list_t*, int); void* list_remove_front(list_t*); void* list_remove_back(list_t*); -void* list_remove_item(list_t*, void*); +void list_remove_item(list_t*, void*); -void list_iter_reset(list_t*); +void list_iter_reset(list_t*); void* list_iter_next(list_t*); void* list_iter_get(list_t*); void* list_iter_remove(list_t*); -void list_iter_insert_before(list_t*, void*); -void list_iter_insert_after(list_t*, void*); -int list_iter_pos(list_t*); +void list_iter_insert_before(list_t*, void*); +void list_iter_insert_after(list_t*, void*); +int list_iter_pos(list_t*); void list_free(list_t*); void list_nodes_free(list_t*); diff --git a/src/packet.h b/src/packet.h index 4a3bfa2..284fabb 100644 --- a/src/packet.h +++ b/src/packet.h @@ -39,7 +39,7 @@ struct packet_t { void packet_context_init(uint8_t); void packet_context_register(uint8_t, uint8_t, uint8_t, uint16_t, ...); -void packet_context_free(); +void packet_context_free(void); packet_t* packet_init_in(uint8_t*); packet_t* packet_init_out(uint8_t, uint16_t); diff --git a/src/queue.h b/src/queue.h index 464ca21..7614e5b 100644 --- a/src/queue.h +++ b/src/queue.h @@ -18,7 +18,7 @@ struct queue_t { queue_node_t *back; }; -queue_t* queue_init(); +queue_t* queue_init(void); void queue_push_front(queue_t*, void*); void queue_push(queue_t*, void*); diff --git a/src/server/context.h b/src/server/context.h index 8939abb..67e503c 100644 --- a/src/server/context.h +++ b/src/server/context.h @@ -5,7 +5,7 @@ #include #include "common.h" -void server_context_start(); -void server_context_stop(); +void server_context_start(void); +void server_context_stop(void); #endif \ No newline at end of file diff --git a/src/server/server.c b/src/server/server.c index 6459671..3347ad9 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -25,7 +25,10 @@ void server() { running = FALSE; break; } else { - + if(user_context_add(conn) == FALSE) { + sock_stop(conn); + sock_free(conn); + } } } diff --git a/src/server/server.h b/src/server/server.h index 8641bfb..6781172 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -9,6 +9,6 @@ #include "queue.h" #include "flimit.h" -void server(); +void server(void); #endif diff --git a/src/server/user.c b/src/server/user.c index 3440fe6..f7dda5e 100644 --- a/src/server/user.c +++ b/src/server/user.c @@ -6,8 +6,10 @@ struct { user_t **users; } static ctx; +BOOL user_check_flag_nomx(user_t *user, uint64_t flag); + void user_context_init() { - ctx.mx_ctx = PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_init(&ctx.mx_ctx, NULL); ctx.active_users = list_init(); ctx.users = (user_t**)malloc(MAX_CONNS * sizeof(user_t*)); @@ -17,51 +19,85 @@ void user_context_init() { } BOOL user_context_add(socket_t *sock) { + pthread_mutex_lock(&ctx.mx_ctx); + int i; for(i = 0; i < MAX_CONNS && ctx.users[i] != NULL; ++i); - if(i >= MAX_CONNS) + if(i >= MAX_CONNS) { + pthread_mutex_unlock(&ctx.mx_ctx); return FALSE; + } + user_t *user = user_init(sock, i); + ctx.users[i] = user; + list_append(ctx.active_users, user); + pthread_mutex_unlock(&ctx.mx_ctx); + return TRUE; } void user_context_remove(user_t *user) { + pthread_mutex_lock(&ctx.mx_ctx); if(user_check_flag(user, USER_FLAG_DELETING)) return; pthread_mutex_lock(&user->mx_user); user->flags |= USER_FLAG_DELETING; list_remove_item(ctx.active_users, user); - users[user->user_id] = NULL; + ctx.users[user->user_id] = NULL; + user_free(user); + pthread_mutex_unlock(&ctx.mx_ctx); } void user_context_free() { + pthread_mutex_lock(&ctx.mx_ctx); list_free(ctx.active_users); int i; for(i = 0; i < MAX_CONNS; ++i) - free(ctx.users[i]); + user_free(ctx.users[i]); free(ctx.users); } -user_t* user_init(socket_t *sock) { +user_t* user_init(socket_t *sock, uint16_t user_id) { user_t *user = (user_t*)malloc(sizeof(user_t)); - user->mx_user = PTHREAD_MUTEX_INITIALIZER; - + pthread_mutex_init(&user->mx_user, NULL); + user->user_id = user_id; + user->sock = sock; + user->flags = 0; + + user->in_packets = queue_init(); + user->out_packets = queue_init(); return user; } +BOOL user_check_flag_nomx(user_t *user, uint64_t flag) { + return (user->flags & flag) != 0; +} + + +BOOL user_check_flag(user_t *user, uint64_t flag) { + BOOL retval; + pthread_mutex_lock(&user->mx_user); + retval = user_check_flag_nomx(user, flag); + pthread_mutex_unlock(&user->mx_user); + return retval; +} + void user_free(user_t *user) { + if(user == NULL) + return; + packet_t *pck = NULL; - while((pck = (packet_t*)queue_pop(user->in_packets) != NULL) + while((pck = (packet_t*)queue_pop(user->in_packets)) != NULL) packet_free(pck); queue_free(user->in_packets); - while((pck = (packet_t*)queue_pop(user->out_packets) != NULL) + while((pck = (packet_t*)queue_pop(user->out_packets)) != NULL) packet_free(pck); queue_free(user->out_packets); diff --git a/src/server/user.h b/src/server/user.h index b2aad73..eab3c1c 100644 --- a/src/server/user.h +++ b/src/server/user.h @@ -7,6 +7,7 @@ #include "list.h" #include "queue.h" #include "sock.h" +#include "packet.h" #include "common.h" #define USER_FLAG_LOGGED 1 @@ -25,12 +26,12 @@ struct user_t { uint16_t x, y; }; -void user_context_init(); -void user_context_add(user_t*); +void user_context_init(void); +BOOL user_context_add(socket_t*); void user_context_remove(user_t*); -void user_context_free(); +void user_context_free(void); -user_t* user_init(socket_t*); +user_t* user_init(socket_t*, uint16_t); BOOL user_check_flag(user_t*, uint64_t); void user_free(user_t*); diff --git a/src/stack.h b/src/stack.h index 44474b5..db0dae4 100644 --- a/src/stack.h +++ b/src/stack.h @@ -12,7 +12,7 @@ struct stack_t { void *data; }; -stack_t* stack_init(); +stack_t* stack_init(void); void stack_push(stack_t*, void*); void* stack_pop(stack_t*);