Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Issue #544 - WebSocket waitForData timeout doesn't work #545

Merged
merged 1 commit into from
Feb 24, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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