Skip to content
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions ixwebsocket/IXWebSocketTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,9 +547,12 @@ namespace ix

if (_rxbuf.size() < ws.header_size + ws.N)
{
_rxbufWanted = ws.header_size + ws.N;
return; /* Need: ws.header_size+ws.N - _rxbuf.size() */
}

_rxbufWanted = 0;

if (!ws.fin && (ws.opcode == wsheader_type::PING || ws.opcode == wsheader_type::PONG ||
ws.opcode == wsheader_type::CLOSE))
{
Expand Down Expand Up @@ -1098,6 +1101,18 @@ namespace ix
{
while (true)
{
// If _rxbufWanted isn't set, don't attempt to read more than kChunkSize
// into _rxbuf. If a client is sending frames faster than they can be
// processed this would otherwise bloat _rxbuf and further introduce
// unnecessary processing overhead due to .erase() in dispatch().
//
// Further, not reading everything from the socket will eventually
// result in back pressure for the client.
if (_rxbufWanted == 0 && _rxbuf.size() >= kChunkSize) break;

// There's also no point in reading more bytes than needed.
if (_rxbufWanted > 0 && _rxbuf.size() >= _rxbufWanted) break;

ssize_t ret = _socket->recv((char*) &_readbuf[0], _readbuf.size());

if (ret < 0 && Socket::isWaitNeeded())
Expand Down
4 changes: 4 additions & 0 deletions ixwebsocket/IXWebSocketTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ namespace ix
// data messages. That buffer is resized
std::vector<uint8_t> _rxbuf;

// If set to a positive value, only read bytes from the socket until
// _rxbuf has reached this size to avoid unnecessary erase churn.
uint64_t _rxbufWanted = 0;

// Contains all messages that are waiting to be sent
std::vector<uint8_t> _txbuf;
mutable std::mutex _txbufMutex;
Expand Down
Loading