#ifndef NETPROTOCOL_H__ #define NETPROTOCOL_H__ #include #include "define.h" #include "vector3.h" /* Protocole Particulier de Partie a Plusieurs Personnes (PPPPP) */ // Packet: packet[0] = PacketType, packet[1..n-1] = {packet} namespace netprot { enum class PACKET_TYPE: uint8_t { ERR, INPUT, OUTPUT, SYNC, TEAMINF, SELFINF, PLAYINF, LOGINF, CHUNKMOD, PLAYERMOD, PICKUPMOD, GAMEINFO, ENDINFO , CHAT, ERRLOG, LAST_PACK }; struct Buffer { // Pour pouvoir rendre l'utilisation des buffers plus clean. char* ptr = new char[BUFFER_LENGTH]; uint32_t len = BUFFER_LENGTH; ~Buffer() { delete[] ptr; } void rstLen() { len = BUFFER_LENGTH; } }; struct Packet { // Pour pouvoir recevoir les paquets du recv() sans avoir à les aiguiller dans la même thread. void* ptr = nullptr; // Notez que le pointeur doit être supprimé séparément lorsqu'il n'est plus utile. PACKET_TYPE type = PACKET_TYPE::ERR; }; struct Keys { bool forward, backward, left, right, jump, shoot, block; }; struct States { bool jumping, shooting, hit, powerup, dead, still, jumpshot, running; }; 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; Sync() {} Sync(Sync* sync) : timestamp(sync->timestamp), sid(sync->sid), timer(sync->timer), ammo(sync->ammo), hp(sync->hp), position(sync->position) {} }; struct TeamInfo { // cli <-> srv TCP once char name[32]; uint64_t id = 0; TeamInfo() {} TeamInfo(TeamInfo* tem) : id(tem->id) { strcpy(tem->name, name); } }; struct LoginInfo { // cli <-> srv TCP once char name[32]; uint64_t sid = 0, tid = 0; LoginInfo() {} LoginInfo(LoginInfo* ply): sid(ply->sid), tid(ply->tid) { strcpy(ply->name, name); } }; struct PlayerInfo { // cli <-> srv TCP once char name[32]; uint64_t id = 0, tid = 0; PlayerInfo() {} PlayerInfo(PlayerInfo* log) : id(log->id), tid(log->tid) { strcpy(log->name, name); } }; struct GameInfo { // cli <-> srv TCP event (before game start)/ once uint64_t seed; uint32_t countdown; uint8_t gameType; // TODD: enum. GameInfo() {} GameInfo(GameInfo* gam) : seed(gam->seed), countdown(gam->countdown), gameType(gam->gameType) {} }; 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. Chat() {} Chat(Chat* cha) : src_id(cha->src_id), dest_id(cha->dest_id), dest_team_id(cha->dest_team_id) { strcpy(cha->mess, mess); } }; struct ErrorLog { // srv -> cli TCP event char mess[140]; bool is_fatal; ErrorLog() {}; ErrorLog(ErrorLog* err) : is_fatal(err->is_fatal) { strcpy(err->mess, mess); } }; 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 PACKET_TYPE getType(char* buf, uint32_t buflen); Packet getPack(char* buf, uint32_t buflen); Packet getPack(Buffer* buf); bool emptyPack(Packet pck); Packet makePack(void* ptr, PACKET_TYPE type); // Pour pouvoir faire une liste de stock a supprimer sans avoir a en faire une pour chaque type. template void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen); template void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad); template void sendPack(SOCKET sock, T* pack, Buffer* buf); template void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad); std::vector recvPacks(SOCKET sock, Buffer* buf); template void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) { netprot::Serialize(pack, buf, buflen); memset(&buf[*buflen], '\0', sizeof(uint64_t) + sizeof(uint8_t)); buflen += (sizeof(uint64_t) + sizeof(uint8_t)); send(sock, *buf, *buflen, 0); *buflen = BUFFER_LENGTH; } template void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) { sockaddr_in addr = *sockad; netprot::Serialize(pack, buf, buflen); memset(&buf[*buflen], '\0', sizeof(uint64_t) + sizeof(uint8_t)); buflen += (sizeof(uint64_t) + sizeof(uint8_t)); sendto(sock, *buf, *buflen, 0, (sockaddr*)&addr, sizeof(addr)); *buflen = BUFFER_LENGTH; } template void sendPack(SOCKET sock, T* pack, Buffer* buf) { netprot::Serialize(pack, &buf->ptr, &buf->len); memset(&buf->ptr[buf->len], '\0', sizeof(uint64_t) + sizeof(uint8_t)); buf->len += sizeof(uint64_t) + sizeof(uint8_t); send(sock, buf->ptr, buf->len, 0); buf->rstLen(); } template void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad) { sockaddr_in addr = *sockad; netprot::Serialize(pack, &buf->ptr, &buf->len); memset(&buf->ptr[buf->len], '\0', sizeof(uint64_t) + sizeof(uint8_t)); buf->len += sizeof(uint64_t) + sizeof(uint8_t); sendto(sock, buf->ptr, buf->len, 0, (sockaddr*)&addr, sizeof(addr)); buf->rstLen(); } }; #endif