Add endian-correctness to TCPSend&Rcv (parallel to the Launcher commit)

This commit is contained in:
Lion Kortlepel
2020-11-08 11:52:32 +01:00
parent 26383d5346
commit 1ef6cf53a2

View File

@@ -12,7 +12,8 @@
void TCPSend(Client*c,const std::string&Data){ void TCPSend(Client*c,const std::string&Data){
Assert(c); Assert(c);
if(c == nullptr)return; if(c == nullptr)return;
auto Size = int32_t(Data.size()); // Size is BIG ENDIAN now, use only for header!
auto Size = htonl(int32_t(Data.size()));
std::string Send(4,0); std::string Send(4,0);
memcpy(&Send[0],&Size,sizeof(Size)); memcpy(&Send[0],&Size,sizeof(Size));
Send += Data; Send += Data;
@@ -61,6 +62,8 @@ void TCPRcv(Client*c){
#else #else
BytesRcv = recv(c->GetTCPSock(), reinterpret_cast<void*>(&Header), sizeof(Header), 0); BytesRcv = recv(c->GetTCPSock(), reinterpret_cast<void*>(&Header), sizeof(Header), 0);
#endif #endif
// convert back to host endianness
Header = ntohl(Header);
#ifdef DEBUG #ifdef DEBUG
//debug(std::string(__func__) + Sec(": expecting ") + std::to_string(Header) + Sec(" bytes.")); //debug(std::string(__func__) + Sec(": expecting ") + std::to_string(Header) + Sec(" bytes."));
#endif // DEBUG #endif // DEBUG