From 974941f4fa98b7056954263866df81b642fc92d4 Mon Sep 17 00:00:00 2001 From: dongresource Date: Fri, 11 Dec 2020 22:41:51 +0100 Subject: [PATCH] Close our end of the connection when we receive an EOF This should/might fix the server's CPU usage spiking to 100%. --- src/CNProtocol.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/CNProtocol.cpp b/src/CNProtocol.cpp index 7fd7906..8174244 100644 --- a/src/CNProtocol.cpp +++ b/src/CNProtocol.cpp @@ -167,7 +167,10 @@ void CNSocket::step() { if (readSize <= 0) { // we aren't reading a packet yet, try to start looking for one int recved = recv(sock, (buffer_t*)readBuffer, sizeof(int32_t), 0); - if (!SOCKETERROR(recved)) { + if (recved == 0) { + // the socket was closed normally + kill(); + } else if (!SOCKETERROR(recved)) { // we got our packet size!!!! readSize = *((int32_t*)readBuffer); // sanity check @@ -189,7 +192,10 @@ void CNSocket::step() { if (readSize > 0 && readBufferIndex < readSize) { // read until the end of the packet! (or at least try too) int recved = recv(sock, (buffer_t*)(readBuffer + readBufferIndex), readSize - readBufferIndex, 0); - if (!SOCKETERROR(recved)) + if (recved == 0) { + // the socket was closed normally + kill(); + } else if (!SOCKETERROR(recved)) readBufferIndex += recved; else if (OF_ERRNO != OF_EWOULD) { // serious socket issue, disconnect connection