Skip to content

Commit

Permalink
improved WebSocket closing behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
obiltschnig committed Jun 25, 2013
1 parent affb7ce commit cab19b2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Net/include/Poco/Net/WebSocket.h
@@ -1,7 +1,7 @@
//
// WebSocket.h
//
// $Id: //poco/1.4/Net/include/Poco/Net/WebSocket.h#2 $
// $Id: //poco/1.4/Net/include/Poco/Net/WebSocket.h#4 $
//
// Library: Net
// Package: WebSocket
Expand Down Expand Up @@ -230,8 +230,8 @@ class Net_API WebSocket: public StreamSocket
/// terminated.
///
/// Returns the number of bytes received.
/// A return value of 0 means a graceful shutdown
/// of the connection from the peer.
/// A return value of 0 means that the peer has
/// shut down or closed the connection.
///
/// Throws a TimeoutException if a receive timeout has
/// been set and nothing is received within that interval.
Expand Down
29 changes: 20 additions & 9 deletions Net/src/WebSocketImpl.cpp
@@ -1,7 +1,7 @@
//
// WebSocketImpl.cpp
//
// $Id: //poco/1.4/Net/src/WebSocketImpl.cpp#8 $
// $Id: //poco/1.4/Net/src/WebSocketImpl.cpp#9 $
//
// Library: Net
// Package: WebSocket
Expand Down Expand Up @@ -120,6 +120,11 @@ int WebSocketImpl::receiveBytes(void* buffer, int length, int)
{
char header[MAX_HEADER_LENGTH];
int n = receiveNBytes(header, 2);
if (n <= 0)
{
_frameFlags = 0;
return n;
}
poco_assert (n == 2);
Poco::UInt8 lengthByte = static_cast<Poco::UInt8>(header[1]);
int maskOffset = 0;
Expand All @@ -134,7 +139,8 @@ int WebSocketImpl::receiveBytes(void* buffer, int length, int)
n = receiveNBytes(header + 2, MAX_HEADER_LENGTH - 2);
}

poco_assert (n > 0);
if (n <= 0) throw WebSocketException("Incomplete frame received", WebSocket::WS_ERR_INCOMPLETE_FRAME);

n += 2;
Poco::MemoryInputStream istr(header, n);
Poco::BinaryReader reader(istr, Poco::BinaryReader::NETWORK_BYTE_ORDER);
Expand Down Expand Up @@ -179,7 +185,9 @@ int WebSocketImpl::receiveBytes(void* buffer, int length, int)
}
if (received < payloadLength)
{
received += receiveNBytes(reinterpret_cast<char*>(buffer) + received, payloadLength - received);
n = receiveNBytes(reinterpret_cast<char*>(buffer) + received, payloadLength - received);
if (n <= 0) throw WebSocketException("Incomplete frame received", WebSocket::WS_ERR_INCOMPLETE_FRAME);
received += n;
}
if (lengthByte & FRAME_FLAG_MASK)
{
Expand All @@ -196,13 +204,16 @@ int WebSocketImpl::receiveBytes(void* buffer, int length, int)
int WebSocketImpl::receiveNBytes(void* buffer, int bytes)
{
int received = _pStreamSocketImpl->receiveBytes(reinterpret_cast<char*>(buffer), bytes);
while (received < bytes)
if (received > 0)
{
int n = _pStreamSocketImpl->receiveBytes(reinterpret_cast<char*>(buffer) + received, bytes - received);
if (n > 0)
received += n;
else
throw WebSocketException("Incomplete frame received", WebSocket::WS_ERR_INCOMPLETE_FRAME);
while (received < bytes)
{
int n = _pStreamSocketImpl->receiveBytes(reinterpret_cast<char*>(buffer) + received, bytes - received);
if (n > 0)
received += n;
else
throw WebSocketException("Incomplete frame received", WebSocket::WS_ERR_INCOMPLETE_FRAME);
}
}
return received;
}
Expand Down

0 comments on commit cab19b2

Please sign in to comment.