From b583cf5347fbd3f1d45e068a970e9cc207337e78 Mon Sep 17 00:00:00 2001
From: dscho <dscho>
Date: Sat, 16 Oct 2004 02:46:52 +0000
Subject: move read buffer to rfbClient structure (thread safety); make
 rfbClientLog overrideable

---
 libvncclient/rfbproto.c  | 10 +++++++---
 libvncclient/sockets.c   | 37 ++++++++++++++++---------------------
 libvncclient/tight.c     |  8 ++++----
 libvncclient/vncviewer.c |  6 +++++-
 libvncclient/zlib.c      |  4 ++--
 rfb/rfbclient.h          | 16 ++++++++++++----
 6 files changed, 46 insertions(+), 35 deletions(-)

diff --git a/libvncclient/rfbproto.c b/libvncclient/rfbproto.c
index cc4da85..0f12407 100644
--- a/libvncclient/rfbproto.c
+++ b/libvncclient/rfbproto.c
@@ -43,7 +43,7 @@
 rfbBool rfbEnableClientLogging=TRUE;
 
 void
-rfbClientLog(const char *format, ...)
+rfbDefaultClientLog(const char *format, ...)
 {
     va_list args;
     char buf[256];
@@ -64,6 +64,9 @@ rfbClientLog(const char *format, ...)
     va_end(args);
 }
 
+rfbClientLogProc rfbClientLog=rfbDefaultClientLog;
+rfbClientLogProc rfbClientErr=rfbDefaultClientLog;
+
 void FillRectangle(rfbClient* client, int x, int y, int w, int h, uint32_t colour) {
   int i,j;
 
@@ -271,13 +274,14 @@ InitialiseRFBConnection(rfbClient* client)
     errorMessageOnReadFailure = FALSE;
 
   if (!ReadFromRFBServer(client, pv, sz_rfbProtocolVersionMsg)) return FALSE;
+  pv[sz_rfbProtocolVersionMsg]=0;
 
   errorMessageOnReadFailure = TRUE;
 
   pv[sz_rfbProtocolVersionMsg] = 0;
 
   if (sscanf(pv,rfbProtocolVersionFormat,&major,&minor) != 2) {
-    rfbClientLog("Not a valid VNC server\n");
+    rfbClientLog("Not a valid VNC server (%s)\n",pv);
     return FALSE;
   }
 
@@ -769,7 +773,7 @@ HandleRFBServerMessage(rfbClient* client)
 	int y=rect.r.y, h=rect.r.h;
 
 	bytesPerLine = rect.r.w * client->format.bitsPerPixel / 8;
-	linesToRead = BUFFER_SIZE / bytesPerLine;
+	linesToRead = RFB_BUFFER_SIZE / bytesPerLine;
 
 	while (h > 0) {
 	  if (linesToRead > h)
diff --git a/libvncclient/sockets.c b/libvncclient/sockets.c
index 61298d4..b7cdbdd 100644
--- a/libvncclient/sockets.c
+++ b/libvncclient/sockets.c
@@ -36,11 +36,6 @@ void PrintInHex(char *buf, int len);
 
 rfbBool errorMessageOnReadFailure = TRUE;
 
-#define BUF_SIZE 8192
-static char buf[BUF_SIZE];
-static char *bufoutptr = buf;
-static int buffered = 0;
-
 /*
  * ReadFromRFBServer is called whenever we want to read some data from the RFB
  * server.  It is non-trivial for two reasons:
@@ -95,28 +90,28 @@ ReadFromRFBServer(rfbClient* client, char *out, unsigned int n)
     return (fread(out,1,n,rec->file)<0?FALSE:TRUE);
   }
   
-  if (n <= buffered) {
-    memcpy(out, bufoutptr, n);
-    bufoutptr += n;
-    buffered -= n;
+  if (n <= client->buffered) {
+    memcpy(out, client->bufoutptr, n);
+    client->bufoutptr += n;
+    client->buffered -= n;
 #ifdef DEBUG_READ_EXACT
     goto hexdump;
 #endif
     return TRUE;
   }
 
-  memcpy(out, bufoutptr, buffered);
+  memcpy(out, client->bufoutptr, client->buffered);
 
-  out += buffered;
-  n -= buffered;
+  out += client->buffered;
+  n -= client->buffered;
 
-  bufoutptr = buf;
-  buffered = 0;
+  client->bufoutptr = client->buf;
+  client->buffered = 0;
 
-  if (n <= BUF_SIZE) {
+  if (n <= RFB_BUF_SIZE) {
 
-    while (buffered < n) {
-      int i = read(client->sock, buf + buffered, BUF_SIZE - buffered);
+    while (client->buffered < n) {
+      int i = read(client->sock, client->buf + client->buffered, RFB_BUF_SIZE - client->buffered);
       if (i <= 0) {
 	if (i < 0) {
 	  if (errno == EWOULDBLOCK || errno == EAGAIN) {
@@ -135,12 +130,12 @@ ReadFromRFBServer(rfbClient* client, char *out, unsigned int n)
 	  return FALSE;
 	}
       }
-      buffered += i;
+      client->buffered += i;
     }
 
-    memcpy(out, bufoutptr, n);
-    bufoutptr += n;
-    buffered -= n;
+    memcpy(out, client->bufoutptr, n);
+    client->bufoutptr += n;
+    client->buffered -= n;
 
   } else {
 
diff --git a/libvncclient/tight.c b/libvncclient/tight.c
index 18d5639..6bcb905 100644
--- a/libvncclient/tight.c
+++ b/libvncclient/tight.c
@@ -227,10 +227,10 @@ HandleTightBPP (rfbClient* client, int rx, int ry, int rw, int rh)
 
   /* Read, decode and draw actual pixel data in a loop. */
 
-  bufferSize = BUFFER_SIZE * bitsPixel / (bitsPixel + BPP) & 0xFFFFFFFC;
+  bufferSize = RFB_BUFFER_SIZE * bitsPixel / (bitsPixel + BPP) & 0xFFFFFFFC;
   buffer2 = &client->buffer[bufferSize];
   if (rowSize > bufferSize) {
-    /* Should be impossible when BUFFER_SIZE >= 16384 */
+    /* Should be impossible when RFB_BUFFER_SIZE >= 16384 */
     rfbClientLog("Internal error: incorrect buffer size.\n");
     return FALSE;
   }
@@ -586,12 +586,12 @@ DecompressJpegRectBPP(rfbClient* client, int x, int y, int w, int h)
     if (jpegError) {
       break;
     }
-    pixelPtr = (CARDBPP *)&client->buffer[BUFFER_SIZE / 2];
+    pixelPtr = (CARDBPP *)&client->buffer[RFB_BUFFER_SIZE / 2];
     for (dx = 0; dx < w; dx++) {
       *pixelPtr++ =
 	RGB24_TO_PIXEL(BPP, client->buffer[dx*3], client->buffer[dx*3+1], client->buffer[dx*3+2]);
     }
-    CopyRectangle(client, &client->buffer[BUFFER_SIZE / 2], x, y + dy, w, 1);
+    CopyRectangle(client, &client->buffer[RFB_BUFFER_SIZE / 2], x, y + dy, w, 1);
     dy++;
   }
 
diff --git a/libvncclient/vncviewer.c b/libvncclient/vncviewer.c
index 417bd2f..914b422 100644
--- a/libvncclient/vncviewer.c
+++ b/libvncclient/vncviewer.c
@@ -23,6 +23,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <time.h>
 #include <rfb/rfbclient.h>
 
@@ -128,6 +129,9 @@ rfbClient* rfbGetClient(int bitsPerSample,int samplesPerPixel,
     }
   }
 
