summaryrefslogtreecommitdiff
path: root/lib/server
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2014-08-15 22:48:10 +0000
committerChris Wilson <chris+github@qwirx.com>2014-08-15 22:48:10 +0000
commit89e5218a1de761452ba1c38b512cd3c00c64e3ad (patch)
tree39aaf7df0e8f99b1265605629f12722f91f7063a /lib/server
parent015a32555fc2c2435a83123add5f26614e6e41a6 (diff)
Fix poll timeout calculation for infinite timeouts.
Diffstat (limited to 'lib/server')
-rw-r--r--lib/server/SocketStream.cpp11
-rw-r--r--lib/server/SocketStream.h26
2 files changed, 23 insertions, 14 deletions
diff --git a/lib/server/SocketStream.cpp b/lib/server/SocketStream.cpp
index dafb4338..db71227f 100644
--- a/lib/server/SocketStream.cpp
+++ b/lib/server/SocketStream.cpp
@@ -218,7 +218,7 @@ int SocketStream::Read(void *pBuffer, int NBytes, int Timeout)
p.fd = mSocketHandle;
p.events = POLLIN;
p.revents = 0;
- switch(::poll(&p, 1, PollTimeout(Timeout)))
+ switch(::poll(&p, 1, PollTimeout(Timeout, 0)))
{
case -1:
// error
@@ -287,13 +287,11 @@ bool SocketStream::Poll(short Events, int Timeout)
p.revents = 0;
box_time_t start = GetCurrentBoxTime();
- box_time_t end = start + MilliSecondsToBoxTime(Timeout);
int result;
do
{
- box_time_t now = GetCurrentBoxTime();
- result = ::poll(&p, 1, PollTimeout(end - now));
+ result = ::poll(&p, 1, PollTimeout(Timeout, start));
}
while(result == -1 && errno == EINTR);
@@ -337,7 +335,6 @@ void SocketStream::Write(const void *pBuffer, int NBytes, int Timeout)
// Bytes left to send
int bytesLeft = NBytes;
box_time_t start = GetCurrentBoxTime();
- box_time_t end = start + MilliSecondsToBoxTime(Timeout);
while(bytesLeft > 0)
{
@@ -370,9 +367,7 @@ void SocketStream::Write(const void *pBuffer, int NBytes, int Timeout)
mSocketHandle << " (" << bytesLeft <<
" of " << NBytes << " bytes left)");
- box_time_t now = GetCurrentBoxTime();
-
- if(!Poll(POLLOUT, PollTimeout(end - now)))
+ if(!Poll(POLLOUT, PollTimeout(Timeout, start)))
{
THROW_EXCEPTION_MESSAGE(ConnectionException,
Protocol_Timeout, "Timed out waiting "
diff --git a/lib/server/SocketStream.h b/lib/server/SocketStream.h
index 406d29e4..c3a67cb0 100644
--- a/lib/server/SocketStream.h
+++ b/lib/server/SocketStream.h
@@ -59,19 +59,33 @@ protected:
void MarkAsWriteClosed() {mWriteClosed = true;}
void CheckForMissingTimeout(int Timeout);
- int PollTimeout(box_time_t Timeout)
+ int PollTimeout(int timeout, box_time_t start_time)
{
- if (Timeout < 0)
+ if (timeout == IOStream::TimeOutInfinite)
{
- return 0;
+ return INFTIM;
}
- else if (Timeout == IOStream::TimeOutInfinite || Timeout > INT_MAX)
+
+ if (start_time == 0)
{
- return INFTIM;
+ return timeout; // no adjustment possible
+ }
+
+ box_time_t end_time = start_time + MilliSecondsToBoxTime(timeout);
+ box_time_t now = GetCurrentBoxTime();
+ box_time_t remaining = end_time - now;
+
+ if (remaining < 0)
+ {
+ return 0; // no delay
+ }
+ else if (BoxTimeToMilliSeconds(remaining) > INT_MAX)
+ {
+ return INT_MAX;
}
else
{
- return (int) Timeout;
+ return (int) BoxTimeToMilliSeconds(remaining);
}
}
bool Poll(short Events, int Timeout);