Surprise.

This commit is contained in:
MarcEricMartel 2023-10-27 16:20:05 -04:00
parent 76a7d0218d
commit c23d1b2120

View File

@ -70,42 +70,54 @@ int Connector::Connect(const char* srv_addr, std::string name) {
buflen = 150;
bool ready = false;
int errors = 0;
int bytes = 0;
char* currbuf = buf;
netprot::PlayerInfo pinfo{};
while (!ready) {
uint32_t bytes = recv(m_sock_tcp, buf, buflen, 0);
uint32_t curr = 0;
char* currbuf = buf;
int len = recv(m_sock_tcp, &buf[bytes], buflen, 0);
if (len <= 0)
return 8;
bytes += len;
buflen -= bytes;
while (bytes > 0) {
switch (netprot::getType(currbuf, buflen)) {
case netprot::PACKET_TYPE::LOGINF:
if (bytes < sizeof(netprot::LoginInfo))
break;
if (!netprot::Deserialize(&m_loginfo, currbuf, buflen)) {
std::cout << "Packet LoginInfo invalide." << std::endl;
return 2;
}
curr = sizeof(netprot::LoginInfo);
currbuf = buf + sizeof(netprot::LoginInfo) + 2;
break;
case netprot::PACKET_TYPE::GAMEINFO:
if (bytes < sizeof(netprot::GameInfo))
break;
if (!netprot::Deserialize(&m_gameinfo, currbuf, buflen)) {
std::cout << "Packet GameInfo invalide." << std::endl;
return 3;
}
curr = sizeof(netprot::GameInfo);
currbuf = buf + sizeof(netprot::GameInfo) + 2;
break;
case netprot::PACKET_TYPE::PLAYINF:
if (bytes < sizeof(netprot::PlayerInfo))
break;
if (!netprot::Deserialize(&pinfo, currbuf, buflen)) {
std::cout << "Packet PlayerInfo invalide." << std::endl;
return 3;
}
m_players[pinfo.id] = pinfo;
curr = sizeof(netprot::PlayerInfo);
currbuf = buf + sizeof(netprot::PlayerInfo) + 2;
break;
case netprot::PACKET_TYPE::SYNC:
if (bytes < sizeof(netprot::Sync))
break;
if (!netprot::Deserialize(&m_origin, currbuf, buflen)) {
std::cout << "Packet Sync invalide." << std::endl;
return 3;
}
curr = sizeof(netprot::Sync);
ready = true;
break;
default:
@ -113,11 +125,8 @@ int Connector::Connect(const char* srv_addr, std::string name) {
//std::cout << "Packet invalide." << std::endl;
break;
}
currbuf = &currbuf[curr + 1];
bytes -= curr;
if (errors > 100)
return 1;
}
}
return 0;
}