From 98aa561cf759f75971afd5dfc4d42e6921a8ab1a Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 18 Apr 2021 09:55:00 +0200 Subject: Codechange: encapsulate reading data from sockets into Packets to prevent packet state modifications outside of the Packet --- src/network/core/tcp.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'src/network/core/tcp.cpp') 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(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(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 */ -- cgit v1.2.3-70-g09d2