good day today park
This commit is contained in:
parent
a949f596b8
commit
3944d713a7
11 changed files with 103 additions and 14 deletions
13
protocol
13
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.1 - user id - ushort
|
||||
r.2 - status - string (max 16b)
|
||||
|
||||
id 2: ctx change request
|
||||
r.0 - ctx id - ushort
|
|
@ -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();
|
||||
|
|
|
@ -4,7 +4,7 @@ struct {
|
|||
WINDOW *main_win;
|
||||
WINDOW *chat_win;
|
||||
WINDOW *info_win;
|
||||
} ctx;
|
||||
} static ctx;
|
||||
|
||||
static void ui_draw_borders();
|
||||
|
||||
|
|
37
src/common.c
Normal file
37
src/common.c
Normal 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;
|
||||
}
|
13
src/common.h
13
src/common.h
|
@ -1,6 +1,10 @@
|
|||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#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
|
||||
|
|
14
src/main.c
14
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();
|
||||
}
|
||||
}
|
||||
|
|
11
src/packet.c
11
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;
|
||||
|
||||
|
|
12
src/packet.h
12
src/packet.h
|
@ -4,6 +4,10 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdint.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_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
|
|
@ -3,7 +3,7 @@
|
|||
struct {
|
||||
BOOL running;
|
||||
pthread_t thread;
|
||||
} ctx;
|
||||
} static ctx;
|
||||
|
||||
void* server_context(void *params) {
|
||||
while(ctx.running == TRUE) {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
struct {
|
||||
list_t *active_users;
|
||||
user_t **users;
|
||||
} ctx;
|
||||
} static ctx;
|
||||
|
||||
void user_context_init() {
|
||||
ctx.active_users = list_init();
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include "list.h"
|
||||
#include "queue.h"
|
||||
#include "sock.h"
|
||||
#include "common.h"
|
||||
|
||||
typedef struct user_t user_t;
|
||||
|
|
Loading…
Reference in a new issue