diff --git a/Listener.xcodeproj/xcuserdata/jrand.xcuserdatad/xcschemes/xcschememanagement.plist b/Listener.xcodeproj/xcuserdata/jrand.xcuserdatad/xcschemes/xcschememanagement.plist index 3a48605..5248539 100644 --- a/Listener.xcodeproj/xcuserdata/jrand.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Listener.xcodeproj/xcuserdata/jrand.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,17 +7,17 @@ Archive.xcscheme_^#shared#^_ orderHint - 3 + 1 Binary.xcscheme_^#shared#^_ orderHint - 2 + 3 DiskImage.xcscheme_^#shared#^_ orderHint - 4 + 2 Listener.xcscheme_^#shared#^_ @@ -27,7 +27,7 @@ doNotBuild.xcscheme_^#shared#^_ orderHint - 1 + 4 diff --git a/Listener/main.c b/Listener/main.c index 45bd2f6..782460f 100644 --- a/Listener/main.c +++ b/Listener/main.c @@ -141,44 +141,41 @@ static char * stateMessages[NUM_LISTEN_STATES] = { // Implementation -void NDAClose(void) +void delayToNextTick() { - if ((globals != NULL) && - (globals->ndaActive)) { - CloseWindow(globals->winPtr); - globals->winPtr = NULL; - globals->ndaActive = FALSE; - } - - ResourceShutDown(); + LongWord oldTickCounter = GetTick(); + while (oldTickCounter == GetTick()) + ; } -void freeGlobals(void) +void closeConnection(void) { - tTextList * textList = globals->textListHead; + static srBuff srBuffer; + int counter; - if (globals->textTransfer != NULL) - free(globals->textTransfer); - - while (textList != NULL) { - tTextList * prev = textList; - textList = textList->header.next; - free(prev); - } - - free(globals); - globals = NULL; -} - - -void teardownNetwork(void) -{ if (globals->hasConnIpid) { - TCPIPAbortTCP(globals->connIpid); + TCPIPCloseTCP(globals->connIpid); + for (counter = 0; counter < 20; counter++) + { + TCPIPPoll(); + delayToNextTick(); + TCPIPStatusTCP(globals->connIpid, &srBuffer); + if ((srBuffer.srState == TCPSCLOSED) || + (srBuffer.srState == TCPSTIMEWAIT)) + break; + } + if ((srBuffer.srState != TCPSCLOSED) & + (srBuffer.srState != TCPSTIMEWAIT)) + TCPIPAbortTCP(globals->connIpid); TCPIPLogout(globals->connIpid); } globals->hasConnIpid = FALSE; +} + +void teardownNetwork(void) +{ + closeConnection(); if (globals->hasListenIpid) { TCPIPCloseTCP(globals->listenIpid); @@ -196,6 +193,45 @@ void teardownNetwork(void) } +void freeText(void) +{ + tTextList * textList = globals->textListHead; + + if (globals->textTransfer != NULL) + free(globals->textTransfer); + + while (textList != NULL) { + tTextList * prev = textList; + textList = textList->header.next; + free(prev); + } +} + +void NDAClose(void) +{ + if ((globals != NULL) && + (globals->ndaActive)) { + CloseWindow(globals->winPtr); + globals->winPtr = NULL; + globals->ndaActive = FALSE; + } + + teardownNetwork(); + ResourceShutDown(); + freeText(); + memset(globals, 0, sizeof(*globals)); +} + + +void freeGlobals(void) +{ + freeText(); + + free(globals); + globals = NULL; +} + + void NDAInit(int code) { /* When code is 1, this is tool startup, otherwise tool @@ -444,9 +480,7 @@ void handleAwaitingMsgHeaderState(void) return; } if (srBuffer.srState != TCPSESTABLISHED) { - TCPIPAbortTCP(globals->connIpid); - TCPIPLogout(globals->connIpid); - globals->hasConnIpid = FALSE; + closeConnection(); newState(LISTEN_STATE_AWAITING_CONNECTION); return; } @@ -507,9 +541,7 @@ void handleAwaitingTextState(void) return; } if (srBuffer.srState != TCPSESTABLISHED) { - TCPIPAbortTCP(globals->connIpid); - TCPIPLogout(globals->connIpid); - globals->hasConnIpid = FALSE; + closeConnection(); newState(LISTEN_STATE_AWAITING_CONNECTION); return; } @@ -556,8 +588,13 @@ void runStateMachine(void) void sendKey(void) { - tTextList * textList = globals->textListHead; + EventRecord eventRecord; + // If there is still a key down waiting to be processed, then don't send another one. + if (EventAvail(keyDownMask, &eventRecord)) + return; + + tTextList * textList = globals->textListHead; PostEvent(keyDownEvt, 0x00C00000 | textList->text[textList->header.position]); textList->header.position++; if (textList->header.position < textList->header.size)