big refractor: i got rid of all typedefs for

structs after reading some stuff online. took
me a few hours, but i managed to get it working
This commit is contained in:
hayden 2019-08-20 11:26:44 -05:00
parent 43143c2caf
commit 09412cbd97
14 changed files with 201 additions and 186 deletions

View File

@ -17,4 +17,4 @@ static bool COBBLE_ONLINE_MODE = false;
static uint16_t COBBLE_MAX_PLAYERS = 20;
/* the plugins array. add any plugins you want loaded to this array */
static Plugin COBBLE_PLUGINS[] = { };
static struct plugin COBBLE_PLUGINS[] = { };

View File

@ -8,23 +8,23 @@
#include <cobble/server.h>
typedef enum {
enum protocol_state {
HANDSHAKE = 0,
STATUS = 1,
LOGIN = 2,
PLAY = 3
} ProtocolState;
};
typedef struct {
struct connection {
dyad_Stream *dyad_stream;
Server *server;
struct server *server;
ProtocolState protocol_state;
enum protocol_state protocol_state;
int32_t protocol;
} Connection;
};
Connection* connection_create();
void connection_destroy(Connection*);
struct connection* connection_create();
void connection_destroy(struct connection*);
void connection_thread(void*);
#endif

View File

@ -9,7 +9,7 @@
#include <cobble/connection.h>
/* enum representing the 1.14.3 protocol */
typedef enum {
enum packet_type {
INVALID,
HANDSHAKE_SERVER_HANDSHAKE,
PLAY_CLIENT_SPAWN_OBJECT,
@ -163,20 +163,20 @@ typedef enum {
LOGIN_SERVER_LOGIN_START,
LOGIN_SERVER_ENCRYPTION_RESPONSE,
LOGIN_SERVER_LOGIN_PLUGIN_RESPONSE
} PacketType;
};
/* generic packet type */
typedef struct {
struct packet {
int32_t id;
PacketType type;
enum packet_type type;
void *data;
} Packet;
};
Packet* packet_new(PacketType packet_type, void* packet_data);
bool packet_read(Connection *conn, Packet *packet, uint8_t *data, size_t data_len);
bool packet_write(Connection *conn, Packet *packet);
size_t packet_size(Packet *packet);
void packet_destroy(Packet *packet);
void packet_handle(Connection *conn, Packet *packet);
struct packet* packet_new(enum packet_type, void*);
bool packet_read(struct connection*, struct packet*, uint8_t*, size_t);
bool packet_write(struct connection*, struct packet*);
size_t packet_size(struct packet*);
void packet_destroy(struct packet*);
void packet_handle(struct connection*, struct packet*);
#endif

View File

@ -5,113 +5,113 @@
#include <cobble/world.h>
/* HANDSHAKE_SERVER_HANDSHAKE */
typedef struct {
struct packet_handshake {
int32_t protocol;
char *server_address;
uint16_t server_port;
int32_t next_state;
} PacketHandshake;
};
/* PLAY_CLIENT_JOIN_GAME */
typedef struct {
struct packet_join_game {
int32_t entity_id;
Gamemode gamemode;
Dimension dimension;
Difficulty difficulty;
enum gamemode gamemode;
enum dimension dimension;
enum difficulty difficulty;
/* this field is ignored */
uint8_t max_players;
char *level_type;
bool reduced_debug_info;
} PacketJoinGame;
};
/* STATUS_CLIENT_RESPONSE */
typedef struct {
struct packet_response {
char *response;
} PacketResponse;
};
/* STATUS_CLIENT_PONG */
typedef struct {
struct packet_pong {
int64_t payload;
} PacketPong;
};
/* STATUS_SERVER_PING */
typedef struct {
struct packet_ping {
int64_t payload;
} PacketPing;
};
/* LOGIN_CLIENT_DISCONNECT */
typedef struct {
struct packet_login_disconnect {
char *reason;
} PacketLoginDisconnect;
};
/* LOGIN_CLIENT_ENCRYPTION_REQUEST */
typedef struct {
struct packet_encryption_request {
char *server_id;
int32_t public_key_len;
uint8_t *public_key;
int32_t verify_token_len;
uint8_t verify_token;
} PacketEncryptionRequest;
};
/* LOGIN_CLIENT_LOGIN_SUCCESS */
typedef struct {
struct packet_login_success {
char *uuid;
char *username;
} PacketLoginSuccess;
};
/* LOGIN_CLIENT_SET_COMPRESSION */
typedef struct {
struct packet_set_compression {
int32_t threshold;
} PacketSetCompression;
};
/* LOGIN_SERVER_LOGIN_START */
typedef struct {
struct packet_login_start {
char *username;
} PacketLoginStart;
};
/* LOGIN_SERVER_ENCRYPTION_RESPONSE */
typedef struct {
struct packet_encryption_response {
int32_t shared_secret_len;
uint8_t *shared_secret;
int32_t verify_token_len;
uint8_t *verify_token;
} PacketEncryptionResponse;
};
/* HANDSHAKE_SERVER_HANDSHAKE */
bool packet_handshake_read(Connection* conn, Packet *packet, uint8_t *data, size_t data_len);
void packet_handshake_destroy(Packet *packet);
void packet_handshake_handle(Connection* conn, Packet *packet);
bool packet_handshake_read(struct connection*, struct packet*, uint8_t *data, size_t data_len);
void packet_handshake_destroy(struct packet*);
void packet_handshake_handle(struct connection*, struct packet*);
/* PLAY_CLIENT_JOIN_GAME */
bool packet_join_game_read(Connection* conn, Packet *packet, uint8_t *data, size_t data_len);
void packet_join_game_destroy(Packet *packet);
void packet_join_game_handle(Connection* conn, Packet *packet);
bool packet_join_game_read(struct connection*, struct packet*, uint8_t *data, size_t data_len);
void packet_join_game_destroy(struct packet*);
void packet_join_game_handle(struct connection*, struct packet*);
/* STATUS_CLIENT_RESPONSE */
bool packet_response_write(Connection *conn, Packet *packet, uint8_t *buf, size_t data_len);
size_t packet_response_size(Packet *packet);
void packet_response_destroy(Packet *packet);
bool packet_response_write(struct connection*, struct packet*, uint8_t *buf, size_t data_len);
size_t packet_response_size(struct packet*);
void packet_response_destroy(struct packet*);
/* STATUS_CLIENT_PONG */
bool packet_pong_write(Connection *conn, Packet *packet, uint8_t *buf, size_t data_len);
size_t packet_pong_size(Packet *packet);
bool packet_pong_write(struct connection*, struct packet*, uint8_t *buf, size_t data_len);
size_t packet_pong_size(struct packet*);
/* STATUS_SERVER_REQUEST */
bool packet_request_read(Connection *conn, Packet *packet, uint8_t *data, size_t data_len);
void packet_request_handle(Connection *conn, Packet *packet);
bool packet_request_read(struct connection*, struct packet*, uint8_t *data, size_t data_len);
void packet_request_handle(struct connection*, struct packet*);
/* STATUS_SERVER_PING */
bool packet_ping_read(Connection* conn, Packet *packet, uint8_t *data, size_t data_len);
void packet_ping_handle(Connection* conn, Packet *packet);
bool packet_ping_read(struct connection*, struct packet*, uint8_t *data, size_t data_len);
void packet_ping_handle(struct connection*, struct packet*);
/* LOGIN_CLIENT_LOGIN_SUCCESS */
bool packet_login_success_write(Connection *conn, Packet *packet, uint8_t *buf, size_t data_len);
size_t packet_login_success_size(Packet *packet);
void packet_login_success_destroy(Packet *packet);
bool packet_login_success_write(struct connection*, struct packet*, uint8_t *buf, size_t data_len);
size_t packet_login_success_size(struct packet*);
void packet_login_success_destroy(struct packet*);
/* LOGIN_SERVER_LOGIN_START */
bool packet_login_start_read(Connection* conn, Packet *packet, uint8_t *data, size_t data_len);
void packet_login_start_destroy(Packet *packet);
void packet_login_start_handle(Connection* conn, Packet *packet);
bool packet_login_start_read(struct connection*, struct packet*, uint8_t *data, size_t data_len);
void packet_login_start_destroy(struct packet*);
void packet_login_start_handle(struct connection*, struct packet*);
#endif

View File

@ -2,10 +2,10 @@
#ifndef COBBLE_PROTOCOL_H
#define COBBLE_PROTOCOL_H
typedef struct __attribute__((__packed__)) {
struct __attribute__((__packed__)) uuid {
uint64_t part1;
uint64_t part2;
} Uuid;
};
/* read */
size_t read_long(int64_t *output, uint8_t *buf, size_t buf_len);
@ -28,6 +28,6 @@ size_t get_varint_len(int32_t input);
size_t get_varlong_len(int64_t input);
size_t get_string_len(char* input);
/* other */
bool generate_uuid(char *username, Uuid *uuid);
bool generate_uuid(char *username, struct uuid*);
#endif

View File

@ -5,16 +5,16 @@
#include <stdbool.h>
#include <rxi/dyad.h>
typedef struct {
struct server {
// for connection stuff
dyad_Stream *dyad_stream;
} Server;
};
typedef enum {
enum difficulty {
DIFFICULTY_PEACEFUL = 0,
DIFFICULTY_EASY = 1,
DIFFICULTY_NORMAL = 2,
DIFFICULTY_HARD = 3,
} Difficulty;
};
#endif

View File

@ -4,16 +4,16 @@
#include <stdint.h>
typedef enum {
enum dimension {
DIMENSION_NETHER = -1,
DIMENSION_OVERWORLD = 0,
DIMENSION_END = 1,
} Dimension;
};
typedef struct {
struct world {
uint64_t seed;
Dimension dimension;
} World;
enum dimension dimension;
};
/* maybe i could use an enum and reference array for this? */
static const char *LEVEL_TYPE_DEFAULT = "default";
@ -24,6 +24,6 @@ static const char *LEVEL_TYPE_CUSTOMIZED = "customized";
static const char *LEVEL_TYPE_BUFFET = "buffet";
static const char *LEVEL_TYPE_DEFAULT_1_1 = "default_1_1";
bool world_load(char *path, World *world);
bool world_load(char *path, struct world*);
#endif

View File

@ -17,12 +17,12 @@
static void
connection_on_data(dyad_Event *e)
{
Connection *conn;
Packet *packet;
struct connection *conn;
struct packet *packet;
size_t len_size;
int len;
conn = (Connection*) e->udata;
conn = (struct connection*) e->udata;
len_size = read_varint(&len, e->data, e->size);
if (len_size == 0 || e->size < len) {
@ -32,7 +32,7 @@ connection_on_data(dyad_Event *e)
return;
}
packet = SAFE_MALLOC(sizeof(Packet));
packet = SAFE_MALLOC(sizeof(struct packet));
packet_read(conn, packet, e->data + len_size, len);
packet_handle(conn, packet);
packet_destroy(packet);
@ -41,9 +41,9 @@ connection_on_data(dyad_Event *e)
static void
connection_on_close(dyad_Event *e)
{
Connection *conn;
struct connection *conn;
conn = (Connection*) e->data;
conn = (struct connection*) e->data;
free(conn);
}
@ -51,12 +51,12 @@ connection_on_close(dyad_Event *e)
static void
server_on_accept(dyad_Event *e)
{
Connection *conn;
Server *server;
struct connection *conn;
struct server *server;
server = (Server*) e->udata;
server = (struct server*) e->udata;
conn = SAFE_MALLOC(sizeof(Connection));
conn = SAFE_MALLOC(sizeof(struct connection));
conn->dyad_stream = e->remote;
conn->server = server;
conn->protocol_state = HANDSHAKE;
@ -69,7 +69,9 @@ server_on_accept(dyad_Event *e)
void
connection_thread(void *args)
{
Server *server = (Server*) args;
struct server *server;
server = (struct server*) args;
dyad_addListener(server->dyad_stream, DYAD_EVENT_ACCEPT, server_on_accept, server);
/* TODO make the backlog count (last arg) configurable or something */
dyad_listenEx(server->dyad_stream, COBBLE_SERVER_IP, COBBLE_SERVER_PORT, 128);

View File

@ -13,12 +13,10 @@
#include <cobble/server.h>
#include <cobble/util.h>
static void tick(Server*);
static void tick_thread(void*);
static void
tick(Server* server)
tick(struct server *server)
{
log_info("tick");
/* for now the main thread does nothing */
}
@ -26,7 +24,7 @@ static void
tick_thread(void *args)
{
while (1) {
tick((Server*) args);
tick((struct server*) args);
usleep(1000 * 1000);
}
}
@ -34,13 +32,15 @@ tick_thread(void *args)
int
main(int argc, char** argv)
{
struct server *server;
log_info("Starting server");
// create and setup the server struct
Server *server = SAFE_MALLOC(sizeof(Server));
server = SAFE_MALLOC(sizeof(struct server));
log_info("Loading world");
FILE* f = fopen("level.dat", "rb");
NBTNode *node;
struct nbt_node *node;
nbt_parse_file(node, f);
fclose(f);

View File

@ -65,17 +65,17 @@ decompression_error:
/* recursive function used to read in an NBTNode, and all child nodes */
static size_t
nbt_parse_node(NBTNode *node, uint8_t *data, size_t data_len)
nbt_parse_node(struct nbt_node *node, uint8_t *data, size_t data_len)
{
/* keeps track of the number of bytes we have read */
size_t size;
size = 0;
node = SAFE_MALLOC(sizeof(NBTNode));
node = SAFE_MALLOC(sizeof(struct nbt_node));
node->name_len = 0;
node->name = NULL;
node->type = (NBTType) data[0];
node->type = (enum nbt_type) data[0];
size++;
/* if this is an end tag, there is no need to read in the name */
@ -105,7 +105,7 @@ nbt_parse_node(NBTNode *node, uint8_t *data, size_t data_len)
}
bool
nbt_parse_file(NBTNode *node, FILE *fp)
nbt_parse_file(struct nbt_node *node, FILE *fp)
{
void *data;
size_t file_len;
@ -122,7 +122,7 @@ nbt_parse_file(NBTNode *node, FILE *fp)
}
bool
nbt_parse_bytes(NBTNode *node, uint8_t *data, size_t data_len)
nbt_parse_bytes(struct nbt_node *node, uint8_t *data, size_t data_len)
{
uint8_t *idata;
size_t idata_len;

View File

@ -167,8 +167,8 @@ static const int32_t packet_id_lookup[] = {
[LOGIN_SERVER_LOGIN_PLUGIN_RESPONSE] = 0x02
};
/* lookup table for id and state to PacketType. this is only used for reading, so only serverbound packets will be included */
static const PacketType packet_type_lookup[][4] = {
/* lookup table for id and state to packet_type. this is only used for reading, so only serverbound packets will be included */
static const enum packet_type packet_type_lookup[][4] = {
[0x00] = { [HANDSHAKE] = HANDSHAKE_SERVER_HANDSHAKE, [PLAY] = PLAY_SERVER_TELEPORT_CONFIRM, [STATUS] = STATUS_SERVER_REQUEST, [LOGIN] = LOGIN_SERVER_LOGIN_START },
[0x01] = { [HANDSHAKE] = INVALID, [PLAY] = PLAY_SERVER_QUERY_BLOCK_NBT, [STATUS] = STATUS_SERVER_PING, [LOGIN] = LOGIN_SERVER_ENCRYPTION_RESPONSE },
[0x02] = { [HANDSHAKE] = INVALID, [PLAY] = PLAY_SERVER_SET_DIFFICULTY, [STATUS] = INVALID, [LOGIN] = LOGIN_SERVER_LOGIN_PLUGIN_RESPONSE },
@ -219,11 +219,11 @@ static const PacketType packet_type_lookup[][4] = {
/* lookup table and storage struct for packet functions */
typedef struct {
bool (*read)(Connection *conn, Packet *packet, uint8_t *data, size_t data_len);
bool (*write)(Connection *conn, Packet *packet, uint8_t *buf, size_t buf_len);
size_t (*size)(Packet *packet);
void (*destroy)(Packet *packet);
void (*handle)(Connection *conn, Packet *packet);
bool (*read)(struct connection*, struct packet*, uint8_t *data, size_t data_len);
bool (*write)(struct connection*, struct packet*, uint8_t *buf, size_t buf_len);
size_t (*size)(struct packet*);
void (*destroy)(struct packet*);
void (*handle)(struct connection*, struct packet*);
} PacketFunctions;
static const PacketFunctions packet_function_lookup[] = {
@ -409,10 +409,12 @@ static const PacketFunctions packet_function_lookup[] = {
[LOGIN_SERVER_LOGIN_PLUGIN_RESPONSE] = { NULL, NULL, NULL, NULL, NULL }
};
Packet*
packet_new(PacketType type, void* packet_data)
struct packet*
packet_new(enum packet_type type, void *packet_data)
{
Packet *packet = (Packet*) malloc(sizeof(Packet));
struct packet *packet;
packet = malloc(sizeof(struct packet));
packet->id = packet_id_lookup[type];
packet->type = type;
packet->data = packet_data;
@ -420,10 +422,10 @@ packet_new(PacketType type, void* packet_data)
}
bool
packet_read(Connection *conn, Packet *packet, uint8_t *data, size_t data_len)
packet_read(struct connection *conn, struct packet *packet, uint8_t *data, size_t data_len)
{
size_t id_size;
bool (*read_function)(Connection *conn, Packet *packet, uint8_t *data, size_t data_len);
bool (*read_function)(struct connection*, struct packet*, uint8_t *data, size_t data_len);
id_size = read_varint(&packet->id, data, data_len);
log_debug("Reading packet with id %d", packet->id);
@ -449,11 +451,11 @@ packet_read(Connection *conn, Packet *packet, uint8_t *data, size_t data_len)
}
bool
packet_write(Connection *conn, Packet *packet)
packet_write(struct connection *conn, struct packet *packet)
{
uint8_t *buf;
size_t p_size, buf_size, data_size, id_size, data_size_size;
bool (*write_function)(Connection *conn, Packet *packet, uint8_t *buf, size_t buf_len);
bool (*write_function)(struct connection*, struct packet*, uint8_t *buf, size_t buf_len);
log_debug("Writing packet with id %d", packet->id);
p_size = packet_size(packet);
@ -490,9 +492,9 @@ packet_write(Connection *conn, Packet *packet)
}
size_t
packet_size(Packet *packet)
packet_size(struct packet *packet)
{
size_t (*size_function)(Packet *packet);
size_t (*size_function)(struct packet*);
size_function = packet_function_lookup[packet->type].size;
@ -505,9 +507,9 @@ packet_size(Packet *packet)
}
void
packet_destroy(Packet *packet)
packet_destroy(struct packet *packet)
{
void (*destroy_function)(Packet *packet);
void (*destroy_function)(struct packet*);
destroy_function = packet_function_lookup[packet->type].destroy;
if (destroy_function == NULL) {
@ -522,9 +524,9 @@ packet_destroy(Packet *packet)
}
void
packet_handle(Connection *conn, Packet *packet)
packet_handle(struct connection *conn, struct packet *packet)
{
void (*handle_function)(Connection *conn, Packet *packet);
void (*handle_function)(struct connection*, struct packet*);
handle_function = packet_function_lookup[packet->type].handle;
if (handle_function == NULL) {

View File

@ -21,13 +21,13 @@
/* HANDSHAKE_SERVER_HANDSHAKE =============================================== */
bool
packet_handshake_read(Connection* conn, Packet *packet, uint8_t *buf, size_t len)
packet_handshake_read(struct connection *conn, struct packet *packet, uint8_t *buf, size_t len)
{
PacketHandshake *p;
struct packet_handshake *p;
size_t size;
size = 0;
p = SAFE_MALLOC(sizeof(PacketHandshake));
p = SAFE_MALLOC(sizeof(struct packet_handshake));
size += read_varint(&p->protocol, buf + size, len - size);
size += read_string(&p->server_address, buf + size, len - size);
@ -39,16 +39,20 @@ packet_handshake_read(Connection* conn, Packet *packet, uint8_t *buf, size_t len
}
void
packet_handshake_destroy(Packet *p)
packet_handshake_destroy(struct packet *p)
{
PacketHandshake *packet = (PacketHandshake*) p->data;
struct packet_handshake *packet;
packet = (struct packet_handshake*) p->data;
free(packet->server_address);
}
void
packet_handshake_handle(Connection *conn, Packet *p)
packet_handshake_handle(struct connection *conn, struct packet *p)
{
PacketHandshake *packet = (PacketHandshake*) p->data;
struct packet_handshake *packet;
packet = (struct packet_handshake*) p->data;
conn->protocol = packet->protocol;
@ -62,12 +66,12 @@ packet_handshake_handle(Connection *conn, Packet *p)
/* PLAY_CLIENT_JOIN_GAME ---------------------------------------------------= */
bool
packet_join_game_write(Connection* conn, Packet *p, uint8_t *data, size_t data_len)
packet_join_game_write(struct connection *conn, struct packet *p, uint8_t *data, size_t data_len)
{
PacketJoinGame *packet;
struct packet_join_game *packet;
size_t size;
packet = (PacketJoinGame*) p->data;
packet = (struct packet_join_game*) p->data;
size = 0;
size += write_int(packet->entity_id, data + size);
@ -82,63 +86,63 @@ packet_join_game_write(Connection* conn, Packet *p, uint8_t *data, size_t data_l
}
size_t
packet_join_game_size(Packet *packet)
packet_join_game_size(struct packet *packet)
{
/* 12 is the size of all the fields but the level_type string */
return 12 + get_string_len(((PacketJoinGame*) packet->data)->level_type);
return 12 + get_string_len(((struct packet_join_game*) packet->data)->level_type);
}
void
packet_join_game_destroy(Packet *p)
packet_join_game_destroy(struct packet *p)
{
PacketJoinGame *packet;
struct packet_join_game *packet;
packet = (PacketJoinGame*) p->data;
packet = (struct packet_join_game*) p->data;
free(packet->level_type);
}
/* STATUS_CLIENT_RESPONSE =================================================== */
bool
packet_response_write(Connection *conn, Packet *packet, uint8_t *buf, size_t len)
packet_response_write(struct connection *conn, struct packet *packet, uint8_t *buf, size_t len)
{
PacketResponse *p;
struct packet_response *p;
p = (PacketResponse*) packet->data;
p = (struct packet_response*) packet->data;
write_string(p->response, buf);
return true;
}
size_t
packet_response_size(Packet *packet)
packet_response_size(struct packet *packet)
{
return get_string_len(((PacketResponse*) packet->data)->response);
return get_string_len(((struct packet_response*) packet->data)->response);
}
void
packet_response_destroy(Packet *packet)
packet_response_destroy(struct packet *packet)
{
PacketResponse*p;
struct packet_response *p;
p = (PacketResponse*) packet->data;
p = (struct packet_response*) packet->data;
free(p->response);
}
/* STATUS_CLIENT_PONG ======================================================= */
bool
packet_pong_write(Connection *conn, Packet *packet, uint8_t *buf, size_t len)
packet_pong_write(struct connection *conn, struct packet *packet, uint8_t *buf, size_t len)
{
PacketPong *p;
struct packet_pong *p;
p = (PacketPong*) packet->data;
p = (struct packet_pong*) packet->data;
write_long(p->payload, buf);
return true;
}
size_t
packet_pong_size(Packet *packet)
packet_pong_size(struct packet *packet)
{
return 8;
}
@ -147,15 +151,19 @@ packet_pong_size(Packet *packet)
#define RESPONSE_BUFFER_SIZE 1024
bool
packet_request_read(Connection* conn, Packet* packet, uint8_t *data, size_t data_len)
packet_request_read(struct connection *conn, struct packet* packet, uint8_t *data, size_t data_len)
{
return true;
}
void
packet_request_handle(Connection *conn, Packet *p)
packet_request_handle(struct connection *conn, struct packet *p)
{
PacketResponse *packet_data = SAFE_MALLOC(sizeof(PacketResponse));
struct packet_response *packet_data;
struct packet *new_packet;
packet_data = SAFE_MALLOC(sizeof(struct packet_response));
packet_data->response = SAFE_MALLOC(sizeof(char) * RESPONSE_BUFFER_SIZE);
packet_data->response[RESPONSE_BUFFER_SIZE - 1] = 0;
/* TODO actually get all of this */
@ -165,7 +173,7 @@ packet_request_handle(Connection *conn, Packet *p)
COBBLE_MAX_PLAYERS,
COBBLE_SERVER_MOTD);
Packet *new_packet = packet_new(STATUS_CLIENT_RESPONSE, packet_data);
new_packet = packet_new(STATUS_CLIENT_RESPONSE, packet_data);
if (!packet_write(conn, new_packet)) {
log_error("Failed to write STATUS_CLIENT_RESPONSE packet");
}
@ -174,11 +182,11 @@ packet_request_handle(Connection *conn, Packet *p)
/* STATUS_SERVER_PING ======================================================= */
bool
packet_ping_read(Connection* conn, Packet *packet, uint8_t *buf, size_t len)
packet_ping_read(struct connection *conn, struct packet *packet, uint8_t *buf, size_t len)
{
PacketPing *p;
struct packet_ping *p;
p = SAFE_MALLOC(sizeof(PacketPing));
p = SAFE_MALLOC(sizeof(struct packet_ping));
read_long(&p->payload, buf, len);
packet->data = (void*) p;
@ -186,14 +194,18 @@ packet_ping_read(Connection* conn, Packet *packet, uint8_t *buf, size_t len)
}
void
packet_ping_handle(Connection *conn, Packet *p)
packet_ping_handle(struct connection *conn, struct packet *p)
{
PacketPing *ping_data = (PacketPing*) p->data;
PacketPong *pong_data = SAFE_MALLOC(sizeof(PacketPong));
struct packet_ping *ping_data;
struct packet_pong *pong_data;
struct packet *new_packet;
ping_data = (struct packet_ping*) p->data;
pong_data = SAFE_MALLOC(sizeof(struct packet_pong));
pong_data->payload = ping_data->payload;
Packet *new_packet = packet_new(STATUS_CLIENT_PONG, pong_data);
new_packet = packet_new(STATUS_CLIENT_PONG, pong_data);
if (!packet_write(conn, new_packet)) {
log_error("Failed to write STATUS_CLIENT_RESPONSE packet");
@ -201,15 +213,14 @@ packet_ping_handle(Connection *conn, Packet *p)
packet_destroy(new_packet);
}
/* LOGIN_CLIENT_LOGIN_SUCCESS =============================================== */
bool
packet_login_success_write(Connection *conn, Packet *p, uint8_t *buf, size_t data_len)
packet_login_success_write(struct connection *conn, struct packet *p, uint8_t *buf, size_t data_len)
{
PacketLoginSuccess *packet;
struct packet_login_success *packet;
size_t size;
packet = (PacketLoginSuccess*) p->data;
packet = (struct packet_login_success*) p->data;
size = 0;
size += write_string(packet->uuid, buf);
@ -219,12 +230,12 @@ packet_login_success_write(Connection *conn, Packet *p, uint8_t *buf, size_t dat
}
size_t
packet_login_success_size(Packet *p)
packet_login_success_size(struct packet *p)
{
PacketLoginSuccess *packet;
struct packet_login_success *packet;
size_t size;
packet = (PacketLoginSuccess*) p->data;
packet = (struct packet_login_success*) p->data;
size = 0;
size += get_string_len(packet->uuid);
size += get_string_len(packet->username);
@ -233,11 +244,11 @@ packet_login_success_size(Packet *p)
}
void
packet_login_success_destroy(Packet *p)
packet_login_success_destroy(struct packet *p)
{
PacketLoginSuccess *packet;
struct packet_login_success *packet;
packet = (PacketLoginSuccess*) p->data;
packet = (struct packet_login_success*) p->data;
free(packet->uuid);
free(packet->username);
@ -245,11 +256,11 @@ packet_login_success_destroy(Packet *p)
/* LOGIN_SERVER_LOGIN_START ================================================= */
bool
packet_login_start_read(Connection* conn, Packet *p, uint8_t *buf, size_t len)
packet_login_start_read(struct connection *conn, struct packet *p, uint8_t *buf, size_t len)
{
PacketLoginStart *packet;
struct packet_login_start *packet;
packet = SAFE_MALLOC(sizeof(PacketLoginStart));
packet = SAFE_MALLOC(sizeof(struct packet_login_start));
read_string(&packet->username, buf, len);
p->data = packet;
@ -258,31 +269,31 @@ packet_login_start_read(Connection* conn, Packet *p, uint8_t *buf, size_t len)
}
void
packet_login_start_destroy(Packet *p)
packet_login_start_destroy(struct packet *p)
{
PacketLoginStart *packet;
struct packet_login_start *packet;
packet = (PacketLoginStart*) p->data;
packet = (struct packet_login_start*) p->data;
free(packet->username);
}
void
packet_login_start_handle(Connection* conn, Packet *p)
packet_login_start_handle(struct connection *conn, struct packet *p)
{
PacketLoginStart *login_start_data;
login_start_data = (PacketLoginStart*) p->data;
struct packet_login_start *login_start_data;
login_start_data = (struct packet_login_start*) p->data;
if (COBBLE_ONLINE_MODE) {
log_error("Online mode login isn't yet implimented!");
} else {
PacketLoginSuccess *login_success_data;
Packet *new_packet;
Uuid *uuid;
struct packet_login_success *login_success_data;
struct packet *new_packet;
struct uuid *uuid;
login_success_data = SAFE_MALLOC(sizeof(PacketLoginSuccess));
login_success_data = SAFE_MALLOC(sizeof(struct packet_login_success));
uuid = SAFE_MALLOC(sizeof(struct uuid));
/* create a uuid by md5 hashing the players username, then encode it into a string */
uuid = (Uuid*) SAFE_MALLOC(sizeof(Uuid));
generate_uuid(login_start_data->username, uuid);
login_success_data->uuid = SAFE_MALLOC(38);
snprintf(login_success_data->uuid , 10, "%08X-", ((uint32_t*) uuid)[0]);

View File

@ -221,7 +221,7 @@ get_string_len(char* input)
}
bool
generate_uuid(char *username, Uuid *uuid)
generate_uuid(char *username, struct uuid *uuid)
{
MD5_CTX context;
MD5_Init(&context);

View File

@ -4,7 +4,7 @@
#include <cobble/world.h>
bool
world_load(char *path, World *world)
world_load(char *path, struct world *world)
{
/* TODO actually do this */
return false;