We were not handling the "sync next" fence properly as we sent the response
right after we got the request (instead of waiting for the next command).
This created a race where we could lose pixel format sync between the client
and the server.


git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@4943 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/common/rfb/VNCSConnectionST.cxx b/common/rfb/VNCSConnectionST.cxx
index 1ecd78c..381ee21 100644
--- a/common/rfb/VNCSConnectionST.cxx
+++ b/common/rfb/VNCSConnectionST.cxx
@@ -66,7 +66,8 @@
 VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
                                    bool reverse)
   : SConnection(reverse), sock(s), inProcessMessages(false),
-    syncFence(false), fenceFlags(0), fenceDataLen(0), fenceData(NULL),
+    pendingSyncFence(false), syncFence(false), fenceFlags(0),
+    fenceDataLen(0), fenceData(NULL),
     baseRTT(-1), minRTT(-1), seenCongestion(false), pingCounter(0),
     ackedOffset(0), sentOffset(0), congWindow(0), congestionTimer(this),
     server(server_),
@@ -156,7 +157,13 @@
     network::TcpSocket::cork(sock->getFd(), true);
 
     while (getInStream()->checkNoWait(1)) {
+      if (pendingSyncFence) {
+        syncFence = true;
+        pendingSyncFence = false;
+      }
+
       processMsg();
+
       if (syncFence) {
         writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
         syncFence = false;
@@ -627,10 +634,7 @@
 {
   if (flags & fenceFlagRequest) {
     if (flags & fenceFlagSyncNext) {
-      if (syncFence)
-        vlog.error("Fence trying to synchronise another fence");
-
-      syncFence = true;
+      pendingSyncFence = true;
 
       fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
       fenceDataLen = len;