diff options
author | Rubidium <rubidium@openttd.org> | 2021-04-18 09:55:00 +0200 |
---|---|---|
committer | rubidium42 <rubidium42@users.noreply.github.com> | 2021-04-24 20:42:01 +0200 |
commit | 98aa561cf759f75971afd5dfc4d42e6921a8ab1a (patch) | |
tree | cd542b51696f535460dc4ec0fc3115b97c1dd6e8 /src/network/core/tcp.cpp | |
parent | a2051bad503618f37e941aca3e4a5d53af1b0fbe (diff) | |
download | openttd-98aa561cf759f75971afd5dfc4d42e6921a8ab1a.tar.xz |
Codechange: encapsulate reading data from sockets into Packets to prevent packet state modifications outside of the Packet
Diffstat (limited to 'src/network/core/tcp.cpp')
-rw-r--r-- | src/network/core/tcp.cpp | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/src/network/core/tcp.cpp b/src/network/core/tcp.cpp index 1461a9298..aa1e1cbed 100644 --- a/src/network/core/tcp.cpp +++ b/src/network/core/tcp.cpp @@ -156,9 +156,8 @@ Packet *NetworkTCPSocketHandler::ReceivePacket() /* Read packet size */ if (!p->HasPacketSizeData()) { - while (!p->HasPacketSizeData()) { - /* Read the size of the packet */ - res = recv(this->sock, (char*)p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0); + while (p->RemainingBytesToTransfer() != 0) { + res = p->TransferIn<int>(recv, this->sock, 0); if (res == -1) { int err = GET_LAST_ERROR(); if (err != EWOULDBLOCK) { @@ -175,7 +174,6 @@ Packet *NetworkTCPSocketHandler::ReceivePacket() this->CloseConnection(); return nullptr; } - p->pos += res; } /* Parse the size in the received packet and if not valid, close the connection. */ @@ -186,8 +184,8 @@ Packet *NetworkTCPSocketHandler::ReceivePacket() } /* Read rest of packet */ - while (p->pos < p->size) { - res = recv(this->sock, (char*)p->buffer + p->pos, p->size - p->pos, 0); + while (p->RemainingBytesToTransfer() != 0) { + res = p->TransferIn<int>(recv, this->sock, 0); if (res == -1) { int err = GET_LAST_ERROR(); if (err != EWOULDBLOCK) { @@ -204,8 +202,6 @@ Packet *NetworkTCPSocketHandler::ReceivePacket() this->CloseConnection(); return nullptr; } - - p->pos += res; } /* Prepare for receiving a new packet */ |