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
<netinet/in.h> to includes in CNetwork.h because it's needed on
some platforms.
This commit is contained in:
crs 2002-10-30 22:16:30 +00:00
parent 9102fb80b9
commit cf13980bb8
2 changed files with 16 additions and 4 deletions

View File

@ -43,6 +43,7 @@ typedef int ssize_t;
#endif #endif
#if UNIX_LIKE #if UNIX_LIKE
# include <netinet/in.h>
# include <netdb.h> # include <netdb.h>
# include <errno.h> # include <errno.h>
#endif #endif
@ -183,9 +184,11 @@ public:
#if WINDOWS_LIKE #if WINDOWS_LIKE
kEADDRINUSE = WSAEADDRINUSE, kEADDRINUSE = WSAEADDRINUSE,
kECONNECTING = WSAEWOULDBLOCK, kECONNECTING = WSAEWOULDBLOCK,
kEINTR = WSAEINTR,
#elif UNIX_LIKE #elif UNIX_LIKE
kEADDRINUSE = EADDRINUSE, kEADDRINUSE = EADDRINUSE,
kECONNECTING = EINPROGRESS, kECONNECTING = EINPROGRESS,
kEINTR = EINTR,
#endif #endif
kNone = 0 kNone = 0
}; };

View File

@ -292,6 +292,12 @@ CTCPSocket::ioService()
m_input->hangup(); m_input->hangup();
m_connected &= ~kRead; m_connected &= ~kRead;
} }
else {
// socket failed
if (CNetwork::getsockerror() != CNetwork::kEINTR) {
return;
}
}
} }
// write some data // write some data
@ -303,14 +309,17 @@ CTCPSocket::ioService()
// write data // write data
const void* buffer = m_output->peek(n); 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 // discard written data
if (n > 0) { if (n2 > 0) {
m_output->pop(n); m_output->pop(n);
} }
else if (n == (UInt32)-1 && CNetwork::getsockerror() == EPIPE) { else if (n2 < 0) {
return; // socket failed
if (CNetwork::getsockerror() != CNetwork::kEINTR) {
return;
}
} }
} }
} }