This commit is contained in:
MarcEricMartel
2023-09-27 11:24:41 -04:00
parent 8bc74624c3
commit 4dbcb08a11
8 changed files with 112 additions and 66 deletions

View File

@@ -73,16 +73,41 @@ int Server::Ready() {
return 1;
}
char buffer[2048];
char* buf = new char[150];
uint32_t buflen = 150;
bool readystart = false;
Log("<EFBFBD> l'<27>coute sur le port: " + std::to_string(SRV_PORT), false, false);
while (!readystart) {
sockaddr_in sockad;
int addrlen = sizeof(sockad);
SOCKET sock = accept(m_sock_tcp, (sockaddr*)&sockad, &addrlen);
// Listen/accept ici.
if (sock < 0)
Log("Erreur de connexion", true, false);
else if (sock > 0) {
char* strbuf = new char[150];
uint32_t strbuflen = 150;
std::cout << inet_ntop(AF_INET, &sockad.sin_addr, strbuf, strbuflen) << ':' << std::to_string(sockad.sin_port) << std::endl;;
if (recv(sock, buf, buflen, 0) > 0) {
netprot::LoginInfo log;
if (netprot::Deserialize(&log, buf, buflen)) {
std::string str;
log.sid = 8675309; // EIGHT SIX SEVENFIVE THREE AUGHT NIIIIIIIIIiiIIIIiINE!
str.append(log.name).append(": ").append(std::to_string(log.sid));
Log(str, false, false);
readystart = true;
netprot::Serialize(&log, &buf, &buflen);
send(sock, buf, buflen, 0);
std::cin.getline(nullptr, 1);
readystart = true;
}
}
}
}
return 0;
}
@@ -118,7 +143,7 @@ void Server::Log(std::string str, bool is_error = false, bool is_fatal = false)
break;
case LOG_DEST::CONSOLE:
default:
std::cout << Timestamp() << (is_fatal? "FATAL ": "") << (is_error ? "ERROR ": "") << str << std::endl;
std::cout << Timestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
}
@@ -135,30 +160,24 @@ void Server::Log(std::string str, bool is_error = false, bool is_fatal = false)
// Test serialize/deserialize:
/*
netprot::LoginInfo* log = new netprot::LoginInfo();
char nom[] = "Jean Dujardin";
memcpy(log->name, &nom, sizeof(nom));
log->sid = 12345;
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);
netprot::Serialize(&log, &buf, &buflen);
delete log;
log = new netprot::LoginInfo();
bool is_work = netprot::Deserialize(log, buf, buflen);
bool is_work = netprot::Deserialize(&log2, buf, buflen);
std::string str;
str.append(is_work ? "Y " : "N ").append(log->name).append(": ").append(std::to_string(log->sid));
str.append(is_work ? "Y " : "N ").append(log2.name).append(": ").append(std::to_string(log2.sid));
Log(str, false, false);
*/
///* Recevoir paquet */
//while (true) {
// char buffer[2048];