diff --git a/core b/core index de3ad55..43edd0b 100644 Binary files a/core and b/core differ diff --git a/src/client/client.c b/src/client/client.c index 6c9f9da..bcf57a5 100644 --- a/src/client/client.c +++ b/src/client/client.c @@ -167,8 +167,8 @@ void create_account() { char input[9]; //scr_prompt_string(20, "this prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test", input, 8); - scr_prompt_voptions(40, "test of a prompt box of a prompt box of a prompt box of a prompt\n \nbox of a prompt box of a prompt box\n \nthis is a test of a prompt box", 3, - "testtesttesttesttesttesttest a", "test b", "test c"); + scr_prompt_voptions(30, "I'm glad you know which way to go, but that ain't gunna stop me, here we go!\n \nCheck and turn the signals to the left.\n \nNow turn to the left.", 3, + "Now turn to the left!", "Now turn to the right!", "Uh oh"); /*int a = 0; while(a != KEY_LF) diff --git a/src/client/client.h b/src/client/client.h index 62eb341..9eb97ce 100644 --- a/src/client/client.h +++ b/src/client/client.h @@ -4,6 +4,7 @@ #include #include #include +#include "common.h" #include "sock.h" #include "packet.h" #include "screen.h" diff --git a/src/common.h b/src/common.h index 4e2a259..5b263de 100644 --- a/src/common.h +++ b/src/common.h @@ -7,6 +7,8 @@ #define WORK +#define MAHOU_PORT "6770" + #define FALSE 0 #define TRUE 1 #define BOOL char diff --git a/src/list.c b/src/list.c index 13c47e5..183f219 100644 --- a/src/list.c +++ b/src/list.c @@ -149,6 +149,12 @@ void* list_remove_back(list_t *list) { return list_remove(list, list->size - 1); } +void list_remove_item(list_t *list, void *item) { + int pos; + if((pos = list_find(list, item)) != -1) + list_remove(list, pos); +} + void list_iter_reset(list_t *list) { list->iterator = NULL; list->iter_prev = NULL; diff --git a/src/list.h b/src/list.h index a1172f8..7531ff9 100644 --- a/src/list.h +++ b/src/list.h @@ -40,6 +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_iter_reset(list_t*); void* list_iter_next(list_t*); diff --git a/src/main.c b/src/main.c index a7ce304..af4f3ee 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,12 @@ #include "server/server.h" int main(int argc, char **argv) { + uint8_t spawn_type = + argc == 2 && strcmp(argv[1], "server") == 0 + ? SPAWN_SERVER + : SPAWN_CLIENT; + + register_spawn_type(spawn_type); packet_context_init(5); packet_context_register(PCK_CTX_C2S, PCK_ID_CLERIC_REQ, 0, 1, 1); @@ -17,14 +23,13 @@ int main(int argc, char **argv) { packet_context_register(PCK_CTX_S2C, PCK_ID_ALERT, 0, 4, 1, 1, 2, 512); packet_context_register(PCK_CTX_S2C, PCK_ID_PROMPT_BOOL, 0, 4, 1, 1, 2, 512); packet_context_register(PCK_CTX_S2C, PCK_ID_PROMPT_STR, 0, 5, 1, 1, 2, 512, 1); - packet_context_register(PCK_CTX_S2C, PCK_ID_PROMPT_LIST, 0, 6, 1, 1, 2, 512, 1, 256); + packet_context_register(PCK_CTX_S2C, PCK_ID_PROMPT_LIST, 5, 6, 1, 1, 2, 512, 2, 256); packet_context_register(PCK_CTX_S2C, PCK_ID_CHANGE_CTX, 0, 1, 2); - if(argc == 2 && strcmp(argv[1], "server") == 0) { - register_spawn_type(SPAWN_SERVER); + if(spawn_type == SPAWN_SERVER) server(); - } else { - register_spawn_type(SPAWN_CLIENT); + else client(); - } + + return 0; } diff --git a/src/server/server.c b/src/server/server.c index ebcf7fa..6459671 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -1,6 +1,9 @@ #include "server.h" void server() { + BOOL running = TRUE; + socket_t *conn; + user_context_init(); server_context_start(); @@ -9,15 +12,33 @@ void server() { sock_set_nonblocking(sock); flimit_t vsync; - for(;;) { + while(running) { frame_limit_tick(&vsync); + for(;;) { + conn = sock_accept(sock); + if(conn == NULL) + break; + else if(conn == -1) { + running = FALSE; + break; + } else { + + } + } + frame_limit_wait(&vsync, 1000); } + + server_context_stop(); + user_context_free(); - printf("awaiting connection...\r\n"); + sock_stop(sock); + sock_free(sock); + + /*printf("awaiting connection...\r\n"); socket_t *conn = sock_accept(sock); printf("connected\r\n"); @@ -26,11 +47,5 @@ void server() { char in[10]; sock_recv(conn, in, 10); - printf("got %s\r\n", in); - - server_context_stop(); - user_context_free(); - - sock_stop(sock); - sock_free(sock); + printf("got %s\r\n", in);*/ } diff --git a/src/server/user.c b/src/server/user.c index 0f1bac1..3440fe6 100644 --- a/src/server/user.c +++ b/src/server/user.c @@ -1,11 +1,13 @@ #include "user.h" struct { + pthread_mutex_t mx_ctx; list_t *active_users; user_t **users; } static ctx; void user_context_init() { + ctx.mx_ctx = PTHREAD_MUTEX_INITIALIZER; ctx.active_users = list_init(); ctx.users = (user_t**)malloc(MAX_CONNS * sizeof(user_t*)); @@ -14,6 +16,26 @@ void user_context_init() { ctx.users[i] = NULL; } +BOOL user_context_add(socket_t *sock) { + int i; + for(i = 0; i < MAX_CONNS && ctx.users[i] != NULL; ++i); + if(i >= MAX_CONNS) + return FALSE; + + +} + +void user_context_remove(user_t *user) { + 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; + +} + void user_context_free() { list_free(ctx.active_users); @@ -22,4 +44,26 @@ void user_context_free() { free(ctx.users[i]); free(ctx.users); +} + +user_t* user_init(socket_t *sock) { + user_t *user = (user_t*)malloc(sizeof(user_t)); + user->mx_user = PTHREAD_MUTEX_INITIALIZER; + + + return user; +} + +void user_free(user_t *user) { + packet_t *pck = 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) + packet_free(pck); + queue_free(user->out_packets); + + free(user); } \ No newline at end of file diff --git a/src/server/user.h b/src/server/user.h index da05f56..b2aad73 100644 --- a/src/server/user.h +++ b/src/server/user.h @@ -3,28 +3,35 @@ #include #include +#include #include "list.h" #include "queue.h" #include "sock.h" #include "common.h" +#define USER_FLAG_LOGGED 1 +#define USER_FLAG_DELETING 2 + typedef struct user_t user_t; struct user_t { - queue_t *in_packets; - pthread_mutex_t mx_in_packets; + pthread_mutex_t mx_user; + queue_t *in_packets; queue_t *out_packets; - pthread_mutex_t mx_out_packets; socket_t *sock; + uint64_t flags; uint16_t user_id, context_id; uint16_t x, y; }; void user_context_init(); - - - +void user_context_add(user_t*); +void user_context_remove(user_t*); void user_context_free(); +user_t* user_init(socket_t*); +BOOL user_check_flag(user_t*, uint64_t); +void user_free(user_t*); + #endif \ No newline at end of file