Small refactoring to reduce boilerplate code

DoReadTCP automatically disposes the old read buffer handle before doing the read, and then locks the new read buffer handle before returning. This removes the need for most explicit locking/unlocking of the handle. A macro is provided to dispose of the handle earlier (useful in a few cases where it may be big, and another read isn't immediately performed).

Also, set displayInProgress to FALSE in NextRect, rather than in code for each encoding.
This commit is contained in:
Stephen Heumann 2016-05-28 21:59:35 -05:00
parent 3a175f9b02
commit e1288f7e1b
9 changed files with 20 additions and 60 deletions

View File

@ -48,9 +48,7 @@ void DoServerCutText (void) {
DoClose(vncWindow); DoClose(vncWindow);
return; return;
} }
HLock(readBufferHndl);
textLen = SwapBytes4((unsigned long) **readBufferHndl); textLen = SwapBytes4((unsigned long) **readBufferHndl);
HUnlock(readBufferHndl);
if (! DoWaitingReadTCP(textLen)) { if (! DoWaitingReadTCP(textLen)) {
DoClose(vncWindow); DoClose(vncWindow);
@ -58,7 +56,6 @@ void DoServerCutText (void) {
}; };
if (allowClipboardTransfers) { if (allowClipboardTransfers) {
ZeroScrap(); ZeroScrap();
HLock(readBufferHndl);
/* Convert lf->cr; Use pointer arithmetic so we can go over 64k */ /* Convert lf->cr; Use pointer arithmetic so we can go over 64k */
for (i = 0; i < textLen; i++) for (i = 0; i < textLen; i++)
@ -68,7 +65,7 @@ void DoServerCutText (void) {
/* Below function call requires <scrap.h> to be fixed */ /* Below function call requires <scrap.h> to be fixed */
PutScrap(textLen, textScrap, (Pointer) *readBufferHndl); PutScrap(textLen, textScrap, (Pointer) *readBufferHndl);
/* Potential errors (e.g. out of memory) ignored */ /* Potential errors (e.g. out of memory) ignored */
HUnlock(readBufferHndl); DoneWithReadBuffer();
} }
} }

View File

@ -48,11 +48,9 @@ void DoCopyRect (void) {
contentOrigin = GetContentOrigin(vncWindow); contentOrigin = GetContentOrigin(vncWindow);
HLock(readBufferHndl);
dataPtr = (unsigned int *) ((char *) (*readBufferHndl)); dataPtr = (unsigned int *) ((char *) (*readBufferHndl));
srcRect.h1 = SwapBytes2(dataPtr[0]) - contentOriginPtr->h; srcRect.h1 = SwapBytes2(dataPtr[0]) - contentOriginPtr->h;
srcRect.v1 = SwapBytes2(dataPtr[1]) - contentOriginPtr->v; srcRect.v1 = SwapBytes2(dataPtr[1]) - contentOriginPtr->v;
HUnlock(readBufferHndl);
srcRect.h2 = srcRect.h1 + rectWidth; srcRect.h2 = srcRect.h1 + rectWidth;
srcRect.v2 = srcRect.v1 + rectHeight; srcRect.v2 = srcRect.v1 + rectHeight;
@ -72,8 +70,6 @@ void DoCopyRect (void) {
rectX - contentOriginPtr->h, rectY - contentOriginPtr->v, modeCopy); rectX - contentOriginPtr->h, rectY - contentOriginPtr->v, modeCopy);
done: done:
displayInProgress = FALSE;
NextRect(); /* Prepare for next rect */ NextRect(); /* Prepare for next rect */
} }

View File

@ -81,8 +81,6 @@ void DoDesktopSize (void) {
SetDataSize(fbWidth, fbHeight, vncWindow); SetDataSize(fbWidth, fbHeight, vncWindow);
DrawControls(vncWindow); DrawControls(vncWindow);
displayInProgress = FALSE;
NextRect(); /* Prepare for next rect */ NextRect(); /* Prepare for next rect */
} }

View File

