ajout LoginInfo dans le parseur de paquet.
This commit is contained in:
parent
4964bc5394
commit
dd2396e5e4
@ -571,46 +571,89 @@ netprot::Packet netprot::getPack(char* buf, uint32_t buflen) {
|
||||
Chat* chat = nullptr;
|
||||
GameInfo* ginfo = nullptr;
|
||||
ErrorLog* errlog = nullptr;
|
||||
LoginInfo* loginf = 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;
|
||||
if (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;
|
||||
if (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;
|
||||
if (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;
|
||||
if (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;
|
||||
if (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;
|
||||
if (netprot::Deserialize(errlog, buf, buflen)) {
|
||||
pck.type = PACKET_TYPE::ERRLOG;
|
||||
pck.ptr = (void*)errlog;
|
||||
}
|
||||
break;
|
||||
case PACKET_TYPE::LOGINF:
|
||||
loginf = new LoginInfo();
|
||||
if (netprot::Deserialize(loginf, buf, buflen)) {
|
||||
pck.type = PACKET_TYPE::LOGINF;
|
||||
pck.ptr = (void*)loginf;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return pck;
|
||||
}
|
||||
|
||||
bool netprot::emptyPack(netprot::Packet pck) {
|
||||
switch (pck.type) {
|
||||
case PACKET_TYPE::INPUT:
|
||||
delete (Input*)pck.ptr;
|
||||
break;
|
||||
case PACKET_TYPE::OUTPUT:
|
||||
delete (Output*)pck.ptr;
|
||||
break;
|
||||
case PACKET_TYPE::SYNC:
|
||||
delete (Sync*)pck.ptr;
|
||||
break;
|
||||
case PACKET_TYPE::CHAT:
|
||||
delete (Chat*)pck.ptr;
|
||||
break;
|
||||
case PACKET_TYPE::GAMEINFO:
|
||||
delete (GameInfo*)pck.ptr;
|
||||
break;
|
||||
case PACKET_TYPE::ERRLOG:
|
||||
delete (ErrorLog*)pck.ptr;
|
||||
break;
|
||||
case PACKET_TYPE::LOGINF:
|
||||
delete (LoginInfo*)pck.ptr;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ namespace netprot {
|
||||
};
|
||||
|
||||
struct Packet {
|
||||
void* ptr;
|
||||
void* ptr = nullptr;
|
||||
PACKET_TYPE type;
|
||||
};
|
||||
|
||||
@ -120,9 +120,10 @@ namespace netprot {
|
||||
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); // srv/cli
|
||||
PacketType getType(char* buf, uint32_t buflen);
|
||||
|
||||
Packet getPack(char* buf, uint32_t buflen);
|
||||
bool emptyPack(Packet pck);
|
||||
|
||||
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);
|
||||
|
@ -67,7 +67,6 @@ int Server::Init() {
|
||||
int Server::Ready() {
|
||||
int nbrjoueurs = 0,
|
||||
nbrconn = 0;
|
||||
#ifdef SRV_MANUAL_SETUP
|
||||
do {
|
||||
Log("Entrez la durée de la partie: ", false, false);
|
||||
std::cin >> m_game.countdown;
|
||||
@ -83,11 +82,7 @@ int Server::Ready() {
|
||||
std::cin >> nbrjoueurs;
|
||||
std::cout << std::endl;
|
||||
} while (nbrjoueurs > 0 && nbrjoueurs >= MAX_CONNECTIONS);
|
||||
#else // setup preconfiguré
|
||||
m_game.countdown = 360;
|
||||
m_game.seed = 9370707;
|
||||
nbrjoueurs = 1;
|
||||
#endif
|
||||
|
||||
m_game.gameType = 1;
|
||||
|
||||
if (listen(m_sock_tcp, MAX_CONNECTIONS) < 0) {
|
||||
@ -106,48 +101,58 @@ int Server::Ready() {
|
||||
|
||||
while (!readystart) {
|
||||
sockaddr_in sockad;
|
||||
|
||||
addrlen_t addrlen = sizeof(sockad);
|
||||
|
||||
SOCKET sock = accept(m_sock_tcp, (sockaddr*)&sockad, &addrlen);
|
||||
|
||||
if (sock < 0)
|
||||
Log("Erreur de connexion", true, false);
|
||||
else if (sock > 0) {
|
||||
std::string str = "Nouvelle connection provenant de: ";
|
||||
char* strbuf = new char[BUFFER_LENGTH];
|
||||
static char* strbuf = new char[BUFFER_LENGTH];
|
||||
uint32_t strbuflen = BUFFER_LENGTH;
|
||||
|
||||
std::string str = "Nouvelle connection provenant de: ";
|
||||
str.append(inet_ntop(AF_INET, &sockad.sin_addr, strbuf, strbuflen)).append(": ").append(std::to_string(sockad.sin_port));
|
||||
|
||||
if (recv(sock, buf, buflen, 0) > 0) {
|
||||
netprot::LoginInfo log;
|
||||
netprot::LoginInfo* log;
|
||||
netprot::PlayerInfo play;
|
||||
if (netprot::Deserialize(&log, buf, buflen)) {
|
||||
log.sid = getUniqueId();
|
||||
|
||||
log.tid = 0;
|
||||
|
||||
str.append(" Nom: ").append(log.name);
|
||||
Log(str, false, false);
|
||||
str = "";
|
||||
|
||||
str.append(log.name).append(" SID: [").append(std::to_string(log.sid).append("]"));
|
||||
Log(str, false, false);
|
||||
|
||||
netprot::sendPack<netprot::LoginInfo>(sock, &log, &buf, &buflen);
|
||||
|
||||
play.id = getUniqueId();
|
||||
memcpy(play.name, log.name, std::strlen(log.name) + 1);
|
||||
play.tid = log.tid;
|
||||
|
||||
netprot::sendPack<netprot::GameInfo>(sock, &m_game, &buf, &buflen);
|
||||
Connection* conn = new Connection(sock, sockad, log, play);
|
||||
|
||||
m_players[log.sid] = conn;
|
||||
if (++nbrconn >= nbrjoueurs)
|
||||
readystart = true;
|
||||
netprot::Packet pck = netprot::getPack(buf, buflen);
|
||||
if (pck.type != netprot::PACKET_TYPE::LOGINF) {
|
||||
if (pck.type != netprot::PACKET_TYPE::ERR)
|
||||
netprot::emptyPack(pck);
|
||||
continue;
|
||||
}
|
||||
log = (netprot::LoginInfo*)pck.ptr;
|
||||
|
||||
log->sid = getUniqueId();
|
||||
log->tid = 0;
|
||||
|
||||
str.append(" Nom: ").append(log->name);
|
||||
Log(str, false, false);
|
||||
str = "";
|
||||
|
||||
str.append(log->name).append(" SID: [").append(std::to_string(log->sid).append("]"));
|
||||
Log(str, false, false);
|
||||
|
||||
netprot::sendPack<netprot::LoginInfo>(sock, log, &buf, &buflen);
|
||||
|
||||
play.id = getUniqueId();
|
||||
memcpy(play.name, log->name, std::strlen(log->name) + 1);
|
||||
play.tid = log->tid;
|
||||
|
||||
netprot::sendPack<netprot::GameInfo>(sock, &m_game, &buf, &buflen);
|
||||
Connection* conn = new Connection(sock, sockad, *log, play);
|
||||
|
||||
// TODO: Envoyer les infos de joueur distant aux joueurs déjà connectés
|
||||
// et envoyer les infos des joueurs distants au nouveau joueur.
|
||||
|
||||
m_players[log->sid] = conn;
|
||||
delete log; // le pck va se supprimer tout seul, mais le pointer du log qui vient de lui, non.
|
||||
|
||||
if (++nbrconn >= nbrjoueurs)
|
||||
readystart = true;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -163,6 +168,11 @@ void Server::Run() {
|
||||
|
||||
Log("Partie en cours...", false, false);
|
||||
|
||||
for (auto& conn : m_players) // Gérer le point de spawn des joueurs.
|
||||
conn.second->player = new Player(Vector3f(64., 128., 64.));
|
||||
|
||||
// TODO: Faire un premier sync pour que le joueur parte à la bonne place.
|
||||
|
||||
while (true) {
|
||||
if (recvfrom(m_sock_udp, buf, BUFFER_LENGTH, 0, (sockaddr*)&sockad, &socklen) > 0) {
|
||||
Deserialize(&in, buf, buflen);
|
||||
@ -248,113 +258,3 @@ uint64_t Server::getUniqueId() {
|
||||
Log(str, false, false);
|
||||
*/
|
||||
|
||||
|
||||
///* Recevoir paquet */
|
||||
//while (true) {
|
||||
// char buffer[2048];
|
||||
// sockaddr_in client;
|
||||
//
|
||||
//#ifdef _WIN32 // Mais pourquoi?
|
||||
// int clen = sizeof(client);
|
||||
//#else
|
||||
// unsigned int clen = sizeof(client);
|
||||
//#endif
|
||||
//
|
||||
// int count = recvfrom(socket_udp, buffer, sizeof(buffer) - 1, 0, (sockaddr*)&client, &clen);
|
||||
//
|
||||
// if (count < 0) {
|
||||
// Log("Erreur de reception de paquet.", socket_udp);
|
||||
// return 4;
|
||||
// }
|
||||
//
|
||||
// buffer[count] = '\0';
|
||||
//
|
||||
// /* Gérer le paquet reçu */
|
||||
// std::string commande(buffer);
|
||||
//
|
||||
// if (commande.find("echo ") == 0) { /* ECHO */
|
||||
// std::string::size_type pos = commande.find(' ');
|
||||
// std::string parametres = commande.substr(pos + 1);
|
||||
//
|
||||
// sendto(socket_udp, parametres.c_str(), parametres.length(), 0, (const sockaddr*)&client, sizeof(client));
|
||||
// }
|
||||
// else if (commande.find("date ") == 0) { /* DATE */
|
||||
// time_t rawtime;
|
||||
// struct tm* timeinfo = new tm();
|
||||
// char tbuffer[80];
|
||||
//
|
||||
// time(&rawtime);
|
||||
//
|
||||
//#ifdef _WIN32
|
||||
// localtime_s(timeinfo, &rawtime);
|
||||
//#else
|
||||
// localtime_r(&rawtime, timeinfo);
|
||||
//#endif
|
||||
//
|
||||
// strftime(tbuffer, 80, "%a %b %e %T %G", timeinfo);
|
||||
//
|
||||
// sendto(socket_udp, tbuffer, sizeof(tbuffer), 0, (const sockaddr*)&client, sizeof(client));
|
||||
// delete timeinfo;
|
||||
// }
|
||||
// else if (commande.find("ping ") == 0) { /* PING */
|
||||
// sendto(socket_udp, "pong", sizeof("pong"), 0, (const sockaddr*)&client, sizeof(client));
|
||||
// }
|
||||
// else if (commande.find("usager ") == 0) { /* USAGER */
|
||||
// std::string user;
|
||||
//
|
||||
//#ifdef _WIN32
|
||||
// wchar_t userbuf[30];
|
||||
// DWORD usersize = 30;
|
||||
//
|
||||
// GetUserNameW(userbuf, &usersize);
|
||||
//
|
||||
// std::wstring wuser = userbuf;
|
||||
// user = std::string(wuser.begin(), wuser.end());
|
||||
//#else
|
||||
// char ptr[30];
|
||||
// getlogin_r(ptr, sizeof(ptr) - 1);
|
||||
// user = std::string(ptr);
|
||||
//#endif
|
||||
//
|
||||
// sendto(socket_udp, user.c_str(), user.length(), 0, (const sockaddr*)&client, sizeof(client));
|
||||
// }
|
||||
// else if (commande.find("exec ") == 0) { /* EXEC */
|
||||
// std::string::size_type pos = commande.find(' ');
|
||||
// std::string parametres = commande.substr(pos + 1);
|
||||
//
|
||||
// FILE* pipe = nullptr;
|
||||
// char buffer[301]; // 300 caractères + '\0'
|
||||
// std::string reponse;
|
||||
//
|
||||
// pipe = popen(parametres.c_str(), "r");
|
||||
//
|
||||
// if (!pipe)
|
||||
// reponse = "Erreur de commande!";
|
||||
// else while (!feof(pipe)) {
|
||||
// if (fgets(buffer, sizeof(buffer) - 1, pipe))
|
||||
// reponse += buffer;
|
||||
// }
|
||||
//
|
||||
// if (pipe)
|
||||
// pclose(pipe);
|
||||
//
|
||||
// if (reponse.length() > 300)
|
||||
// reponse = reponse.substr(0, 300);
|
||||
// else if (reponse.length() < 1)
|
||||
// reponse = "OK!";
|
||||
//
|
||||
// sendto(socket_udp, reponse.c_str(), reponse.length(), 0, (const sockaddr*)&client, sizeof(client));
|
||||
// }
|
||||
// else if (commande.find("bye ") == 0) { /* BYE */
|
||||
// Log("", 0); // Message d'erreur pas de message d'erreur!
|
||||
// return 0;
|
||||
// }
|
||||
// else sendto(socket_udp, "huh?", sizeof("huh?"), 0, (const sockaddr*)&client, sizeof(client)); /* DEFAULT */
|
||||
//}
|
||||
//
|
||||
///* Ce bout de code ne devrait theoriquement jamais etre atteint, mais au cas. */
|
||||
//Log("", 0);
|
||||
//return 0;
|
||||
//return false;
|
||||
//}
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user