the only commit that will actually build ever

This commit is contained in:
mallocnull 2017-11-13 17:01:06 -06:00
parent 5e3fb97a12
commit b0e6c58c14
13 changed files with 76 additions and 36 deletions

View file

@ -10,6 +10,6 @@
#include "screen.h"
#include "ui.h"
void client();
void client(void);
#endif

View file

@ -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);

View file

@ -5,9 +5,9 @@
#include <stdlib.h>
#include <stdint.h>
void ui_init();
void ui_init(void);
void ui_free();
void ui_free(void);
#endif

View file

@ -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

View file

@ -24,7 +24,7 @@ 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*);
@ -40,7 +40,7 @@ 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_next(list_t*);

View file

@ -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);

View file

@ -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*);

View file

@ -5,7 +5,7 @@
#include <pthread.h>
#include "common.h"
void server_context_start();
void server_context_stop();
void server_context_start(void);
void server_context_stop(void);
#endif

View file

@ -25,7 +25,10 @@ void server() {
running = FALSE;
break;
} else {
if(user_context_add(conn) == FALSE) {
sock_stop(conn);
sock_free(conn);
}
}
}

View file

@ -9,6 +9,6 @@
#include "queue.h"
#include "flimit.h"
void server();
void server(void);
#endif

View file

@ -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);

View file

@ -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*);

View file

@ -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*);