properly handle socket errors in recieving packets

This commit is contained in:
CPunch 2020-08-23 15:09:51 -05:00
parent 561a809f33
commit 8105d0aa88

View File

@ -78,7 +78,7 @@ bool CNSocket::sendData(uint8_t* data, int size) {
while (sentBytes < size) { while (sentBytes < size) {
int sent = send(sock, (buffer_t*)(data + sentBytes), size - sentBytes, 0); // no flags defined int sent = send(sock, (buffer_t*)(data + sentBytes), size - sentBytes, 0); // no flags defined
if (SOCKETERROR(sent)) { if (SOCKETERROR(sent)) {
if (errno == 11 && maxTries > 0) { if (errno == EAGAIN && maxTries > 0) {
maxTries--; maxTries--;
continue; // try again continue; // try again
} }
@ -181,6 +181,10 @@ void CNSocket::step() {
// we'll just leave bufferIndex at 0 since we already have the packet size, it's safe to overwrite those bytes // we'll just leave bufferIndex at 0 since we already have the packet size, it's safe to overwrite those bytes
activelyReading = true; activelyReading = true;
} else if (errno != EAGAIN) {
// serious socket issue, disconnect connection
kill();
return;
} }
} }
@ -189,6 +193,11 @@ void CNSocket::step() {
int recved = recv(sock, (buffer_t*)(readBuffer + readBufferIndex), readSize - readBufferIndex, 0); int recved = recv(sock, (buffer_t*)(readBuffer + readBufferIndex), readSize - readBufferIndex, 0);
if (!SOCKETERROR(recved)) if (!SOCKETERROR(recved))
readBufferIndex += recved; readBufferIndex += recved;
else if (errno != EAGAIN) {
// serious socket issue, disconnect connection
kill();
return;
}
} }
if (activelyReading && readBufferIndex - readSize <= 0) { if (activelyReading && readBufferIndex - readSize <= 0) {