From 3944d713a7803f992edad269c65c4b5f69354960 Mon Sep 17 00:00:00 2001 From: mallocnull Date: Thu, 26 Oct 2017 21:56:07 +0000 Subject: [PATCH] good day today park --- protocol | 15 ++++++++------- src/client/client.c | 6 +++--- src/client/ui.c | 2 +- src/common.c | 37 +++++++++++++++++++++++++++++++++++++ src/common.h | 13 +++++++++++++ src/main.c | 14 ++++++++++++++ src/packet.c | 11 +++++++++++ src/packet.h | 12 +++++++++++- src/server/context.c | 2 +- src/server/user.c | 2 +- src/server/user.h | 3 +++ 11 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 src/common.c diff --git a/protocol b/protocol index 8d435c9..389f492 100644 --- a/protocol +++ b/protocol @@ -3,7 +3,8 @@ PROTOCOL all numbers are packed unless stated otherwise packed values are msb -strings and blobs are prefixed with a packed ushort length +strings are predefined sizes and are terminated at either +the boundary of the region or the first NUL character (0x0) byte n is b.n region n is r.n @@ -22,11 +23,11 @@ byte the bytes after the header octet bytes bytes: client -> server id 0: registration attempt - r.0 - username - string + r.0 - username - string (max 8b) r.1 - pin - ushort (max 9999) id 1: login attempt - r.0 - username - string + r.0 - username - string (max 8b) r.1 - pin - ushort (max 9999) id 2: ctx change response @@ -38,12 +39,12 @@ client -> server server -> client id 0: registration response r.0 - succeeded - bool - r.1 - status - string + r.1 - status - string (max 16b) id 1: login response - r.0 - succeeded - bool - r.1 -> status - string (if r.0 false) - -> user id - ushort (if r.0 true) + r.0 - succeeded - bool + r.1 - user id - ushort + r.2 - status - string (max 16b) id 2: ctx change request r.0 - ctx id - ushort \ No newline at end of file diff --git a/src/client/client.c b/src/client/client.c index bee33ab..73c6c55 100644 --- a/src/client/client.c +++ b/src/client/client.c @@ -26,7 +26,7 @@ char MM_OPTIONS[MM_OPT_COUNT][MM_MAX_OPT_LEN + 1] = { struct { socket_t *sock; -} client_ctx; +} static ctx; static int main_menu(); static void how_to_play(); @@ -36,10 +36,10 @@ static void client_loop(); void client() { int selected, err; BOOL running = TRUE; - client_ctx.sock = sock_client_init("127.0.0.1", "6770"); + ctx.sock = sock_client_init("127.0.0.1", "6770"); printf("Connecting to server...\n"); - err = sock_start(client_ctx.sock); + err = sock_start(ctx.sock); if(err != SOCK_SUCCESS) { printf("Connection failed, error %d.", err); getchar(); diff --git a/src/client/ui.c b/src/client/ui.c index b6ff1f6..790b1cc 100644 --- a/src/client/ui.c +++ b/src/client/ui.c @@ -4,7 +4,7 @@ struct { WINDOW *main_win; WINDOW *chat_win; WINDOW *info_win; -} ctx; +} static ctx; static void ui_draw_borders(); diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..72dfc86 --- /dev/null +++ b/src/common.c @@ -0,0 +1,37 @@ +#include "common.h" + +/* HOST TO NETWORK CONVERSIONS */ + +static BOOL in_order = 0xFF; + +uint64_t htonll(uint64_t host) { + if(in_order = 0xFF) + in_order = htonl(1) == 1; + else if(in_order) + return host; + + return ((uint64_t)htonl(host & 0xFFFFFFFF) << 32) | htonl(host >> 32); +} + +uint64_t ntohll(uint64_t net) { + if(in_order = 0xFF) + in_order = htonl(1) == 1; + else if(in_order) + return net; + + return ((uint64_t)ntohl(net & 0xFFFFFFFF) << 32) | ntohl(net >> 32); +} + + +/* SPAWN TYPE REGISTRATION */ + +static uint8_t spawn_type = 0; + +void register_spawn_type(uint8_t type) { + if(spawn_type == 0) + spawn_type = type; +} + +uint8_t get_spawn_type() { + return spawn_type; +} \ No newline at end of file diff --git a/src/common.h b/src/common.h index aeb9bbe..9d73b43 100644 --- a/src/common.h +++ b/src/common.h @@ -1,6 +1,10 @@ #ifndef COMMON_H #define COMMON_H +#include +#include +#include + #define FALSE 0 #define TRUE 1 #define BOOL char @@ -9,4 +13,13 @@ #define MAX_CONNS 100 +uint64_t htonll(uint64_t); +uint64_t ntohll(uint64_t); + +#define SPAWN_UNDEF 0 +#define SPAWN_CLIENT 1 +#define SPAWN_SERVER 2 +void register_spawn_type(uint8_t); +uint8_t get_spawn_type(); + #endif diff --git a/src/main.c b/src/main.c index 907073e..2c0ce03 100644 --- a/src/main.c +++ b/src/main.c @@ -1,10 +1,24 @@ +#include "common.h" +#include "packet.h" #include "client/client.h" #include "server/server.h" int main(int argc, char **argv) { + packet_context_init(2); + + packet_context_register(PCK_CTX_C2S, PCK_ID_REGISTER, 2, 8, 2); + packet_context_register(PCK_CTX_C2S, PCK_ID_LOGIN, 2, 8, 2); + packet_context_register(PCK_CTX_C2S, PCK_ID_CHANGE_CTX, 2, 8, 2); + + packet_context_register(PCK_CTX_S2C, PCK_ID_REGISTER, 2, 1, 16); + packet_context_register(PCK_CTX_S2C, PCK_ID_LOGIN, 3, 1, 2, 16); + packet_context_register(PCK_CTX_S2C, PCK_ID_CHANGE_CTX, 1, 2); + if(argc == 2 && strcmp(argv[1], "server") == 0) { + register_spawn_type(SPAWN_SERVER); server(); } else { + register_spawn_type(SPAWN_CLIENT); client(); } } diff --git a/src/packet.c b/src/packet.c index 7eb5324..ce8be5b 100644 --- a/src/packet.c +++ b/src/packet.c @@ -1,2 +1,13 @@ #include "packet.h" +typedef struct packet_ctx_t packet_ctx_t; +struct packet_ctx_t { + uint8_t iter_start; + uint8_t region_count; + uint16_t *region_lengths; + uint32_t length; +}; + +static packet_ctx_t *c2s_ctx; +static packet_ctx_t *s2c_ctx; + diff --git a/src/packet.h b/src/packet.h index 97b5d2a..1ed3c19 100644 --- a/src/packet.h +++ b/src/packet.h @@ -4,6 +4,10 @@ #include #include #include "list.h" +#include "common.h" + +#define PCK_CTX_C2S 0 +#define PCK_CTX_S2C 1 #define PCK_ID_REGISTER 0 #define PCK_ID_LOGIN 1 @@ -14,10 +18,16 @@ struct packet_t { uint8_t id; uint32_t length; uint8_t **regions; - const uint8_t *region_lengths; + const uint16_t *region_lengths; uint8_t region_count; + uint8_t iterator; }; +void packet_context_init(uint8_t); + +void packet_context_register(uint8_t, uint8_t, uint8_t, ...); +void packet_context_register_iter(uint8_t, uint8_t, uint8_t, uint8_t, ...); + #endif \ No newline at end of file diff --git a/src/server/context.c b/src/server/context.c index b04d8ee..dd3cad4 100644 --- a/src/server/context.c +++ b/src/server/context.c @@ -3,7 +3,7 @@ struct { BOOL running; pthread_t thread; -} ctx; +} static ctx; void* server_context(void *params) { while(ctx.running == TRUE) { diff --git a/src/server/user.c b/src/server/user.c index a39d33c..0f1bac1 100644 --- a/src/server/user.c +++ b/src/server/user.c @@ -3,7 +3,7 @@ struct { list_t *active_users; user_t **users; -} ctx; +} static ctx; void user_context_init() { ctx.active_users = list_init(); diff --git a/src/server/user.h b/src/server/user.h index c1716da..da05f56 100644 --- a/src/server/user.h +++ b/src/server/user.h @@ -3,6 +3,9 @@ #include #include +#include "list.h" +#include "queue.h" +#include "sock.h" #include "common.h" typedef struct user_t user_t;