improved WebSocket closing behavior

This commit is contained in:
Guenter Obiltschnig 2013-06-25 13:56:28 +02:00
parent affb7ce5a4
commit cab19b21a4
2 changed files with 23 additions and 12 deletions

View File

@ -1,7 +1,7 @@
// //
// WebSocket.h // 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 // Library: Net
// Package: WebSocket // Package: WebSocket
@ -230,8 +230,8 @@ public:
/// terminated. /// terminated.
/// ///
/// Returns the number of bytes received. /// Returns the number of bytes received.
/// A return value of 0 means a graceful shutdown /// A return value of 0 means that the peer has
/// of the connection from the peer. /// shut down or closed the connection.
/// ///
/// Throws a TimeoutException if a receive timeout has /// Throws a TimeoutException if a receive timeout has
/// been set and nothing is received within that interval. /// been set and nothing is received within that interval.

View File

@ -1,7 +1,7 @@
// //
// WebSocketImpl.cpp // WebSocketImpl.cpp
// //
// $Id: //poco/1.4/Net/src/WebSocketImpl.cpp#8 $ // $Id: //poco/1.4/Net/src/WebSocketImpl.cpp#9 $
// //
// Library: Net // Library: Net
// Package: WebSocket // Package: WebSocket
@ -120,6 +120,11 @@ int WebSocketImpl::receiveBytes(void* buffer, int length, int)
{ {
char header[MAX_HEADER_LENGTH]; char header[MAX_HEADER_LENGTH];
int n = receiveNBytes(header, 2); int n = receiveNBytes(header, 2);
if (n <= 0)
{
_frameFlags = 0;
return n;
}
poco_assert (n == 2); poco_assert (n == 2);
Poco::UInt8 lengthByte = static_cast<Poco::UInt8>(header[1]); Poco::UInt8 lengthByte = static_cast<Poco::UInt8>(header[1]);
int maskOffset = 0; int maskOffset = 0;
@ -134,7 +139,8 @@ int WebSocketImpl::receiveBytes(void* buffer, int length, int)
n = receiveNBytes(header + 2, MAX_HEADER_LENGTH - 2); 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; n += 2;
Poco::MemoryInputStream istr(header, n); Poco::MemoryInputStream istr(header, n);
Poco::BinaryReader reader(istr, Poco::BinaryReader::NETWORK_BYTE_ORDER); Poco::BinaryReader reader(istr, Poco::BinaryReader::NETWORK_BYTE_ORDER);
@ -179,7 +185,9 @@ int WebSocketImpl::receiveBytes(void* buffer, int length, int)
} }
if (received < payloadLength) 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) if (lengthByte & FRAME_FLAG_MASK)
{ {
@ -196,13 +204,16 @@ int WebSocketImpl::receiveBytes(void* buffer, int length, int)
int WebSocketImpl::receiveNBytes(void* buffer, int bytes) int WebSocketImpl::receiveNBytes(void* buffer, int bytes)
{ {
int received = _pStreamSocketImpl->receiveBytes(reinterpret_cast<char*>(buffer), 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); while (received < bytes)
if (n > 0) {
received += n; int n = _pStreamSocketImpl->receiveBytes(reinterpret_cast<char*>(buffer) + received, bytes - received);
else if (n > 0)
throw WebSocketException("Incomplete frame received", WebSocket::WS_ERR_INCOMPLETE_FRAME); received += n;
else
throw WebSocketException("Incomplete frame received", WebSocket::WS_ERR_INCOMPLETE_FRAME);
}
} }
return received; return received;
} }