@ -57,7 +57,6 @@ static void HexNextTile (void) {
if (hexXTileNum == hexXTiles) { if (hexXTileNum == hexXTiles) {
hexYTileNum++; hexYTileNum++;
if (hexYTileNum == hexYTiles) { /* Done with this Hextile rect */ if (hexYTileNum == hexYTiles) { /* Done with this Hextile rect */
displayInProgress = FALSE;
NextRect(); NextRect();
return; return;
} }
@ -172,7 +171,7 @@ static void HexRawDraw (Point *contentOriginPtr, int rectWidth, int rectHeight)
/* The macros below are used in HexDispatch() */ /* The macros below are used in HexDispatch() */
#define HexDispatch_NextTile() do { \ #define HexDispatch_NextTile() do { \
HexNextTile(); \ HexNextTile(); \
HUnlock(readBufferHndl); \ DoneWithReadBuffer(); \
/* Set up for next time */ \ /* Set up for next time */ \
status = hexWaitingForSubencoding; \ status = hexWaitingForSubencoding; \
bytesNeeded = 1; \ bytesNeeded = 1; \
@ -211,7 +210,6 @@ void HexDispatch (void) {
/* If we don't have the next bit of needed data yet, return. */ /* If we don't have the next bit of needed data yet, return. */
while (DoReadTCP(bytesNeeded)) { while (DoReadTCP(bytesNeeded)) {
HLock(readBufferHndl);
dataPtr = *(unsigned char **) readBufferHndl; dataPtr = *(unsigned char **) readBufferHndl;
/* If we're here, readBufferHndl contains bytesNeeded bytes of data. */ /* If we're here, readBufferHndl contains bytesNeeded bytes of data. */
switch (status) { switch (status) {
@ -280,7 +278,6 @@ void HexDispatch (void) {
HexDispatch_NextTile(); HexDispatch_NextTile();
} }
} }
HUnlock(readBufferHndl);
} }
} }

View File

@ -162,8 +162,6 @@ void DoCursor (void) {
if (!DoReadTCP((unsigned long)rectWidth * rectHeight + bitmaskLineBytes * rectHeight)) if (!DoReadTCP((unsigned long)rectWidth * rectHeight + bitmaskLineBytes * rectHeight))
return; /* Try again later */ return; /* Try again later */
HLock(readBufferHndl);
cursorPixels = (unsigned char *)(*readBufferHndl); cursorPixels = (unsigned char *)(*readBufferHndl);
bitmask = (unsigned char *)(*readBufferHndl) + (unsigned long)rectWidth * rectHeight; bitmask = (unsigned char *)(*readBufferHndl) + (unsigned long)rectWidth * rectHeight;
@ -316,8 +314,6 @@ void DoCursor (void) {
#endif #endif
done: done:
HUnlock(readBufferHndl);
free(oldCursor); free(oldCursor);
displayInProgress = FALSE;
NextRect(); /* Prepare for next rect */ NextRect(); /* Prepare for next rect */
} }

8
raw.cc
View File

@ -57,10 +57,6 @@ unsigned char * rawDecode320(unsigned startOffset, unsigned endOffset,
* because the rectangle is not visible. * because the rectangle is not visible.
*/ */
static void StopRawDrawing (void) { static void StopRawDrawing (void) {
HUnlock(readBufferHndl);
displayInProgress = FALSE;
NextRect(); /* Prepare for next rect */ NextRect(); /* Prepare for next rect */
} }
@ -237,7 +233,6 @@ static void RawDrawLine (void) {
srcRect.v1 = 0; srcRect.v1 = 0;
srcRect.v2 = 1; srcRect.v2 = 1;
HLock(readBufferHndl);
dataPtr = (unsigned char *) *readBufferHndl; dataPtr = (unsigned char *) *readBufferHndl;
SetPort(vncWindow); /* Drawing in VNC window */ SetPort(vncWindow); /* Drawing in VNC window */
@ -266,7 +261,7 @@ static void RawDrawLine (void) {
} }
} }
HUnlock(readBufferHndl); DoneWithReadBuffer();
contentOrigin = GetContentOrigin(vncWindow); contentOrigin = GetContentOrigin(vncWindow);
PPToPort(&srcLocInfo, &srcRect, rectX - contentOriginPtr->h, PPToPort(&srcLocInfo, &srcRect, rectX - contentOriginPtr->h,
rectY - contentOriginPtr->v, modeCopy); rectY - contentOriginPtr->v, modeCopy);
@ -359,5 +354,4 @@ void DoRawRect (void) {
displayInProgress = TRUE; displayInProgress = TRUE;
drawingLine = 0; /* Drawing first line of rect */ drawingLine = 0; /* Drawing first line of rect */
checkBounds = TRUE; /* Flag to check bounds when drawing 1st line */ checkBounds = TRUE; /* Flag to check bounds when drawing 1st line */
HLock(readBufferHndl); /* Lock handle just once for efficiency */
} }

