]> git.tdb.fi Git - libs/net.git/commitdiff
Check for EOF return before checking for errors
authorMikko Rasa <tdb@tdb.fi>
Sun, 11 Dec 2022 10:17:12 +0000 (12:17 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 11 Dec 2022 16:26:44 +0000 (18:26 +0200)
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
source/net/unix/socket.cpp

index fbbd2415c693911b88063f0091600d0d3e73f25a..7987f4b82bce50212ab23cbe1be573a71ef796d6 100644 (file)
@@ -3,6 +3,8 @@
 #include <msp/core/systemerror.h>
 #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<size_t>::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
index d87cdd3a72907109133929d5b8a405e6237cc2cf..2c1e2ad8d355ac9e0fe9a31b5292f0709535c2f8 100644 (file)
@@ -64,7 +64,7 @@ size_t check_sys_error(make_signed<size_t>::type ret, const char *func)
 {
        if(ret<0)
        {
-               if(errno==EAGAIN)
+               if(errno==EAGAIN || errno==EWOULDBLOCK)
                        return 0;
                else
                        throw system_error(func);