From 133360184487ce69ed58d72f82b9a5ca161eb1c2 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Sat, 16 May 2020 12:26:08 +0200 Subject: [PATCH] Check buffer usage with a simply boolean External callers don't need to know the exact details, only if there is data that needs to be flushed or not. --- common/rdr/BufferedOutStream.cxx | 14 +++++++------- common/rdr/BufferedOutStream.h | 4 +++- common/rfb/VNCSConnectionST.cxx | 8 ++++---- unix/x0vncserver/x0vncserver.cxx | 2 +- unix/xserver/hw/vnc/XserverDesktop.cc | 2 +- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/common/rdr/BufferedOutStream.cxx b/common/rdr/BufferedOutStream.cxx index cb2a0b0b..930b80b9 100644 --- a/common/rdr/BufferedOutStream.cxx +++ b/common/rdr/BufferedOutStream.cxx @@ -51,11 +51,6 @@ size_t BufferedOutStream::length() return offset + ptr - sentUpTo; } -size_t BufferedOutStream::bufferUsage() -{ - return ptr - sentUpTo; -} - void BufferedOutStream::flush() { struct timeval now; @@ -63,12 +58,12 @@ void BufferedOutStream::flush() while (sentUpTo < ptr) { size_t len; - len = bufferUsage(); + len = (ptr - sentUpTo); if (!flushBuffer(false)) break; - offset += len - bufferUsage(); + offset += len - (ptr - sentUpTo); } // Managed to flush everything? @@ -99,6 +94,11 @@ void BufferedOutStream::flush() } } +bool BufferedOutStream::hasBufferedData() +{ + return sentUpTo != ptr; +} + void BufferedOutStream::overrun(size_t needed) { size_t totalNeeded, newSize; diff --git a/common/rdr/BufferedOutStream.h b/common/rdr/BufferedOutStream.h index 05727f6e..dd64a136 100644 --- a/common/rdr/BufferedOutStream.h +++ b/common/rdr/BufferedOutStream.h @@ -38,7 +38,9 @@ namespace rdr { virtual size_t length(); virtual void flush(); - size_t bufferUsage(); + // hasBufferedData() checks if there is any data yet to be flushed + + bool hasBufferedData(); private: // flushBuffer() requests that the stream be flushed. Returns true if it is diff --git a/common/rfb/VNCSConnectionST.cxx b/common/rfb/VNCSConnectionST.cxx index e23e9b87..6ac9edbe 100644 --- a/common/rfb/VNCSConnectionST.cxx +++ b/common/rfb/VNCSConnectionST.cxx @@ -115,10 +115,10 @@ void VNCSConnectionST::close(const char* reason) vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason); try { - if (sock->outStream().bufferUsage() > 0) { + if (sock->outStream().hasBufferedData()) { sock->outStream().cork(false); sock->outStream().flush(); - if (sock->outStream().bufferUsage() > 0) + if (sock->outStream().hasBufferedData()) vlog.error("Failed to flush remaining socket data on close"); } } catch (rdr::Exception& e) { @@ -199,7 +199,7 @@ void VNCSConnectionST::flushSocket() sock->outStream().flush(); // Flushing the socket might release an update that was previously // delayed because of congestion. - if (sock->outStream().bufferUsage() == 0) + if (!sock->outStream().hasBufferedData()) writeFramebufferUpdate(); } catch (rdr::Exception &e) { close(e.str()); @@ -832,7 +832,7 @@ bool VNCSConnectionST::isCongested() // Stuff still waiting in the send buffer? sock->outStream().flush(); congestion.debugTrace("congestion-trace.csv", sock->getFd()); - if (sock->outStream().bufferUsage() > 0) + if (sock->outStream().hasBufferedData()) return true; if (!client.supportsFence()) diff --git a/unix/x0vncserver/x0vncserver.cxx b/unix/x0vncserver/x0vncserver.cxx index c406e3c9..a9782ada 100644 --- a/unix/x0vncserver/x0vncserver.cxx +++ b/unix/x0vncserver/x0vncserver.cxx @@ -307,7 +307,7 @@ int main(int argc, char** argv) delete (*i); } else { FD_SET((*i)->getFd(), &rfds); - if ((*i)->outStream().bufferUsage() > 0) + if ((*i)->outStream().hasBufferedData()) FD_SET((*i)->getFd(), &wfds); clients_connected++; } diff --git a/unix/xserver/hw/vnc/XserverDesktop.cc b/unix/xserver/hw/vnc/XserverDesktop.cc index 5c8b4cef..8215c936 100644 --- a/unix/xserver/hw/vnc/XserverDesktop.cc +++ b/unix/xserver/hw/vnc/XserverDesktop.cc @@ -366,7 +366,7 @@ void XserverDesktop::blockHandler(int* timeout) delete (*i); } else { /* Update existing NotifyFD to listen for write (or not) */ - vncSetNotifyFd(fd, screenIndex, true, (*i)->outStream().bufferUsage() > 0); + vncSetNotifyFd(fd, screenIndex, true, (*i)->outStream().hasBufferedData()); } } -- 2.39.5