View File

@ -226,7 +226,6 @@ static void DoFBUpdate (void) {
//printf("Closing in DoFBUpdate\n"); //printf("Closing in DoFBUpdate\n");
return; return;
} }
HLock(readBufferHndl);
dataPtr = (unsigned int *) (((char *) (*readBufferHndl)) + 1); dataPtr = (unsigned int *) (((char *) (*readBufferHndl)) + 1);
numRects = SwapBytes2(dataPtr[0]); /* Get data */ numRects = SwapBytes2(dataPtr[0]); /* Get data */
rectX = SwapBytes2(dataPtr[1]); rectX = SwapBytes2(dataPtr[1]);
@ -234,7 +233,6 @@ static void DoFBUpdate (void) {
rectWidth = SwapBytes2(dataPtr[3]); rectWidth = SwapBytes2(dataPtr[3]);
rectHeight = SwapBytes2(dataPtr[4]); rectHeight = SwapBytes2(dataPtr[4]);
rectEncoding = SwapBytes4(*(unsigned long *)(dataPtr + 5)); rectEncoding = SwapBytes4(*(unsigned long *)(dataPtr + 5));
HUnlock(readBufferHndl);
} }
/* The server should never send a color map, since we don't use a mapped /* The server should never send a color map, since we don't use a mapped
@ -249,9 +247,7 @@ static void DoSetColourMapEntries (void) {
DoWaitingReadTCP(3); DoWaitingReadTCP(3);
DoWaitingReadTCP(2); DoWaitingReadTCP(2);
HLock(readBufferHndl);
numColors = SwapBytes2((unsigned int) **readBufferHndl); numColors = SwapBytes2((unsigned int) **readBufferHndl);
HUnlock(readBufferHndl);
for (; numColors > 0; numColors--) { for (; numColors > 0; numColors--) {
DoWaitingReadTCP(6); DoWaitingReadTCP(6);
} }
@ -265,6 +261,8 @@ static void DoSetColourMapEntries (void) {
void NextRect (void) { void NextRect (void) {
unsigned int *dataPtr; unsigned int *dataPtr;
displayInProgress = FALSE;
numRects--; numRects--;
if (numRects) { /* Process next rectangle */ if (numRects) { /* Process next rectangle */
if (!DoWaitingReadTCP(12)) { if (!DoWaitingReadTCP(12)) {
@ -272,20 +270,20 @@ void NextRect (void) {
DoClose(vncWindow); DoClose(vncWindow);
return; return;
} }
HLock(readBufferHndl);
dataPtr = (unsigned int *) ((char *) (*readBufferHndl)); dataPtr = (unsigned int *) ((char *) (*readBufferHndl));
rectX = SwapBytes2(dataPtr[0]); rectX = SwapBytes2(dataPtr[0]);
rectY = SwapBytes2(dataPtr[1]); rectY = SwapBytes2(dataPtr[1]);
rectWidth = SwapBytes2(dataPtr[2]); rectWidth = SwapBytes2(dataPtr[2]);
rectHeight = SwapBytes2(dataPtr[3]); rectHeight = SwapBytes2(dataPtr[3]);
rectEncoding = SwapBytes4(*(unsigned long *)(dataPtr + 4)); rectEncoding = SwapBytes4(*(unsigned long *)(dataPtr + 4));
HUnlock(readBufferHndl);
//printf("New Rect: X = %u, Y = %u, Width = %u, Height = %u\n", rectX, rectY, rectWidth, rectHeight); //printf("New Rect: X = %u, Y = %u, Width = %u, Height = %u\n", rectX, rectY, rectWidth, rectHeight);
} }
else { /* No more rectangles from last update */ else { /* No more rectangles from last update */
unsigned long contentOrigin; unsigned long contentOrigin;
Point * contentOriginPtr = (void *) &contentOrigin; Point * contentOriginPtr = (void *) &contentOrigin;
DoneWithReadBuffer();
contentOrigin = GetContentOrigin(vncWindow); contentOrigin = GetContentOrigin(vncWindow);
SendFBUpdateRequest(TRUE, contentOriginPtr->h, contentOriginPtr->v, SendFBUpdateRequest(TRUE, contentOriginPtr->h, contentOriginPtr->v,
winWidth, winHeight); winWidth, winHeight);
@ -339,9 +337,7 @@ void ConnectedEventLoop (void) {
} }
} }
else if (DoReadTCP(1)) { /* Read message type byte */ else if (DoReadTCP(1)) { /* Read message type byte */
HLock(readBufferHndl);
messageType = ((unsigned char) **readBufferHndl); messageType = ((unsigned char) **readBufferHndl);
HUnlock(readBufferHndl);
switch (messageType) { switch (messageType) {
case FBUpdate: DoFBUpdate(); case FBUpdate: DoFBUpdate();
break; break;

View File

@ -108,7 +108,6 @@ void DoConnect (void) {
} }
if (DoVNCHandshaking() == FALSE) { if (DoVNCHandshaking() == FALSE) {
SetHandleSize(1,readBufferHndl);
CloseConnectStatusWindow(); CloseConnectStatusWindow();
InitCursor(); InitCursor();
SysBeep(); SysBeep();
@ -119,7 +118,6 @@ void DoConnect (void) {
return; return;
} }
if (FinishVNCHandshaking() == FALSE) { if (FinishVNCHandshaking() == FALSE) {
SetHandleSize(1,readBufferHndl);
CloseConnectStatusWindow(); CloseConnectStatusWindow();
InitCursor(); InitCursor();
AlertWindow(awResource, NULL, badOptionNegotiationError); AlertWindow(awResource, NULL, badOptionNegotiationError);
@ -320,6 +318,7 @@ static BOOLEAN ReadFixup (unsigned long requested, unsigned long returned) {
SetHandleSize(requested, readBufferHndl); SetHandleSize(requested, readBufferHndl);
if (toolerror()) if (toolerror())
return FALSE; return FALSE;
HLock(readBufferHndl);
do { do {
TCPIPPoll(); TCPIPPoll();
@ -350,6 +349,7 @@ BOOLEAN DoReadTCP (unsigned long dataLength) {
static srBuff theSRBuff; static srBuff theSRBuff;
static rrBuff theRRBuff; static rrBuff theRRBuff;
DoneWithReadBuffer();
TCPIPPoll(); TCPIPPoll();
if ((tcperr = TCPIPStatusTCP(hostIpid, &theSRBuff)) != tcperrOK) if ((tcperr = TCPIPStatusTCP(hostIpid, &theSRBuff)) != tcperrOK)
@ -360,7 +360,6 @@ BOOLEAN DoReadTCP (unsigned long dataLength) {
if (theSRBuff.srRcvQueued < dataLength) if (theSRBuff.srRcvQueued < dataLength)
return FALSE; return FALSE;
DisposeHandle(readBufferHndl);
if ((tcperr = TCPIPReadTCP(hostIpid, buffTypeNewHandle, NULL, if ((tcperr = TCPIPReadTCP(hostIpid, buffTypeNewHandle, NULL,
dataLength, &theRRBuff)) != tcperrOK) dataLength, &theRRBuff)) != tcperrOK)
return FALSE; return FALSE;
@ -371,6 +370,7 @@ BOOLEAN DoReadTCP (unsigned long dataLength) {
if (theRRBuff.rrBuffCount != dataLength) if (theRRBuff.rrBuffCount != dataLength)
return ReadFixup(dataLength, theRRBuff.rrBuffCount); return ReadFixup(dataLength, theRRBuff.rrBuffCount);
HLock(readBufferHndl);
return TRUE; return TRUE;
} }
@ -393,18 +393,15 @@ static BOOLEAN DoVNCHandshaking (void) {
strcpy(versionString, ""); strcpy(versionString, "");
if (! DoWaitingReadTCP(12)) if (! DoWaitingReadTCP(12))
return FALSE; return FALSE;
HLock(readBufferHndl);
if ( ! ((strncmp((char *)*readBufferHndl, "RFB ", 4) == 0) && if ( ! ((strncmp((char *)*readBufferHndl, "RFB ", 4) == 0) &&
(strncmp((char *)*readBufferHndl+4, RFBMAJORVERSIONSTR, 3) >= 0) && (strncmp((char *)*readBufferHndl+4, RFBMAJORVERSIONSTR, 3) >= 0) &&
(strncmp((char *)*readBufferHndl+7, ".", 1) == 0) && (strncmp((char *)*readBufferHndl+7, ".", 1) == 0) &&
(strncmp((char *)*readBufferHndl+11, "\n", 1) == 0))) { (strncmp((char *)*readBufferHndl+11, "\n", 1) == 0))) {
HUnlock(readBufferHndl);
InitCursor(); InitCursor();
AlertWindow(awResource, NULL, badRFBVersionAlert); AlertWindow(awResource, NULL, badRFBVersionAlert);
alerted = TRUE; alerted = TRUE;
return FALSE; return FALSE;
} }
HUnlock(readBufferHndl);
strcpy(versionString, RFBVERSIONSTR); strcpy(versionString, RFBVERSIONSTR);
if (TCPIPWriteTCP(hostIpid, versionString, 12, TRUE, FALSE)) { if (TCPIPWriteTCP(hostIpid, versionString, 12, TRUE, FALSE)) {
@ -417,20 +414,17 @@ static BOOLEAN DoVNCHandshaking (void) {
if (! DoWaitingReadTCP(4)) { /* Read authentication type */ if (! DoWaitingReadTCP(4)) { /* Read authentication type */
return FALSE; return FALSE;
} }
HLock(readBufferHndl);
switch ((unsigned long) (**readBufferHndl)) { switch ((unsigned long) (**readBufferHndl)) {
case vncConnectionFailed: HUnlock(readBufferHndl); case vncConnectionFailed: if (! DoWaitingReadTCP(4))
if (! DoWaitingReadTCP(4))
return FALSE; return FALSE;
if (toolerror()) if (toolerror())
return FALSE; return FALSE;
HLock(readBufferHndl);
reasonLength = SwapBytes4(**readBufferHndl); reasonLength = SwapBytes4(**readBufferHndl);
HUnlock(readBufferHndl);
if (! DoWaitingReadTCP(reasonLength)) if (! DoWaitingReadTCP(reasonLength))
return FALSE; return FALSE;
if (toolerror()) if (toolerror())
return FALSE; return FALSE;
HUnlock(readBufferHndl);
SetHandleSize( SetHandleSize(
GetHandleSize(readBufferHndl)+1, GetHandleSize(readBufferHndl)+1,
readBufferHndl); readBufferHndl);
@ -443,22 +437,18 @@ static BOOLEAN DoVNCHandshaking (void) {
(Pointer) readBufferHndl, (Pointer) readBufferHndl,
connectionFailedAlert); connectionFailedAlert);
alerted = TRUE; alerted = TRUE;
HUnlock(readBufferHndl);
} }
return FALSE; return FALSE;
case vncNoAuthentication: break; case vncNoAuthentication: break;
case vncVNCAuthentication: if (DoDES()) case vncVNCAuthentication: if (DoDES())
break; break;
HUnlock(readBufferHndl);
return FALSE; return FALSE;
default: HUnlock(readBufferHndl); default: AlertWindow(awResource, NULL,
AlertWindow(awResource, NULL,
badAuthTypeAlert); badAuthTypeAlert);
alerted = TRUE; alerted = TRUE;
return FALSE; return FALSE;
} }
HUnlock(readBufferHndl);
return TRUE; return TRUE;
#undef connectionFailedAlert #undef connectionFailedAlert
#undef badRFBVersionAlert #undef badRFBVersionAlert
@ -547,10 +537,8 @@ static BOOLEAN DoDES (void) {
DESAddParity(theKey, &vncPassword[1]); DESAddParity(theKey, &vncPassword[1]);
HLock(readBufferHndl);
DESCipher(theResponse, theKey, *(char **)readBufferHndl, modeEncrypt); DESCipher(theResponse, theKey, *(char **)readBufferHndl, modeEncrypt);
DESCipher(&theResponse[8], theKey, *(char **)readBufferHndl+8, modeEncrypt); DESCipher(&theResponse[8], theKey, *(char **)readBufferHndl+8, modeEncrypt);
HUnlock(readBufferHndl);
if (TCPIPWriteTCP(hostIpid, theResponse, sizeof(theResponse), TRUE, FALSE)) if (TCPIPWriteTCP(hostIpid, theResponse, sizeof(theResponse), TRUE, FALSE))
{ {
@ -566,7 +554,6 @@ static BOOLEAN DoDES (void) {
goto UnloadCrypto; goto UnloadCrypto;
} }
HLock(readBufferHndl);
if ((**readBufferHndl) == statusOK) { if ((**readBufferHndl) == statusOK) {
success = TRUE; success = TRUE;
goto UnloadCrypto; goto UnloadCrypto;
@ -590,8 +577,6 @@ static BOOLEAN DoDES (void) {
UnloadCrypto: UnloadCrypto:
HUnlock(readBufferHndl);
if (startedCrypto) { if (startedCrypto) {
CryptoShutDown(); /* Shut down Crypto tool set */ CryptoShutDown(); /* Shut down Crypto tool set */
DisposeHandle(dpSpace); DisposeHandle(dpSpace);
@ -678,14 +663,10 @@ static BOOLEAN FinishVNCHandshaking (void) {
/* ServerInitialisation */ /* ServerInitialisation */
if (! DoWaitingReadTCP(2)) if (! DoWaitingReadTCP(2))
return FALSE; return FALSE;
HLock(readBufferHndl);
fbWidth = SwapBytes2(**(unsigned **)readBufferHndl); fbWidth = SwapBytes2(**(unsigned **)readBufferHndl);
HUnlock(readBufferHndl);
if (! DoWaitingReadTCP(2)) if (! DoWaitingReadTCP(2))
return FALSE; return FALSE;
HLock(readBufferHndl);
fbHeight = SwapBytes2(**(unsigned **)readBufferHndl); fbHeight = SwapBytes2(**(unsigned **)readBufferHndl);
HUnlock(readBufferHndl);
if ((fbWidth > 16384) || (fbHeight > 16384)) { if ((fbWidth > 16384) || (fbHeight > 16384)) {
AlertWindow(awResource, NULL, screenTooBigError); AlertWindow(awResource, NULL, screenTooBigError);
@ -697,9 +678,7 @@ static BOOLEAN FinishVNCHandshaking (void) {
return FALSE; return FALSE;
if (! DoWaitingReadTCP(4)) if (! DoWaitingReadTCP(4))
return FALSE; return FALSE;
HLock(readBufferHndl);
serverNameLen = SwapBytes4(**(unsigned long **)readBufferHndl); serverNameLen = SwapBytes4(**(unsigned long **)readBufferHndl);
HUnlock(readBufferHndl);
if (! DoWaitingReadTCP(serverNameLen)) if (! DoWaitingReadTCP(serverNameLen))
return FALSE; return FALSE;

View File

@ -23,6 +23,13 @@ extern unsigned int hostIpid;
extern void DisplayConnectStatus(char *, BOOLEAN); extern void DisplayConnectStatus(char *, BOOLEAN);
extern void DoConnect (void); extern void DoConnect (void);
#define DoneWithReadBuffer() do \
if (readBufferHndl) { \
DisposeHandle(readBufferHndl); \
readBufferHndl = NULL; \
} while (0) \
extern BOOLEAN DoReadTCP (unsigned long); extern BOOLEAN DoReadTCP (unsigned long);
extern BOOLEAN DoWaitingReadTCP(unsigned long); extern BOOLEAN DoWaitingReadTCP(unsigned long);
extern void CloseTCPConnection (void); extern void CloseTCPConnection (void);