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.
This commit is contained in:
Barry Kane 2023-02-15 21:22:14 +00:00
parent f411333203
commit c753182827
1 changed files with 25 additions and 14 deletions

View File

@ -17,11 +17,14 @@
int messageSend(gnutls_session_t receivingSession, userMessage * messageToSend) int messageSend(gnutls_session_t receivingSession, userMessage * messageToSend)
{ {
int returnValue = 0; int returnValue = 0;
// Continuously attempt to send the name field until it succeeds or fatally errors:
do do
{ {
returnValue = gnutls_record_send(receivingSession, messageToSend->senderName, returnValue = gnutls_record_send(receivingSession, messageToSend->senderName,
sizeof(((userMessage*)0)->senderName)); sizeof(((userMessage*)0)->senderName));
} while (returnValue == GNUTLS_E_AGAIN || returnValue == GNUTLS_E_INTERRUPTED); } while (returnValue == GNUTLS_E_AGAIN || returnValue == GNUTLS_E_INTERRUPTED);
// Continuously attempt to send the message field until it succeeds or fatally errors:
do do
{ {
returnValue = gnutls_record_send(receivingSession, messageToSend->messageContent, 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 messageReceive(gnutls_session_t receiveFromSession, userMessage * receiveToMessage)
{ {
int returnValue = 0; int returnValue = 0;
// Continuously attempt to receive the name field until it succeeds or fatally errors:
do do
{ {
returnValue = gnutls_record_recv(receiveFromSession, receiveToMessage->senderName, returnValue = gnutls_record_recv(receiveFromSession, receiveToMessage->senderName,
sizeof(((userMessage*)0)->senderName)); sizeof(((userMessage*)0)->senderName));
} while (returnValue == GNUTLS_E_AGAIN || returnValue == GNUTLS_E_INTERRUPTED); } while (returnValue == GNUTLS_E_AGAIN || returnValue == GNUTLS_E_INTERRUPTED);
// Continuously attempt to receive the message field until it succeeds or fatally errors:
do do
{ {
returnValue = gnutls_record_recv(receiveFromSession, receiveToMessage->messageContent, returnValue = gnutls_record_recv(receiveFromSession, receiveToMessage->messageContent,
@ -76,17 +82,19 @@ void * outputThreadHandler(void * parameters)
gnutls_session_t * tlssessions = variables->tlssessions; gnutls_session_t * tlssessions = variables->tlssessions;
playerInfo * connectedPlayers = variables->connectedPlayers; 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); 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: // Wait until the queue unlocks:
while(outputQueue->lock); while (outputQueue->lock);
// Lock the queue: // Lock the queue:
outputQueue->lock = true; outputQueue->lock = true;
@ -98,30 +106,32 @@ void * outputThreadHandler(void * parameters)
outputQueue->lock = false; outputQueue->lock = false;
// If the first target is set to NULL, it's intended for all connected: // 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++) for (int index = 0; index < PLAYERCOUNT; index++)
{ {
messageSend(tlssessions[index], message->content); messageSend(tlssessions[index], message->content);
} }
} }
// Otherwise, send it only to the targeted players: // Otherwise, send it only to the targeted players:
else else
{ {
int targetIndex = 0; int sentCount = 0;
for(int index = 0; index < PLAYERCOUNT; index++) for (int index = 0; index < PLAYERCOUNT; index++)
{ {
if(targetIndex == message->recipientsCount) if (sentCount == message->recipientsCount)
{ {
break; break;
} }
if(&connectedPlayers[index] == message->recipients[targetIndex]) if (&connectedPlayers[index] == message->recipients[targetIndex])
{ {
targetIndex++; sentCount++;
messageSend(tlssessions[index], message->content); messageSend(tlssessions[index], message->content);
} }
} }
} }
// Remove the output message from the queue: // Remove the output message from the queue:
popQueue(outputQueue); popQueue(outputQueue);
} }
@ -131,9 +141,9 @@ void * outputThreadHandler(void * parameters)
// Sanatize user input to ensure it's okay to process: // Sanatize user input to ensure it's okay to process:
void userInputSanatize(char * inputString, int length) 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])) if(!isprint(inputString[index]))
{ {
inputString[index] = '\n'; inputString[index] = '\n';
@ -141,6 +151,7 @@ void userInputSanatize(char * inputString, int length)
break; break;
} }
} }
// Make sure it's null-terminated: // Make sure it's null-terminated:
inputString[length - 1] = '\0'; inputString[length - 1] = '\0';
} }
@ -150,7 +161,7 @@ void userNameSanatize(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])) if(!isprint(inputString[index]))
{ {
inputString[index] = '\0'; inputString[index] = '\0';