This commit is contained in:
Marc-Éric Martel 2023-10-27 12:02:29 -04:00
parent 0dded0bc9b
commit 6a8a505167
3 changed files with 130 additions and 52 deletions

View File

@ -637,27 +637,122 @@ bool netprot::emptyPack(netprot::Packet pck) {
switch (pck.type) {
case PACKET_TYPE::INPUT:
delete (Input*)pck.ptr;
break;
return true;
case PACKET_TYPE::OUTPUT:
delete (Output*)pck.ptr;
break;
return true;
case PACKET_TYPE::SYNC:
delete (Sync*)pck.ptr;
break;
return true;
case PACKET_TYPE::CHAT:
delete (Chat*)pck.ptr;
break;
return true;
case PACKET_TYPE::GAMEINFO:
delete (GameInfo*)pck.ptr;
break;
return true;
case PACKET_TYPE::ERRLOG:
delete (ErrorLog*)pck.ptr;
break;
return true;
case PACKET_TYPE::LOGINF:
delete (LoginInfo*)pck.ptr;
break;
return true;
default:
return false;
}
return true;
}
template <>
void netprot::sendPack<netprot::Packet>(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen) {
switch (pack->type) {
case PACKET_TYPE::INPUT:
sendPack<Input>(sock, (Input*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::OUTPUT:
sendPack<Output>(sock, (Output*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::SYNC:
sendPack<Sync>(sock, (Sync*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::TEAMINF:
sendPack<TeamInfo>(sock, (TeamInfo*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::PLAYINF:
sendPack<PlayerInfo>(sock, (PlayerInfo*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::LOGINF:
sendPack<LoginInfo>(sock, (LoginInfo*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::CHUNKMOD:
//sendPack<ChunkMod>(sock, (ChunkMod*)pack->ptr, buf, buflen);
case PACKET_TYPE::PLAYERMOD:
//sendPack<PlayerMod>(sock, (PlayerMod*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::PICKUPMOD:
//sendPack<PickupMod>(sock, (PickupMod*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::GAMEINFO:
sendPack<GameInfo>(sock, (GameInfo*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::ENDINFO:
//sendPack<EndInfo>(sock, (EndInfo*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::CHAT:
sendPack<Chat>(sock, (Chat*)pack->ptr, buf, buflen);
case PACKET_TYPE::ERRLOG:
sendPack<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::LAST_PACK: [[fallthrough]];
case PACKET_TYPE::ERR: [[fallthrough]];
default:
return;
}
}
template <>
void netprot::sendPackTo<netprot::Packet>(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) {
switch (pack->type) {
case PACKET_TYPE::INPUT:
sendPackTo<Input>(sock, (Input*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::OUTPUT:
sendPackTo<Output>(sock, (Output*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::SYNC:
sendPackTo<Sync>(sock, (Sync*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::TEAMINF:
sendPackTo<TeamInfo>(sock, (TeamInfo*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::PLAYINF:
sendPackTo<PlayerInfo>(sock, (PlayerInfo*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::LOGINF:
sendPackTo<LoginInfo>(sock, (LoginInfo*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::CHUNKMOD:
//sendPackTo<ChunkMod>(sock, (ChunkMod*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::PLAYERMOD:
//sendPackTo<PlayerMod>(sock, (PlayerMod*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::PICKUPMOD:
//sendPackTo<PickupMod>(sock, (PickupMod*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::GAMEINFO:
sendPackTo<GameInfo>(sock, (GameInfo*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::ENDINFO:
//sendPackTo<EndInfo>(sock, (EndInfo*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::CHAT:
sendPackTo<Chat>(sock, (Chat*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::ERRLOG:
sendPackTo<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::LAST_PACK: [[fallthrough]];
case PACKET_TYPE::ERR: [[fallthrough]];
default:
return;
}
}

View File

@ -134,6 +134,8 @@ namespace netprot {
bool emptyPack(Packet pck);
Packet makePack(void* ptr, PACKET_TYPE type); // Pour pouvoir faire une liste de stock à supprimer sans avoir à en faire une pour chaque type.
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);

View File

@ -196,52 +196,53 @@ void Server::Run() {
if (recvfrom(m_sock_udp, m_buf.ptr, m_buf.len, 0, (sockaddr*)&sockad, &socklen) > 0) {
Packet pck = getPack(&m_buf);
switch (pck.type) {
case netprot::PACKET_TYPE::ERR:
using enum netprot::PACKET_TYPE;
case ERR:
std::cout << "ERROR!" << std::endl;
break;
case netprot::PACKET_TYPE::INPUT:
case INPUT:
std::cout << "INPUT!" << std::endl;
break;
case netprot::PACKET_TYPE::OUTPUT:
case OUTPUT:
std::cout << "OUTPUT!" << std::endl;
break;
case netprot::PACKET_TYPE::SYNC:
case SYNC:
std::cout << "SYNC!" << std::endl;
break;
case netprot::PACKET_TYPE::TEAMINF:
case TEAMINF:
std::cout << "TEAMINF!" << std::endl;
break;
case netprot::PACKET_TYPE::SELFINF:
case SELFINF:
std::cout << "SELFINF!" << std::endl;
break;
case netprot::PACKET_TYPE::PLAYINF:
case PLAYINF:
std::cout << "PLAYINF!" << std::endl;
break;
case netprot::PACKET_TYPE::LOGINF:
case LOGINF:
std::cout << "LOGINF!" << std::endl;
break;
case netprot::PACKET_TYPE::CHUNKMOD:
case CHUNKMOD:
std::cout << "CHUNKMOD!" << std::endl;
break;
case netprot::PACKET_TYPE::PLAYERMOD:
case PLAYERMOD:
std::cout << "PLAYERMOD!" << std::endl;
break;
case netprot::PACKET_TYPE::PICKUPMOD:
case PICKUPMOD:
std::cout << "PICKUPMOD!" << std::endl;
break;
case netprot::PACKET_TYPE::GAMEINFO:
case GAMEINFO:
std::cout << "GAMEINFO!" << std::endl;
break;
case netprot::PACKET_TYPE::ENDINFO:
case ENDINFO:
std::cout << "ENDINFO!" << std::endl;
break;
case netprot::PACKET_TYPE::CHAT:
case CHAT:
std::cout << "CHAT!" << std::endl;
break;
case netprot::PACKET_TYPE::ERRLOG:
case ERRLOG:
std::cout << "ERRLOG!" << std::endl;
break;
case netprot::PACKET_TYPE::LAST_PACK:
case LAST_PACK:
std::cout << "wtf?!" << std::endl;
break;
}
@ -271,14 +272,14 @@ inline std::string Server::LogTimestamp() {
void Server::Log(std::string str, bool is_error = false, bool is_fatal = false) {
switch (m_log) {
using enum LOG_DEST; // C++20!
case LOGFILE:
m_logfile << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
case CONSOLE:
default:
std::cout << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
using enum LOG_DEST; // C++20!
case LOGFILE:
m_logfile << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
case CONSOLE: [[fallthrough]]; // Pour dire que c'est voulu que ça traverse vers le case en dessous (C++17!)
default:
std::cout << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
}
if (is_fatal) {
@ -315,23 +316,3 @@ uint64_t Server::getUniqueId() {
m_ids.pop_back();
return id;
}
// Test serialize/deserialize:
/*
netprot::LoginInfo log, log2;
std::cout << "Nom? ";
std::cin.getline(log.name, 32); // NO! STD::CIN >> VARIABLE;! EVEEEEEERRRR!!!
log.sid = 12345;
char* buf = new char[150];
uint32_t buflen = 150;
netprot::Serialize(&log, &buf, &buflen);
bool is_work = netprot::Deserialize(&log2, buf, buflen);
std::string str;
str.append(is_work ? "Y " : "N ").append(log2.name).append(": ").append(std::to_string(log2.sid));
Log(str, false, false);
*/