SQCSimulator2023/SQCSim-common/netprotocol.h

149 lines
4.1 KiB
C
Raw Normal View History

2023-09-25 08:23:52 -04:00
#ifndef NETPROTOCOL_H__
#define NETPROTOCOL_H__
2023-09-30 14:54:39 -04:00
2023-09-25 08:23:52 -04:00
#include "define.h"
2023-09-25 16:30:03 -04:00
#include <string>
2023-09-25 08:23:52 -04:00
#include "vector3.h"
/* Protocole Particulier de Partie <20> Plusieurs Personnes (PPPPP) */
2023-09-25 16:30:03 -04:00
// Packet: packet[0] = PacketType, packet[1..n-1] = {packet}
2023-09-25 08:23:52 -04:00
namespace netprot {
typedef uint8_t PacketType;
enum PACKET_TYPE {
ERR, INPUT, OUTPUT, SYNC,
2023-09-25 16:30:03 -04:00
TEAMINF, SELFINF, PLAYINF, LOGINF,
2023-09-25 08:23:52 -04:00
CHUNKMOD, PLAYERMOD, PICKUPMOD,
2023-09-25 16:30:03 -04:00
GAMEINFO, ENDINFO , CHAT, ERRLOG,
LAST_PACK
2023-09-25 08:23:52 -04:00
};
2023-10-24 10:43:30 -04:00
struct Packet {
void* ptr = nullptr;
2023-10-24 10:43:30 -04:00
PACKET_TYPE type;
};
2023-09-29 12:04:08 -04:00
struct Keys {
bool forward,
backward,
left,
right,
jump,
2023-09-30 10:27:05 -04:00
shoot,
block;
2023-09-29 12:04:08 -04:00
};
struct States {
bool jumping,
shooting,
hit,
powerup;
};
2023-09-25 16:30:03 -04:00
struct Input { // cli -> srv UDP ~frame
2023-09-25 08:23:52 -04:00
Timestamp timestamp;
2023-09-29 12:04:08 -04:00
uint64_t sid = 0;
Keys keys; // 0bFBLRJS__ bit-packing de bool.
2023-09-25 08:23:52 -04:00
Vector3f direction;
2023-09-25 16:30:03 -04:00
};
2023-09-25 08:23:52 -04:00
2023-09-25 16:30:03 -04:00
struct Output { // srv -> cli UDP ~frame
2023-09-25 08:23:52 -04:00
Timestamp timestamp;
uint64_t id = 0;
Vector3f position,
2023-09-29 12:04:08 -04:00
direction;
States states; // 0bJSH_____ bit-packing de bool.
2023-09-25 16:30:03 -04:00
};
2023-09-25 08:23:52 -04:00
2023-09-25 16:30:03 -04:00
struct Sync { // srv -> cli TCP ~second
2023-09-25 08:23:52 -04:00
Timestamp timestamp;
2023-09-25 08:53:23 -04:00
uint64_t sid = 0;
uint32_t timer = 0;
2023-09-25 08:23:52 -04:00
uint16_t ammo = 0;
2023-09-25 08:53:23 -04:00
uint8_t hp = 0;
2023-09-25 08:23:52 -04:00
Vector3f position;
2023-09-25 16:30:03 -04:00
};
2023-09-25 08:23:52 -04:00
2023-09-25 16:30:03 -04:00
struct TeamInfo { // cli <-> srv TCP once
2023-09-25 08:23:52 -04:00
char name[32];
uint64_t id = 0;
2023-09-25 16:30:03 -04:00
};
2023-09-25 08:23:52 -04:00
2023-09-25 16:30:03 -04:00
struct LoginInfo { // cli <-> srv TCP once
2023-09-25 08:23:52 -04:00
char name[32];
uint64_t sid = 0,
tid = 0;
2023-09-25 16:30:03 -04:00
};
2023-09-25 08:23:52 -04:00
2023-09-25 16:30:03 -04:00
struct PlayerInfo { // cli <-> srv TCP once
2023-09-25 08:23:52 -04:00
char name[32];
uint64_t id = 0,
tid = 0;
2023-09-25 16:30:03 -04:00
};
struct GameInfo { // cli <-> srv TCP event (before game start)/ once
2023-09-25 08:23:52 -04:00
uint64_t seed;
2023-09-25 08:53:23 -04:00
uint32_t countdown;
uint8_t gameType; // TOOD: enum.
2023-09-25 16:30:03 -04:00
};
2023-09-25 08:23:52 -04:00
2023-09-25 16:30:03 -04:00
struct Chat { // cli <-> srv TCP event
2023-09-25 08:23:52 -04:00
uint64_t src_id = 0,
dest_id = 0,
dest_team_id = 0;
char mess[140]; // Good 'nough for twitr, good 'nough for me.
2023-09-25 16:30:03 -04:00
};
2023-09-25 08:23:52 -04:00
2023-09-25 16:30:03 -04:00
struct ErrorLog { // srv -> cli TCP event
2023-09-25 08:53:23 -04:00
char mess[140];
bool is_fatal;
2023-09-25 16:30:03 -04:00
};
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);
2023-10-23 16:20:52 -04:00
2023-10-24 10:43:30 -04:00
Packet getPack(char* buf, uint32_t buflen);
bool emptyPack(Packet pck);
2023-10-24 10:43:30 -04:00
2023-10-23 16:20:52 -04:00
template <class T> void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen);
2023-10-24 09:17:21 -04:00
template <class T> void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad);
2023-10-23 16:20:52 -04:00
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;
}
2023-10-23 17:49:40 -04:00
2023-10-24 09:17:21 -04:00
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;
}
2023-10-23 16:20:52 -04:00
};
2023-09-25 08:23:52 -04:00
#endif