aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVincent Richard <[email protected]>2012-10-15 09:48:14 +0000
committerVincent Richard <[email protected]>2012-10-15 11:33:54 +0000
commit794afe9a1b5ee36a5de8f90f3d789ca46f393bb7 (patch)
tree06d2cb077b0002b65819c14b35947ad71874469a /src
parentSupport for bogus encoding name 'bmoted-printable' (Zarafa). (diff)
downloadvmime-794afe9a1b5ee36a5de8f90f3d789ca46f393bb7.tar.gz
vmime-794afe9a1b5ee36a5de8f90f3d789ca46f393bb7.zip
Added support for timeout when receiving data from a socket (POSIX platform).
Diffstat (limited to 'src')
-rw-r--r--src/platforms/posix/posixSocket.cpp50
1 files changed, 49 insertions, 1 deletions
diff --git a/src/platforms/posix/posixSocket.cpp b/src/platforms/posix/posixSocket.cpp
index b8bb8b18..f280dadc 100644
--- a/src/platforms/posix/posixSocket.cpp
+++ b/src/platforms/posix/posixSocket.cpp
@@ -336,7 +336,45 @@ void posixSocket::receive(vmime::string& buffer)
posixSocket::size_type posixSocket::receiveRaw(char* buffer, const size_type count)
{
- const int ret = ::recv(m_desc, buffer, count, 0);
+ // Check whether data is available
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(m_desc, &fds);
+
+ struct timeval tv;
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+
+ int ret = ::select(m_desc + 1, &fds, NULL, NULL, &tv);
+
+ if (ret < 0)
+ {
+ if (errno != EAGAIN)
+ throwSocketError(errno);
+
+ // No data available at this time
+ // Check if we are timed out
+ if (m_timeoutHandler &&
+ m_timeoutHandler->isTimeOut())
+ {
+ if (!m_timeoutHandler->handleTimeOut())
+ {
+ // Server did not react within timeout delay
+ throwSocketError(errno);
+ }
+ else
+ {
+ // Reset timeout
+ m_timeoutHandler->resetTimeOut();
+ }
+ }
+
+ // Continue waiting for data
+ return 0;
+ }
+
+ // Read available data
+ ret = ::recv(m_desc, buffer, count, 0);
if (ret < 0)
{
@@ -351,6 +389,12 @@ posixSocket::size_type posixSocket::receiveRaw(char* buffer, const size_type cou
// Host shutdown
throwSocketError(ENOTCONN);
}
+ else
+ {
+ // Data received, reset timeout
+ if (m_timeoutHandler)
+ m_timeoutHandler->resetTimeOut();
+ }
return ret;
}
@@ -383,6 +427,10 @@ void posixSocket::sendRaw(const char* buffer, const size_type count)
size -= ret;
}
}
+
+ // Reset timeout
+ if (m_timeoutHandler)
+ m_timeoutHandler->resetTimeOut();
}