Throttle overlapping screen updates

We need to make sure the display server has finished reading our
previous update before we overwrite the buffer with the next update.
diff --git a/vncviewer/CConn.cxx b/vncviewer/CConn.cxx
index fb136b5..262dd2c 100644
--- a/vncviewer/CConn.cxx
+++ b/vncviewer/CConn.cxx
@@ -46,6 +46,7 @@
 #include "CConn.h"
 #include "OptionsDialog.h"
 #include "DesktopWindow.h"
+#include "PlatformPixelBuffer.h"
 #include "i18n.h"
 #include "parameters.h"
 #include "vncviewer.h"
@@ -336,6 +337,9 @@
 // one.
 void CConn::framebufferUpdateStart()
 {
+  ModifiablePixelBuffer* pb;
+  PlatformPixelBuffer* ppb;
+
   CConnection::framebufferUpdateStart();
 
   // Note: This might not be true if sync fences are supported
@@ -343,6 +347,22 @@
 
   requestNewUpdate();
 
+  // We might still be rendering the previous update
+  pb = getFramebuffer();
+  assert(pb != NULL);
+  ppb = dynamic_cast<PlatformPixelBuffer*>(pb);
+  assert(ppb != NULL);
+  if (ppb->isRendering()) {
+    // Need to stop monitoring the socket or we'll just busy loop
+    assert(sock != NULL);
+    Fl::remove_fd(sock->getFd());
+
+    while (ppb->isRendering())
+      run_mainloop();
+
+    Fl::add_fd(sock->getFd(), FL_READ | FL_EXCEPT, socketEvent, this);
+  }
+
   // Update the screen prematurely for very slow updates
   Fl::add_timeout(1.0, handleUpdateTimeout, this);
 }