Clean up socket code.

This commit is contained in:
Gunnar Beutner 2013-10-18 08:26:48 +02:00
parent fb0a100e24
commit 917f288a95
4 changed files with 22 additions and 7 deletions

View File

@ -210,5 +210,5 @@ bool BufferedStream::IsEof(void) const
{ {
boost::mutex::scoped_lock lock(m_Mutex); boost::mutex::scoped_lock lock(m_Mutex);
return m_Eof; return m_InnerStream->IsEof() && m_RecvQ->GetAvailableBytes() == 0;
} }

View File

@ -43,7 +43,15 @@ void NetworkStream::Close(void)
*/ */
size_t NetworkStream::Read(void *buffer, size_t count) size_t NetworkStream::Read(void *buffer, size_t count)
{ {
size_t rc = m_Socket->Read(buffer, count); size_t rc;
try {
rc = m_Socket->Read(buffer, count);
} catch (...) {
m_Eof = true;
throw;
}
if (rc == 0) if (rc == 0)
m_Eof = true; m_Eof = true;
@ -60,7 +68,16 @@ size_t NetworkStream::Read(void *buffer, size_t count)
*/ */
void NetworkStream::Write(const void *buffer, size_t count) void NetworkStream::Write(const void *buffer, size_t count)
{ {
size_t rc = m_Socket->Write(buffer, count); size_t rc;
try {
rc = m_Socket->Write(buffer, count);
} catch (...) {
m_Eof = true;
throw;
}
if (rc < count) if (rc < count)
BOOST_THROW_EXCEPTION(std::runtime_error("Short write for socket.")); BOOST_THROW_EXCEPTION(std::runtime_error("Short write for socket."));
} }

View File

@ -62,8 +62,8 @@ Socket::~Socket(void)
void Socket::SetFD(SOCKET fd) void Socket::SetFD(SOCKET fd)
{ {
if (fd != INVALID_SOCKET) { if (fd != INVALID_SOCKET) {
/* mark the socket as close-on-exec */
#ifndef _WIN32 #ifndef _WIN32
/* mark the socket as close-on-exec */
Utility::SetCloExec(fd); Utility::SetCloExec(fd);
#endif /* _WIN32 */ #endif /* _WIN32 */
} }

View File

@ -58,8 +58,6 @@ protected:
void SetFD(SOCKET fd); void SetFD(SOCKET fd);
SOCKET GetFD(void) const; SOCKET GetFD(void) const;
void SetConnected(bool connected);
int GetError(void) const; int GetError(void) const;
mutable boost::mutex m_SocketMutex; mutable boost::mutex m_SocketMutex;