Skip to content

Commit

Permalink
Merge pull request #545 from MartinNowak/fix544
Browse files Browse the repository at this point in the history
fix Issue #544 - WebSocket waitForData timeout doesn't work
  • Loading branch information
s-ludwig committed Feb 24, 2014
2 parents 60154a7 + cc79ae9 commit e145c18
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions source/vibe/http/websockets.d
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,33 @@ class WebSocket {
This function can be used in a read loop to cleanly determine when to stop reading.
*/
bool waitForData(Duration timeout = 0.seconds)
bool waitForData()
{
if (m_nextMessage) return true;

synchronized (m_readMutex) {
while (connected && m_nextMessage is null)
m_readCondition.wait();
}
return m_nextMessage !is null;
}

/// ditto
bool waitForData(Duration timeout)
{
import std.datetime;

if (m_nextMessage) return true;

immutable limit_time = Clock.currTime(UTC()) + timeout;

synchronized (m_readMutex) {
while (connected) {
if (timeout > 0.seconds) m_readCondition.wait(timeout);
else m_readCondition.wait();
if (m_nextMessage) return true;
while (connected && m_nextMessage is null && timeout > 0.seconds) {
m_readCondition.wait(timeout);
timeout = limit_time - Clock.currTime(UTC());
}
}
return false;
return m_nextMessage !is null;
}

/**
Expand Down

0 comments on commit e145c18

Please sign in to comment.