Ev'ryday I'm bufferin'.

This commit is contained in:
Marc-Éric Martel 2023-10-27 09:51:40 -04:00
parent 0bc7f29492
commit 9ef44a3993
4 changed files with 82 additions and 50 deletions

View File

@ -20,7 +20,7 @@ namespace netprot {
struct Buffer { // Pour pouvoir rendre l'utilisation des buffers plus clean. struct Buffer { // Pour pouvoir rendre l'utilisation des buffers plus clean.
char* ptr = new char[BUFFER_LENGTH]; char* ptr = new char[BUFFER_LENGTH];
int32_t len = BUFFER_LENGTH; uint32_t len = BUFFER_LENGTH;
~Buffer() { delete[] ptr; } ~Buffer() { delete[] ptr; }
void rstLen() { len = BUFFER_LENGTH; } void rstLen() { len = BUFFER_LENGTH; }
@ -157,7 +157,7 @@ namespace netprot {
template <class T> template <class T>
void sendPack(SOCKET sock, T* pack, Buffer* buf) { void sendPack(SOCKET sock, T* pack, Buffer* buf) {
netprot::Serialize(pack, buf->ptr, buf->len); netprot::Serialize(pack, &buf->ptr, &buf->len);
send(sock, buf->ptr, buf->len, 0); send(sock, buf->ptr, buf->len, 0);
buf->rstLen(); buf->rstLen();
} }
@ -165,7 +165,7 @@ namespace netprot {
template <class T> template <class T>
void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad) { void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad) {
sockaddr_in addr = *sockad; sockaddr_in addr = *sockad;
netprot::Serialize(pack, buf->ptr, buf->len); netprot::Serialize(pack, &buf->ptr, &buf->len);
sendto(sock, buf->ptr, buf->len, 0, (sockaddr*)&addr, sizeof(addr)); sendto(sock, buf->ptr, buf->len, 0, (sockaddr*)&addr, sizeof(addr));
buf->rstLen(); buf->rstLen();
} }

View File

@ -15,7 +15,4 @@
#define strcpy strcpy_s #define strcpy strcpy_s
#endif #endif
typedef unsigned char LogDest;
enum LOG_DEST { CONSOLE, LOGFILE, LOG_LAST };
#endif #endif

View File

@ -1,6 +1,6 @@
#include "server.h" #include "server.h"
Server::Server(LogDest log) { Server::Server(LOG_DEST log) {
m_log = log; m_log = log;
if (log == LOG_DEST::LOGFILE) { if (log == LOG_DEST::LOGFILE) {
m_logfile = std::ofstream("server.log", std::ofstream::out); m_logfile = std::ofstream("server.log", std::ofstream::out);
@ -70,25 +70,21 @@ int Server::Init() {
int Server::Ready() { int Server::Ready() {
int nbrjoueurs = 0, int nbrjoueurs = 0,
nbrconn = 0; nbrconn = 0;
char *buf = new char[BUFFER_LENGTH],
*strbuf = new char[BUFFER_LENGTH];
uint32_t buflen = BUFFER_LENGTH,
strbuflen = BUFFER_LENGTH;
bool readystart = false; bool readystart = false;
do { do {
Log("Entrez la duree de la partie: ", false, false); Log("Entrez la duree de la partie: ", false, false);
std::cin.getline(strbuf, BUFFER_LENGTH); std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
m_game.countdown = std::stoi(strbuf); m_game.countdown = std::stoi(m_buf.ptr);
} while (m_game.countdown < 1); } while (m_game.countdown < 1);
do { do {
Log("Entrez le seed de la partie: ", false, false); Log("Entrez le seed de la partie: ", false, false);
std::cin.getline(strbuf, BUFFER_LENGTH); std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
m_game.seed = std::stoi(strbuf); m_game.seed = std::stoi(m_buf.ptr);
} while (m_game.seed < 1); } while (m_game.seed < 1);
do { do {
Log("Entrez le nombre de joueurs: ", false, false); Log("Entrez le nombre de joueurs: ", false, false);
std::cin.getline(strbuf, BUFFER_LENGTH); std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
nbrjoueurs = std::stoi(strbuf); nbrjoueurs = std::stoi(m_buf.ptr);
} while (nbrjoueurs > 0 && nbrjoueurs >= MAX_CONNECTIONS); } while (nbrjoueurs > 0 && nbrjoueurs >= MAX_CONNECTIONS);
m_game.gameType = 1; m_game.gameType = 1;
@ -111,13 +107,13 @@ int Server::Ready() {
Log("Erreur de connexion", true, false); Log("Erreur de connexion", true, false);
else if (sock > 0) { else if (sock > 0) {
std::string str = "Nouvelle connexion provenant de: "; std::string str = "Nouvelle connexion provenant de: ";
str.append(inet_ntop(AF_INET, &sockad.sin_addr, strbuf, strbuflen)).append(": ").append(std::to_string(sockad.sin_port)); str.append(inet_ntop(AF_INET, &sockad.sin_addr, m_buf.ptr, m_buf.len)).append(": ").append(std::to_string(sockad.sin_port));
if (recv(sock, buf, buflen, 0) > 0) { if (recv(sock, m_buf.ptr, m_buf.len, 0) > 0) {
LoginInfo* log; LoginInfo* log;
PlayerInfo play; PlayerInfo play;
Packet pck = getPack(buf, buflen); Packet pck = getPack(&m_buf);
if (pck.type != PACKET_TYPE::LOGINF) { if (pck.type != PACKET_TYPE::LOGINF) {
Log("Paquet invalide.", true, false); Log("Paquet invalide.", true, false);
if (pck.type != PACKET_TYPE::ERR) if (pck.type != PACKET_TYPE::ERR)
@ -134,21 +130,19 @@ int Server::Ready() {
Log(str.append(log->name).append(" SID: [").append(std::to_string(log->sid).append("]")), false, false); Log(str.append(log->name).append(" SID: [").append(std::to_string(log->sid).append("]")), false, false);
sendPack<LoginInfo>(sock, log, &buf, &buflen); sendPack<LoginInfo>(sock, log, &m_buf.ptr, &m_buf.len);
play.id = getUniqueId(); play.id = getUniqueId();
strcpy(play.name, log->name); strcpy(play.name, log->name);
play.tid = log->tid; play.tid = log->tid;
sendPack<GameInfo>(sock, &m_game, &buf, &buflen); sendPack<GameInfo>(sock, &m_game, &m_buf.ptr, &m_buf.len);
Connection* conn = new Connection(sock, sockad, *log, play); Connection* conn = new Connection(sock, sockad, *log, play);
for (auto& [key, player] : m_players) { for (auto& [key, player] : m_players) {
sendPack<PlayerInfo>(player->getSock(), &play, &buf, &buflen); // Envoyer les infos de joueur distant aux joueurs d<>j<EFBFBD> connect<63>s sendPack<PlayerInfo>(player->getSock(), &play, &m_buf); // Envoyer les infos de joueur distant aux joueurs d<>j<EFBFBD> connect<63>s
buflen = BUFFER_LENGTH; sendPack<PlayerInfo>(sock, player->getInfo(), &m_buf); // et envoyer les infos des joueurs distants au nouveau joueur.
sendPack<PlayerInfo>(sock, player->getInfo(), &buf, &buflen); // et envoyer les infos des joueurs distants au nouveau joueur.
buflen = BUFFER_LENGTH;
} }
m_players[log->sid] = conn; m_players[log->sid] = conn;
@ -158,14 +152,10 @@ int Server::Ready() {
} }
} }
} }
delete[] buf;
delete[] strbuf;
return 0; return 0;
} }
void Server::Run() { void Server::Run() {
char* buf = new char[BUFFER_LENGTH];
uint32_t buflen = BUFFER_LENGTH;
Input in; Input in;
sockaddr_in sockad; sockaddr_in sockad;
addrlen_t socklen = sizeof(sockad); addrlen_t socklen = sizeof(sockad);
@ -182,16 +172,64 @@ void Server::Run() {
sync.ammo = 0; sync.ammo = 0;
sync.timestamp = 0; sync.timestamp = 0;
sync.timer = m_game.countdown; sync.timer = m_game.countdown;
sendPackTo<Sync>(conn->getSock(), &sync, &buf, &buflen, conn->getAddr()); sendPackTo<Sync>(conn->getSock(), &sync, &m_buf, conn->getAddr());
} }
while (true) { while (true) {
if (recvfrom(m_sock_udp, buf, BUFFER_LENGTH, 0, (sockaddr*)&sockad, &socklen) > 0) { if (recvfrom(m_sock_udp, m_buf.ptr, m_buf.len, 0, (sockaddr*)&sockad, &socklen) > 0) {
Deserialize(&in, buf, buflen); Packet pck = getPack(&m_buf);
std::cout << "Id: " << in.sid << "\r\n" switch (pck.type) {
<< "Direction: { " << in.direction.x << ", " << in.direction.y << ", " << in.direction.z << " }" << "\r\n"; case netprot::PACKET_TYPE::ERR:
std::cout << "ERROR!" << std::endl;
break;
case netprot::PACKET_TYPE::INPUT:
std::cout << "INPUT!" << std::endl;
break;
case netprot::PACKET_TYPE::OUTPUT:
std::cout << "OUTPUT!" << std::endl;
break;
case netprot::PACKET_TYPE::SYNC:
std::cout << "SYNC!" << std::endl;
break;
case netprot::PACKET_TYPE::TEAMINF:
std::cout << "TEAMINF!" << std::endl;
break;
case netprot::PACKET_TYPE::SELFINF:
std::cout << "SELFINF!" << std::endl;
break;
case netprot::PACKET_TYPE::PLAYINF:
std::cout << "PLAYINF!" << std::endl;
break;
case netprot::PACKET_TYPE::LOGINF:
std::cout << "LOGINF!" << std::endl;
break;
case netprot::PACKET_TYPE::CHUNKMOD:
std::cout << "CHUNKMOD!" << std::endl;
break;
case netprot::PACKET_TYPE::PLAYERMOD:
std::cout << "PLAYERMOD!" << std::endl;
break;
case netprot::PACKET_TYPE::PICKUPMOD:
std::cout << "PICKUPMOD!" << std::endl;
break;
case netprot::PACKET_TYPE::GAMEINFO:
std::cout << "GAMEINFO!" << std::endl;
break;
case netprot::PACKET_TYPE::ENDINFO:
std::cout << "ENDINFO!" << std::endl;
break;
case netprot::PACKET_TYPE::CHAT:
std::cout << "CHAT!" << std::endl;
break;
case netprot::PACKET_TYPE::ERRLOG:
std::cout << "ERRLOG!" << std::endl;
break;
case netprot::PACKET_TYPE::LAST_PACK:
std::cout << "wtf?!" << std::endl;
break;
}
emptyPack(pck);
} }
std::cout << "!" << std::endl;
} }
} }

View File

@ -14,7 +14,9 @@ using namespace netprot;
class Server { class Server {
public: public:
Server(LogDest log = LOG_DEST::CONSOLE); enum LOG_DEST: unsigned char { CONSOLE, LOGFILE, LOG_LAST };
Server(LOG_DEST log = LOG_DEST::CONSOLE);
~Server(); ~Server();
int Init(); int Init();
@ -22,18 +24,21 @@ public:
void Run(); void Run();
private: private:
#ifdef _WIN32 #ifdef _WIN32
WSADATA m_wsaData; WSADATA m_wsaData;
#endif #endif
SOCKET m_sock_udp = 0, SOCKET m_sock_udp = 0,
m_sock_tcp = 0; m_sock_tcp = 0;
LogDest m_log; LOG_DEST m_log;
std::ofstream m_logfile; std::ofstream m_logfile;
Buffer m_buf;
std::map<uint64_t, Connection*> m_players; std::map<uint64_t, Connection*> m_players;
std::map <Timestamp, netprot::Chat> m_chatlog; std::map <Timestamp, Chat> m_chatlog;
std::vector<uint64_t> m_ids; std::vector<uint64_t> m_ids;
netprot::GameInfo m_game; GameInfo m_game;
World* m_world = nullptr; World* m_world = nullptr;
const bool m_manual_setup = SRV_MANUAL_SETUP; const bool m_manual_setup = SRV_MANUAL_SETUP;
@ -43,15 +48,7 @@ private:
void buildIdList(size_t size); void buildIdList(size_t size);
uint64_t getUniqueId(); uint64_t getUniqueId();
//template <class T> void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen);
}; };
//template <class T>
//void Server::sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) {
// netprot::Serialize(pack, buf, buflen);
// send(sock, *buf, *buflen, 0);
// *buflen = BUFFER_LENGTH;
//}
#endif #endif