From c7531828274f5a725a31238d7d50ce6a83e8f96f Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Wed, 15 Feb 2023 21:22:14 +0000 Subject: [PATCH] Cleaned up inputoutput.c. - Added additional comments in sections functions that were unclear. - Renamed targetIndex to sentCount, in order to clarify the usage of the variable. - Linted according to the current SilverMUD style guide. --- src/inputoutput.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/inputoutput.c b/src/inputoutput.c index 4bd4b10..b5cb6c3 100644 --- a/src/inputoutput.c +++ b/src/inputoutput.c @@ -17,11 +17,14 @@ int messageSend(gnutls_session_t receivingSession, userMessage * messageToSend) { int returnValue = 0; + // Continuously attempt to send the name field until it succeeds or fatally errors: do { returnValue = gnutls_record_send(receivingSession, messageToSend->senderName, sizeof(((userMessage*)0)->senderName)); } while (returnValue == GNUTLS_E_AGAIN || returnValue == GNUTLS_E_INTERRUPTED); + + // Continuously attempt to send the message field until it succeeds or fatally errors: do { returnValue = gnutls_record_send(receivingSession, messageToSend->messageContent, @@ -35,11 +38,14 @@ int messageSend(gnutls_session_t receivingSession, userMessage * messageToSend) int messageReceive(gnutls_session_t receiveFromSession, userMessage * receiveToMessage) { int returnValue = 0; + // Continuously attempt to receive the name field until it succeeds or fatally errors: do { returnValue = gnutls_record_recv(receiveFromSession, receiveToMessage->senderName, sizeof(((userMessage*)0)->senderName)); } while (returnValue == GNUTLS_E_AGAIN || returnValue == GNUTLS_E_INTERRUPTED); + + // Continuously attempt to receive the message field until it succeeds or fatally errors: do { returnValue = gnutls_record_recv(receiveFromSession, receiveToMessage->messageContent, @@ -76,17 +82,19 @@ void * outputThreadHandler(void * parameters) gnutls_session_t * tlssessions = variables->tlssessions; playerInfo * connectedPlayers = variables->connectedPlayers; - while(true) + while (true) { - if(outputQueue->itemCount == 0) + // If there's nothing to do, put the thread to sleep: + if (outputQueue->itemCount == 0) { pthread_cond_wait(&outputQueue->condition, &outputQueue->mutex); } - // Run through the output queue and send all unused messages: - while(outputQueue->itemCount != 0) + + // Run through the output queue and send all unsent messages: + while (outputQueue->itemCount != 0) { // Wait until the queue unlocks: - while(outputQueue->lock); + while (outputQueue->lock); // Lock the queue: outputQueue->lock = true; @@ -98,30 +106,32 @@ void * outputThreadHandler(void * parameters) outputQueue->lock = false; // If the first target is set to NULL, it's intended for all connected: - if(message->recipientsCount == 0) + if (message->recipientsCount == 0) { for (int index = 0; index < PLAYERCOUNT; index++) { messageSend(tlssessions[index], message->content); } } + // Otherwise, send it only to the targeted players: else { - int targetIndex = 0; - for(int index = 0; index < PLAYERCOUNT; index++) + int sentCount = 0; + for (int index = 0; index < PLAYERCOUNT; index++) { - if(targetIndex == message->recipientsCount) + if (sentCount == message->recipientsCount) { break; } - if(&connectedPlayers[index] == message->recipients[targetIndex]) + if (&connectedPlayers[index] == message->recipients[targetIndex]) { - targetIndex++; + sentCount++; messageSend(tlssessions[index], message->content); } } } + // Remove the output message from the queue: popQueue(outputQueue); } @@ -131,9 +141,9 @@ void * outputThreadHandler(void * parameters) // Sanatize user input to ensure it's okay to process: void userInputSanatize(char * inputString, int length) { - for(int index = 0; index <= length; index++) + for (int index = 0; index <= length; index++) { - // If it's not a printable character, it has no buisness being here: + // If it's not a printable character, it has no business being here: if(!isprint(inputString[index])) { inputString[index] = '\n'; @@ -141,6 +151,7 @@ void userInputSanatize(char * inputString, int length) break; } } + // Make sure it's null-terminated: inputString[length - 1] = '\0'; } @@ -150,7 +161,7 @@ void userNameSanatize(char * inputString, int length) { for(int index = 0; index <= length; index++) { - // If it's not a printable character, it has no buisness being here: + // If it's not a printable character, it has no business being here: if(!isprint(inputString[index])) { inputString[index] = '\0';