+  client->bufoutptr=client->buf;
+  client->buffered=0;
+
   client->HandleCursorPos = DummyPoint;
   client->SoftCursorLockArea = DummyRect;
   client->SoftCursorUnlockScreen = Dummy;
@@ -188,7 +192,7 @@ rfbBool rfbInitClient(rfbClient* client,int* argc,char** argv) {
       char* colon=strchr(argv[i],':');
 
       if(colon) {
-        client->serverHost=strndup(argv[i],colon-argv[i]);
+        client->serverHost=strndup(argv[i],(int)(colon-argv[i]));
 	client->serverPort=atoi(colon+1);
       } else {
 	client->serverHost=strdup(argv[i]);
diff --git a/libvncclient/zlib.c b/libvncclient/zlib.c
index 0479263..ec371be 100644
--- a/libvncclient/zlib.c
+++ b/libvncclient/zlib.c
@@ -96,8 +96,8 @@ HandleZlibBPP (rfbClient* client, int rx, int ry, int rw, int rh)
   while (( remaining > 0 ) &&
          ( inflateResult == Z_OK )) {
   
-    if ( remaining > BUFFER_SIZE ) {
-      toRead = BUFFER_SIZE;
+    if ( remaining > RFB_BUFFER_SIZE ) {
+      toRead = RFB_BUFFER_SIZE;
     }
     else {
       toRead = remaining;
diff --git a/rfb/rfbclient.h b/rfb/rfbclient.h
index 6983c31..b094ce2 100644
--- a/rfb/rfbclient.h
+++ b/rfb/rfbclient.h
@@ -102,7 +102,7 @@ typedef struct _rfbClient {
 	AppData appData;
 
 	const char* programName;
-	const char* serverHost;
+	char* serverHost;
 	int serverPort; /* if -1, then use file recorded by vncrec */
 	rfbBool listenSpecified;
 	int listenPort, flashPort;
@@ -112,8 +112,8 @@ typedef struct _rfbClient {
 	   Hextile also assumes it is big enough to hold 16 * 16 * 32 bits.
 	   Tight encoding assumes BUFFER_SIZE is at least 16384 bytes. */
 
-#define BUFFER_SIZE (640*480)
-	char buffer[BUFFER_SIZE];
+#define RFB_BUFFER_SIZE (640*480)
+	char buffer[RFB_BUFFER_SIZE];
 
 	/* rfbproto.c */
 
@@ -126,6 +126,13 @@ typedef struct _rfbClient {
 	char *serverCutText;
 	rfbBool newServerCutText;
 
+	/* sockets.c */
+#define RFB_BUF_SIZE 8192
+	char buf[RFB_BUF_SIZE];
+	char *bufoutptr;
+	int buffered;
+
+
 	/* cursor.c */
 	uint8_t *rcSource, *rcMask;
 
@@ -156,7 +163,8 @@ extern void listenForIncomingConnections(rfbClient* viewer);
 /* rfbproto.c */
 
 extern rfbBool rfbEnableClientLogging;
-extern void rfbClientLog(const char *format, ...);
+typedef void (*rfbClientLogProc)(const char *format, ...);
+extern rfbClientLogProc rfbClientLog,rfbClientErr;
 extern rfbBool ConnectToRFBServer(rfbClient* client,const char *hostname, int port);
 extern rfbBool InitialiseRFBConnection(rfbClient* client);
 extern rfbBool SetFormatAndEncodings(rfbClient* client);
-- 
cgit v1.2.3

