From 63432878fc84bc666bb74efaeee588fa6d03d5b2 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 11 Dec 2022 12:17:12 +0200 Subject: [PATCH] Check for EOF return before checking for errors The check_sys_error function converts errors from non-blocking operations which would block into a zero return, making them indistinguishable from a zero return due to the other end closing the connection. --- source/net/clientsocket.cpp | 17 +++++++++++------ source/net/unix/socket.cpp | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/source/net/clientsocket.cpp b/source/net/clientsocket.cpp index fbbd241..7987f4b 100644 --- a/source/net/clientsocket.cpp +++ b/source/net/clientsocket.cpp @@ -3,6 +3,8 @@ #include #include "socket_private.h" +using namespace std; + namespace Msp { namespace Net { @@ -75,15 +77,18 @@ size_t ClientSocket::do_read(char *buf, size_t size) if(size==0) return 0; - size_t ret = check_sys_error(::recv(priv->handle, buf, size, 0), "recv"); - if(ret==0 && !eof_flag) + make_signed::type ret = ::recv(priv->handle, buf, size, 0); + if(ret==0) { - eof_flag = true; - set_socket_events(S_NONE); - signal_end_of_file.emit(); + if(!eof_flag) + { + set_socket_events(S_NONE); + set_eof(); + } + return 0; } - return ret; + return check_sys_error(ret, "recv"); } } // namespace Net diff --git a/source/net/unix/socket.cpp b/source/net/unix/socket.cpp index d87cdd3..2c1e2ad 100644 --- a/source/net/unix/socket.cpp +++ b/source/net/unix/socket.cpp @@ -64,7 +64,7 @@ size_t check_sys_error(make_signed::type ret, const char *func) { if(ret<0) { - if(errno==EAGAIN) + if(errno==EAGAIN || errno==EWOULDBLOCK) return 0; else throw system_error(func); -- 2.43.0