SQCSimulator2023/SQCSim-common/netprotocol.h
2023-10-25 10:00:54 -04:00

149 lines
4.1 KiB
C++

#ifndef NETPROTOCOL_H__
#define NETPROTOCOL_H__
#include "define.h"
#include <string>
#include "vector3.h"
/* Protocole Particulier de Partie à Plusieurs Personnes (PPPPP) */
// Packet: packet[0] = PacketType, packet[1..n-1] = {packet}
namespace netprot {
typedef uint8_t PacketType;
enum PACKET_TYPE {
ERR, INPUT, OUTPUT, SYNC,
TEAMINF, SELFINF, PLAYINF, LOGINF,
CHUNKMOD, PLAYERMOD, PICKUPMOD,
GAMEINFO, ENDINFO , CHAT, ERRLOG,
LAST_PACK
};
struct Packet {
void* ptr = nullptr;
PACKET_TYPE type;
};
struct Keys {
bool forward,
backward,
left,
right,
jump,
shoot,
block;
};
struct States {
bool jumping,
shooting,
hit,
powerup;
};
struct Input { // cli -> srv UDP ~frame
Timestamp timestamp;
uint64_t sid = 0;
Keys keys; // 0bFBLRJS__ bit-packing de bool.
Vector3f direction;
};
struct Output { // srv -> cli UDP ~frame
Timestamp timestamp;
uint64_t id = 0;
Vector3f position,
direction;
States states; // 0bJSH_____ bit-packing de bool.
};
struct Sync { // srv -> cli TCP ~second
Timestamp timestamp;
uint64_t sid = 0;
uint32_t timer = 0;
uint16_t ammo = 0;
uint8_t hp = 0;
Vector3f position;
};
struct TeamInfo { // cli <-> srv TCP once
char name[32];
uint64_t id = 0;
};
struct LoginInfo { // cli <-> srv TCP once
char name[32];
uint64_t sid = 0,
tid = 0;
};
struct PlayerInfo { // cli <-> srv TCP once
char name[32];
uint64_t id = 0,
tid = 0;
};
struct GameInfo { // cli <-> srv TCP event (before game start)/ once
uint64_t seed;
uint32_t countdown;
uint8_t gameType; // TOOD: enum.
};
struct Chat { // cli <-> srv TCP event
uint64_t src_id = 0,
dest_id = 0,
dest_team_id = 0;
char mess[140]; // Good 'nough for twitr, good 'nough for me.
};
struct ErrorLog { // srv -> cli TCP event
char mess[140];
bool is_fatal;
};
void Serialize(Input* in, char* buf[], uint32_t* buflen); // cli
void Serialize(Output* out, char* buf[], uint32_t* buflen); // srv
void Serialize(Sync* sync, char* buf[], uint32_t* buflen); // srv
void Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen); // cli/srv
void Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen); // cli/srv
void Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen); // srv
void Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen); // cli/srv
void Serialize(Chat* chat, char* buf[], uint32_t* buflen); // cli/srv
void Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen); // srv
bool Deserialize(Input* in, char* buf, const uint32_t buflen); // srv
bool Deserialize(Output* out, char* buf, const uint32_t buflen); // cli
bool Deserialize(Sync* sync, char* buf, const uint32_t buflen); // cli
bool Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen); // cli/srv
bool Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen); // cli/srv
bool Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen); // cli
bool Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen); // cli
bool Deserialize(Chat* chat, char* buf, const uint32_t buflen); // srv/cli
bool Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen); // srv
PacketType getType(char* buf, uint32_t buflen);
Packet getPack(char* buf, uint32_t buflen);
bool emptyPack(Packet pck);
template <class T> void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen);
template <class T> void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad);
template <class T>
void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) {
netprot::Serialize(pack, buf, buflen);
send(sock, *buf, *buflen, 0);
*buflen = BUFFER_LENGTH;
}
template <class T>
void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) {
sockaddr_in addr = *sockad;
netprot::Serialize(pack, buf, buflen);
sendto(sock, *buf, *buflen, 0, (sockaddr*)&addr, sizeof(addr));
*buflen = BUFFER_LENGTH;
}
};
#endif