move read buffer to rfbClient structure (thread safety); make rfbClientLog

overrideable
pull/1/head
dscho 20 years ago
parent 8715a8ab42
commit b583cf5347

@ -43,7 +43,7 @@
rfbBool rfbEnableClientLogging=TRUE; rfbBool rfbEnableClientLogging=TRUE;
void void
rfbClientLog(const char *format, ...) rfbDefaultClientLog(const char *format, ...)
{ {
va_list args; va_list args;
char buf[256]; char buf[256];
@ -64,6 +64,9 @@ rfbClientLog(const char *format, ...)
va_end(args); va_end(args);
} }
rfbClientLogProc rfbClientLog=rfbDefaultClientLog;
rfbClientLogProc rfbClientErr=rfbDefaultClientLog;
void FillRectangle(rfbClient* client, int x, int y, int w, int h, uint32_t colour) { void FillRectangle(rfbClient* client, int x, int y, int w, int h, uint32_t colour) {
int i,j; int i,j;
@ -271,13 +274,14 @@ InitialiseRFBConnection(rfbClient* client)
errorMessageOnReadFailure = FALSE; errorMessageOnReadFailure = FALSE;
if (!ReadFromRFBServer(client, pv, sz_rfbProtocolVersionMsg)) return FALSE; if (!ReadFromRFBServer(client, pv, sz_rfbProtocolVersionMsg)) return FALSE;
pv[sz_rfbProtocolVersionMsg]=0;
errorMessageOnReadFailure = TRUE; errorMessageOnReadFailure = TRUE;
pv[sz_rfbProtocolVersionMsg] = 0; pv[sz_rfbProtocolVersionMsg] = 0;
if (sscanf(pv,rfbProtocolVersionFormat,&major,&minor) != 2) { 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; return FALSE;
} }
@ -769,7 +773,7 @@ HandleRFBServerMessage(rfbClient* client)
int y=rect.r.y, h=rect.r.h; int y=rect.r.y, h=rect.r.h;
bytesPerLine = rect.r.w * client->format.bitsPerPixel / 8; bytesPerLine = rect.r.w * client->format.bitsPerPixel / 8;
linesToRead = BUFFER_SIZE / bytesPerLine; linesToRead = RFB_BUFFER_SIZE / bytesPerLine;
while (h > 0) { while (h > 0) {
if (linesToRead > h) if (linesToRead > h)

@ -36,11 +36,6 @@ void PrintInHex(char *buf, int len);
rfbBool errorMessageOnReadFailure = TRUE; 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 * ReadFromRFBServer is called whenever we want to read some data from the RFB
* server. It is non-trivial for two reasons: * 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); return (fread(out,1,n,rec->file)<0?FALSE:TRUE);
} }
if (n <= buffered) { if (n <= client->buffered) {
memcpy(out, bufoutptr, n); memcpy(out, client->bufoutptr, n);
bufoutptr += n; client->bufoutptr += n;
buffered -= n; client->buffered -= n;
#ifdef DEBUG_READ_EXACT #ifdef DEBUG_READ_EXACT
goto hexdump; goto hexdump;
#endif #endif
return TRUE; return TRUE;
} }
memcpy(out, bufoutptr, buffered); memcpy(out, client->bufoutptr, client->buffered);
out += buffered; out += client->buffered;
n -= buffered; n -= client->buffered;
bufoutptr = buf; client->bufoutptr = client->buf;
buffered = 0; client->buffered = 0;
if (n <= BUF_SIZE) { if (n <= RFB_BUF_SIZE) {
while (buffered < n) { while (client->buffered < n) {
int i = read(client->sock, buf + buffered, BUF_SIZE - buffered); int i = read(client->sock, client->buf + client->buffered, RFB_BUF_SIZE - client->buffered);
if (i <= 0) { if (i <= 0) {
if (i < 0) { if (i < 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN) { if (errno == EWOULDBLOCK || errno == EAGAIN) {
@ -135,12 +130,12 @@ ReadFromRFBServer(rfbClient* client, char *out, unsigned int n)
return FALSE; return FALSE;
} }
} }
buffered += i; client->buffered += i;
} }
memcpy(out, bufoutptr, n); memcpy(out, client->bufoutptr, n);
bufoutptr += n; client->bufoutptr += n;
buffered -= n; client->buffered -= n;
} else { } else {

@ -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. */ /* 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]; buffer2 = &client->buffer[bufferSize];
if (rowSize > 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"); rfbClientLog("Internal error: incorrect buffer size.\n");
return FALSE; return FALSE;
} }
@ -586,12 +586,12 @@ DecompressJpegRectBPP(rfbClient* client, int x, int y, int w, int h)
if (jpegError) { if (jpegError) {
break; break;
} }
pixelPtr = (CARDBPP *)&client->buffer[BUFFER_SIZE / 2]; pixelPtr = (CARDBPP *)&client->buffer[RFB_BUFFER_SIZE / 2];
for (dx = 0; dx < w; dx++) { for (dx = 0; dx < w; dx++) {
*pixelPtr++ = *pixelPtr++ =
RGB24_TO_PIXEL(BPP, client->buffer[dx*3], client->buffer[dx*3+1], client->buffer[dx*3+2]); 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++; dy++;
} }

@ -23,6 +23,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <time.h> #include <time.h>
#include <rfb/rfbclient.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->HandleCursorPos = DummyPoint;
client->SoftCursorLockArea = DummyRect; client->SoftCursorLockArea = DummyRect;
client->SoftCursorUnlockScreen = Dummy; client->SoftCursorUnlockScreen = Dummy;
@ -188,7 +192,7 @@ rfbBool rfbInitClient(rfbClient* client,int* argc,char** argv) {
char* colon=strchr(argv[i],':'); char* colon=strchr(argv[i],':');
if(colon) { if(colon) {
client->serverHost=strndup(argv[i],colon-argv[i]); client->serverHost=strndup(argv[i],(int)(colon-argv[i]));
client->serverPort=atoi(colon+1); client->serverPort=atoi(colon+1);
} else { } else {
client->serverHost=strdup(argv[i]); client->serverHost=strdup(argv[i]);

@ -96,8 +96,8 @@ HandleZlibBPP (rfbClient* client, int rx, int ry, int rw, int rh)
while (( remaining > 0 ) && while (( remaining > 0 ) &&
( inflateResult == Z_OK )) { ( inflateResult == Z_OK )) {
if ( remaining > BUFFER_SIZE ) { if ( remaining > RFB_BUFFER_SIZE ) {
toRead = BUFFER_SIZE; toRead = RFB_BUFFER_SIZE;
} }
else { else {
toRead = remaining; toRead = remaining;

@ -102,7 +102,7 @@ typedef struct _rfbClient {
AppData appData; AppData appData;
const char* programName; const char* programName;
const char* serverHost; char* serverHost;
int serverPort; /* if -1, then use file recorded by vncrec */ int serverPort; /* if -1, then use file recorded by vncrec */
rfbBool listenSpecified; rfbBool listenSpecified;
int listenPort, flashPort; int listenPort, flashPort;
@ -112,8 +112,8 @@ typedef struct _rfbClient {
Hextile also assumes it is big enough to hold 16 * 16 * 32 bits. Hextile also assumes it is big enough to hold 16 * 16 * 32 bits.
Tight encoding assumes BUFFER_SIZE is at least 16384 bytes. */ Tight encoding assumes BUFFER_SIZE is at least 16384 bytes. */
#define BUFFER_SIZE (640*480) #define RFB_BUFFER_SIZE (640*480)
char buffer[BUFFER_SIZE]; char buffer[RFB_BUFFER_SIZE];
/* rfbproto.c */ /* rfbproto.c */
@ -126,6 +126,13 @@ typedef struct _rfbClient {
char *serverCutText; char *serverCutText;
rfbBool newServerCutText; rfbBool newServerCutText;
/* sockets.c */
#define RFB_BUF_SIZE 8192
char buf[RFB_BUF_SIZE];
char *bufoutptr;
int buffered;
/* cursor.c */ /* cursor.c */
uint8_t *rcSource, *rcMask; uint8_t *rcSource, *rcMask;
@ -156,7 +163,8 @@ extern void listenForIncomingConnections(rfbClient* viewer);
/* rfbproto.c */ /* rfbproto.c */
extern rfbBool rfbEnableClientLogging; 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 ConnectToRFBServer(rfbClient* client,const char *hostname, int port);
extern rfbBool InitialiseRFBConnection(rfbClient* client); extern rfbBool InitialiseRFBConnection(rfbClient* client);
extern rfbBool SetFormatAndEncodings(rfbClient* client); extern rfbBool SetFormatAndEncodings(rfbClient* client);

Loading…
Cancel
Save