good day today park

This commit is contained in:
mallocnull 2017-10-26 21:56:07 +00:00
parent a949f596b8
commit 3944d713a7
11 changed files with 103 additions and 14 deletions

View file

@ -3,7 +3,8 @@ PROTOCOL
all numbers are packed unless stated otherwise all numbers are packed unless stated otherwise
packed values are msb 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 byte n is b.n
region n is r.n region n is r.n
@ -22,11 +23,11 @@ byte the bytes after the header octet bytes bytes:
client -> server client -> server
id 0: registration attempt id 0: registration attempt
r.0 - username - string r.0 - username - string (max 8b)
r.1 - pin - ushort (max 9999) r.1 - pin - ushort (max 9999)
id 1: login attempt id 1: login attempt
r.0 - username - string r.0 - username - string (max 8b)
r.1 - pin - ushort (max 9999) r.1 - pin - ushort (max 9999)
id 2: ctx change response id 2: ctx change response
@ -38,12 +39,12 @@ client -> server
server -> client server -> client
id 0: registration response id 0: registration response
r.0 - succeeded - bool r.0 - succeeded - bool
r.1 - status - string r.1 - status - string (max 16b)
id 1: login response id 1: login response
r.0 - succeeded - bool r.0 - succeeded - bool
r.1 -> status - string (if r.0 false) r.1 - user id - ushort
-> user id - ushort (if r.0 true) r.2 - status - string (max 16b)
id 2: ctx change request id 2: ctx change request
r.0 - ctx id - ushort r.0 - ctx id - ushort

View file

@ -26,7 +26,7 @@ char MM_OPTIONS[MM_OPT_COUNT][MM_MAX_OPT_LEN + 1] = {
struct { struct {
socket_t *sock; socket_t *sock;
} client_ctx; } static ctx;
static int main_menu(); static int main_menu();
static void how_to_play(); static void how_to_play();
@ -36,10 +36,10 @@ static void client_loop();
void client() { void client() {
int selected, err; int selected, err;
BOOL running = TRUE; 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"); printf("Connecting to server...\n");
err = sock_start(client_ctx.sock); err = sock_start(ctx.sock);
if(err != SOCK_SUCCESS) { if(err != SOCK_SUCCESS) {
printf("Connection failed, error %d.", err); printf("Connection failed, error %d.", err);
getchar(); getchar();

View file

@ -4,7 +4,7 @@ struct {
WINDOW *main_win; WINDOW *main_win;
WINDOW *chat_win; WINDOW *chat_win;
WINDOW *info_win; WINDOW *info_win;
} ctx; } static ctx;
static void ui_draw_borders(); static void ui_draw_borders();

37
src/common.c Normal file
View file

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

View file

@ -1,6 +1,10 @@
#ifndef COMMON_H #ifndef COMMON_H
#define COMMON_H #define COMMON_H
#include <stdint.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define FALSE 0 #define FALSE 0
#define TRUE 1 #define TRUE 1
#define BOOL char #define BOOL char
@ -9,4 +13,13 @@
#define MAX_CONNS 100 #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 #endif

View file

@ -1,10 +1,24 @@
#include "common.h"
#include "packet.h"
#include "client/client.h" #include "client/client.h"
#include "server/server.h" #include "server/server.h"
int main(int argc, char **argv) { 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) { if(argc == 2 && strcmp(argv[1], "server") == 0) {
register_spawn_type(SPAWN_SERVER);
server(); server();
} else { } else {
register_spawn_type(SPAWN_CLIENT);
client(); client();
} }
} }

View file

@ -1,2 +1,13 @@
#include "packet.h" #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;

View file

@ -4,6 +4,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include "list.h" #include "list.h"
#include "common.h"
#define PCK_CTX_C2S 0
#define PCK_CTX_S2C 1
#define PCK_ID_REGISTER 0 #define PCK_ID_REGISTER 0
#define PCK_ID_LOGIN 1 #define PCK_ID_LOGIN 1
@ -14,10 +18,16 @@ struct packet_t {
uint8_t id; uint8_t id;
uint32_t length; uint32_t length;
uint8_t **regions; uint8_t **regions;
const uint8_t *region_lengths; const uint16_t *region_lengths;
uint8_t region_count; 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 #endif

View file

@ -3,7 +3,7 @@
struct { struct {
BOOL running; BOOL running;
pthread_t thread; pthread_t thread;
} ctx; } static ctx;
void* server_context(void *params) { void* server_context(void *params) {
while(ctx.running == TRUE) { while(ctx.running == TRUE) {

View file

@ -3,7 +3,7 @@
struct { struct {
list_t *active_users; list_t *active_users;
user_t **users; user_t **users;
} ctx; } static ctx;
void user_context_init() { void user_context_init() {
ctx.active_users = list_init(); ctx.active_users = list_init();

View file

@ -3,6 +3,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include "list.h"
#include "queue.h"
#include "sock.h"
#include "common.h" #include "common.h"
typedef struct user_t user_t; typedef struct user_t user_t;