From 917f288a95771f6257f38fd1b060a3932c0c345f Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 18 Oct 2013 08:26:48 +0200 Subject: [PATCH] Clean up socket code. --- lib/base/bufferedstream.cpp | 4 ++-- lib/base/networkstream.cpp | 21 +++++++++++++++++++-- lib/base/socket.cpp | 2 +- lib/base/socket.h | 2 -- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/base/bufferedstream.cpp b/lib/base/bufferedstream.cpp index 0e7c48931..8277e32c0 100644 --- a/lib/base/bufferedstream.cpp +++ b/lib/base/bufferedstream.cpp @@ -210,5 +210,5 @@ bool BufferedStream::IsEof(void) const { boost::mutex::scoped_lock lock(m_Mutex); - return m_Eof; -} \ No newline at end of file + return m_InnerStream->IsEof() && m_RecvQ->GetAvailableBytes() == 0; +} diff --git a/lib/base/networkstream.cpp b/lib/base/networkstream.cpp index 3263d459a..ea86ff2cd 100644 --- a/lib/base/networkstream.cpp +++ b/lib/base/networkstream.cpp @@ -43,7 +43,15 @@ void NetworkStream::Close(void) */ 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) 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) { - 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) BOOST_THROW_EXCEPTION(std::runtime_error("Short write for socket.")); } diff --git a/lib/base/socket.cpp b/lib/base/socket.cpp index a5f1b06c0..667cbfe99 100644 --- a/lib/base/socket.cpp +++ b/lib/base/socket.cpp @@ -62,8 +62,8 @@ Socket::~Socket(void) void Socket::SetFD(SOCKET fd) { if (fd != INVALID_SOCKET) { - /* mark the socket as close-on-exec */ #ifndef _WIN32 + /* mark the socket as close-on-exec */ Utility::SetCloExec(fd); #endif /* _WIN32 */ } diff --git a/lib/base/socket.h b/lib/base/socket.h index b8506daa4..c7ab87a04 100644 --- a/lib/base/socket.h +++ b/lib/base/socket.h @@ -58,8 +58,6 @@ protected: void SetFD(SOCKET fd); SOCKET GetFD(void) const; - void SetConnected(bool connected); - int GetError(void) const; mutable boost::mutex m_SocketMutex; -- 2.40.0