From cf13980bb80acf308d44fe91e9907127a56ec987 Mon Sep 17 00:00:00 2001 From: crs Date: Wed, 30 Oct 2002 22:16:30 +0000 Subject: [PATCH] Fixed bugs in error handling in CTCPSocket; previously was not handling read errors at all and error handling for writes was never being used. Now the socket disconnects if a read or write fails on the socket for any reason except EINTR. Also added to includes in CNetwork.h because it's needed on some platforms. --- lib/net/CNetwork.h | 3 +++ lib/net/CTCPSocket.cpp | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/net/CNetwork.h b/lib/net/CNetwork.h index 06c2e411..30401ff2 100644 --- a/lib/net/CNetwork.h +++ b/lib/net/CNetwork.h @@ -43,6 +43,7 @@ typedef int ssize_t; #endif #if UNIX_LIKE +# include # include # include #endif @@ -183,9 +184,11 @@ public: #if WINDOWS_LIKE kEADDRINUSE = WSAEADDRINUSE, kECONNECTING = WSAEWOULDBLOCK, + kEINTR = WSAEINTR, #elif UNIX_LIKE kEADDRINUSE = EADDRINUSE, kECONNECTING = EINPROGRESS, + kEINTR = EINTR, #endif kNone = 0 }; diff --git a/lib/net/CTCPSocket.cpp b/lib/net/CTCPSocket.cpp index a9937537..0832904f 100644 --- a/lib/net/CTCPSocket.cpp +++ b/lib/net/CTCPSocket.cpp @@ -292,6 +292,12 @@ CTCPSocket::ioService() m_input->hangup(); m_connected &= ~kRead; } + else { + // socket failed + if (CNetwork::getsockerror() != CNetwork::kEINTR) { + return; + } + } } // write some data @@ -303,14 +309,17 @@ CTCPSocket::ioService() // write data const void* buffer = m_output->peek(n); - n = (UInt32)CNetwork::write(m_fd, buffer, n); + ssize_t n2 = (UInt32)CNetwork::write(m_fd, buffer, n); // discard written data - if (n > 0) { + if (n2 > 0) { m_output->pop(n); } - else if (n == (UInt32)-1 && CNetwork::getsockerror() == EPIPE) { - return; + else if (n2 < 0) { + // socket failed + if (CNetwork::getsockerror() != CNetwork::kEINTR) { + return; + } } } }