Began updating server data definitions.
This commit is contained in:
28
server/src/api.c
Normal file
28
server/src/api.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "include/data.h"
|
||||
|
||||
PacketType packet_type_parse(const char* type_str) {
|
||||
if (!type_str) return PACKET_TYPE_INVALID;
|
||||
if (!strcmp(type_str, "join")) return PACKET_TYPE_JOIN;
|
||||
if (!strcmp(type_str, "JOIN_EV")) return PACKET_TYPE_JOIN_EV;
|
||||
return PACKET_TYPE_INVALID;
|
||||
}
|
||||
|
||||
Packet* packet_init(PacketType type, void* data) {
|
||||
Packet* packet = malloc(sizeof(Packet));
|
||||
if (!packet) return NULL;
|
||||
|
||||
packet->type = type;
|
||||
packet->data = data;
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
Packet* packet_init_untrusted(PacketType type, void* data) {
|
||||
Packet* packet = malloc(sizeof(Packet));
|
||||
if (!packet) return NULL;
|
||||
|
||||
packet->type = type;
|
||||
packet->data = data;
|
||||
|
||||
return packet;
|
||||
}
|
||||
13
server/src/data.c
Normal file
13
server/src/data.c
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "include/data.h"
|
||||
|
||||
int name_verify(const char* name) {
|
||||
if (!name) return 0;
|
||||
if (strlen(name) > NAME_MAX_LENGTH) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int msg_verify(const char* content) {
|
||||
if (!content) return 0;
|
||||
if (strlen(content) > MSG_MAX_LENGTH) return 0;
|
||||
return 1;
|
||||
}
|
||||
41
server/src/include/api.h
Normal file
41
server/src/include/api.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef API__H
|
||||
#define API__H
|
||||
|
||||
#include "data.h"
|
||||
|
||||
typedef enum {
|
||||
PACKET_TYPE_JOIN,
|
||||
PACKET_TYPE_WELCOME,
|
||||
PACKET_TYPE_JOIN_EV,
|
||||
} PacketType;
|
||||
|
||||
static const char* packet_type_strings[] = {
|
||||
[PACKET_TYPE_JOIN]= "join",
|
||||
[PACKET_TYPE_WELCOME]= "welcome",
|
||||
[PACKET_TYPE_JOIN_EV]= "join_ev",
|
||||
};
|
||||
|
||||
PacketType packet_type_parse(const char* type_str);
|
||||
|
||||
typedef struct {
|
||||
PacketType type;
|
||||
void* data;
|
||||
} Packet;
|
||||
|
||||
Packet* packet_init(PacketType type, void* data);
|
||||
Packet* packet_init_untrusted(PacketType type, void* data);
|
||||
|
||||
typedef struct {
|
||||
Name name;
|
||||
} PacketJoin;
|
||||
|
||||
typedef struct {
|
||||
UserData* online;
|
||||
MsgData* history;
|
||||
} PacketWelcome;
|
||||
|
||||
typedef struct {
|
||||
UserData newguy;
|
||||
} PacketJoinEvent;
|
||||
|
||||
#endif
|
||||
31
server/src/include/data.h
Normal file
31
server/src/include/data.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef DATA__H
|
||||
#define DATA__H
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#define NAME_MAX_LENGTH 16
|
||||
#define MSG_MAX_LENGTH 1024
|
||||
|
||||
typedef int UserID;
|
||||
typedef int MsgID;
|
||||
|
||||
typedef char Name[NAME_MAX_LENGTH];
|
||||
int name_verify(const char* name);
|
||||
|
||||
typedef char MsgContent[MSG_MAX_LENGTH];
|
||||
int msg_verify(const char* content);
|
||||
|
||||
typedef struct {
|
||||
UserID id;
|
||||
Name name;
|
||||
} UserData;
|
||||
|
||||
typedef struct {
|
||||
MsgID id;
|
||||
UserData author;
|
||||
MsgID parent;
|
||||
MsgContent content;
|
||||
time_t timestamp;
|
||||
} MsgData;
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user