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 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,
@ -78,11 +84,13 @@ void * outputThreadHandler(void * parameters)
while (true)
{
// 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:
// Run through the output queue and send all unsent messages:
while (outputQueue->itemCount != 0)
{
// Wait until the queue unlocks:
@ -105,23 +113,25 @@ void * outputThreadHandler(void * parameters)
messageSend(tlssessions[index], message->content);
}
}
// Otherwise, send it only to the targeted players:
else
{
int targetIndex = 0;
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])
{
targetIndex++;
sentCount++;
messageSend(tlssessions[index], message->content);
}
}
}
// Remove the output message from the queue:
popQueue(outputQueue);
}
@ -133,7 +143,7 @@ void userInputSanatize(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] = '\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';