ajouts dans netprot

This commit is contained in:
MarcEricMartel 2023-10-24 10:43:30 -04:00
parent cf6e58cd95
commit 4964bc5394
2 changed files with 65 additions and 2 deletions

View File

@ -62,11 +62,15 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
}
void netprot::Serialize(Output* out, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::OUTPUT;
}
void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::SYNC;
}
void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) {
@ -558,3 +562,55 @@ netprot::PacketType netprot::getType(char* buf, const uint32_t buflen) {
return netprot::PACKET_TYPE::ERR;
return buf[0];
}
netprot::Packet netprot::getPack(char* buf, uint32_t buflen) {
Packet pck = { nullptr, PACKET_TYPE::ERR };
Input* in = nullptr;
Output* out = nullptr;
Sync* sync = nullptr;
Chat* chat = nullptr;
GameInfo* ginfo = nullptr;
ErrorLog* errlog = nullptr;
switch (getType(buf, buflen)) {
case PACKET_TYPE::INPUT:
in = new Input();
netprot::Deserialize(in, buf, buflen);
pck.type = PACKET_TYPE::INPUT;
pck.ptr = (void*)in;
break;
case PACKET_TYPE::OUTPUT:
out = new Output();
netprot::Deserialize(out, buf, buflen);
pck.type = PACKET_TYPE::OUTPUT;
pck.ptr = (void*)out;
break;
case PACKET_TYPE::SYNC:
sync = new Sync();
netprot::Deserialize(sync, buf, buflen);
pck.type = PACKET_TYPE::SYNC;
pck.ptr = (void*)sync;
break;
case PACKET_TYPE::CHAT:
chat = new Chat();
netprot::Deserialize(chat, buf, buflen);
pck.type = PACKET_TYPE::CHAT;
pck.ptr = (void*)chat;
break;
case PACKET_TYPE::GAMEINFO:
ginfo = new GameInfo();
netprot::Deserialize(ginfo, buf, buflen);
pck.type = PACKET_TYPE::GAMEINFO;
pck.ptr = (void*)ginfo;
break;
case PACKET_TYPE::ERRLOG:
errlog = new ErrorLog();
netprot::Deserialize(errlog, buf, buflen);
pck.type = PACKET_TYPE::ERRLOG;
pck.ptr = (void*)errlog;
break;
default:
break;
}
return pck;
}

View File

@ -19,6 +19,11 @@ namespace netprot {
LAST_PACK
};
struct Packet {
void* ptr;
PACKET_TYPE type;
};
struct Keys {
bool forward,
backward,
@ -117,6 +122,8 @@ namespace netprot {
PacketType getType(char* buf, uint32_t buflen); // srv/cli
Packet getPack(char* buf, uint32_t buflen);
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);