Squashed current warnings.

- Fixed all current warnings.
- Added the ability to set the text delay as a command line option in the server.
This commit is contained in:
Barry Kane 2022-10-31 01:55:44 +00:00
parent ca8ba5e410
commit c68e66e7bc
6 changed files with 74 additions and 52 deletions

View File

@ -16,11 +16,11 @@ playerArea * createArea(char * nameString, char * descriptionString)
// Copy the strings into the newly created area:
strncpy(createdArea->areaName, nameString, 32 - 1);
strncpy(createdArea->areaDescription, descriptionString, MAX - 35);
strncpy(createdArea->areaDescription, descriptionString, MAX - 36);
// Properly null-terminate the strings:
createdArea->areaName[31] = '\0';
createdArea->areaDescription[MAX] = '\0';
createdArea->areaDescription[MAX - 36] = '\0';
// Ensure that all the paths are set to NULL:
for(int index = 0; index < 16; index++)

View File

@ -157,12 +157,12 @@ int main(int argc, char ** argv)
{
case 'i':
{
strncpy(ipAddress, optarg, 32);
memcpy(ipAddress, optarg, 32);
break;
}
case 'c':
{
strncpy(chatLogPath, optarg, PATH_MAX + 1);
memcpy(chatLogPath, optarg, PATH_MAX + 1);
chatLog = fopen(chatLogPath, "a+");
if (chatLog == NULL)
{
@ -176,7 +176,7 @@ int main(int argc, char ** argv)
}
case 'g':
{
strncpy(gameLogPath, optarg, PATH_MAX + 1);
memcpy(gameLogPath, optarg, PATH_MAX + 1);
gameLog = fopen(gameLogPath, "a+");
if (gameLog == NULL)
{

View File

@ -112,13 +112,16 @@ int queueMessagedCommand(commandQueue * queue, inputMessage * messageToQueue)
strtok(messageToQueue->content->messageContent, " ");
// Copy the command and arguments to the new commandEvent:
strncpy(newCommand->command, &messageToQueue->content->messageContent[1], 16);
strncpy(newCommand->arguments, &messageToQueue->content->messageContent[strlen(newCommand->command) + 2],
memcpy(newCommand->command, &messageToQueue->content->messageContent[1], 16);
memcpy(newCommand->arguments, &messageToQueue->content->messageContent[strlen(newCommand->command) + 2],
MAX - (strlen(newCommand->command) + 2));
// Ensure the arguments are safe to parse, without adding newlines:
userNameSanatize(newCommand->command, 16);
newCommand->command[15] = '\0';
userNameSanatize(newCommand->arguments, MAX);
newCommand->arguments[MAX - 1] = '\0';
// Lowercase the command for easier comparison:
for (char * character = newCommand->command; *character; ++character)
@ -336,7 +339,7 @@ int evaluateNextCommand(gameLogicParameters * parameters, commandQueue * queue)
char requestedPath[32];
if(strlen(currentCommand->arguments) > 0 && currentCommand->caller->currentArea != getAreaFromList(parameters->areaList, 0))
{
strncpy(requestedPath, currentCommand->arguments, 32);
memcpy(requestedPath, currentCommand->arguments, 32);
userNameSanatize(requestedPath, 32);
requestedPath[31] = '\0';
if(movePlayerToArea(currentCommand->caller, requestedPath) == 0)

View File

@ -265,17 +265,17 @@ int dequeueInputMessage(inputMessageQueue * queue)
int queueInputMessage(inputMessageQueue * queue, userMessage messageToQueue, playerInfo * sendingPlayer)
{
// Copy the message into a new input message:
inputMessage * inputMessage = malloc(sizeof(inputMessage));
inputMessage * input = malloc(sizeof(inputMessage));
// Allocate the internal userMessage to store the message:
inputMessage->content = malloc(sizeof(userMessage));
input->content = malloc(sizeof(userMessage));
// Copy the userMessage to the internal userMessage:
strncpy(inputMessage->content->senderName, messageToQueue.senderName, 32);
strncpy(inputMessage->content->messageContent, messageToQueue.messageContent, MAX);
strncpy(input->content->senderName, messageToQueue.senderName, 32);
strncpy(input->content->messageContent, messageToQueue.messageContent, MAX);
// We have no targets, NULL sends to all players in an area:
inputMessage->sender = sendingPlayer;
input->sender = sendingPlayer;
// Wait for the queue to unlock:
while (queue->lock);
@ -295,8 +295,8 @@ int queueInputMessage(inputMessageQueue * queue, userMessage messageToQueue, pla
// If the queue is empty, set the first message as both the front and back of the queue:
if(queue->front == NULL)
{
queue->front = inputMessage;
queue->back = inputMessage;
queue->front = input;
queue->back = input;
queue->currentLength++;
// Unlock the queue:
@ -306,8 +306,8 @@ int queueInputMessage(inputMessageQueue * queue, userMessage messageToQueue, pla
}
else
{
queue->back->next = inputMessage;
queue->back = inputMessage;
queue->back->next = input;
queue->back = input;
queue->currentLength++;
// Unlock the queue:

View File

@ -58,36 +58,39 @@ int addSkillNode(skillList * skillList, playerSkill * skill)
// Remove a skill node from a skill list:
int removeSkillNode(skillList * skillList, playerSkill * skill)
{
// Check the validity of the pointers:
if(skillList->head == NULL || skill == NULL)
{
return -1;
}
if(skillList->head->skill == skill)
{
skillNode * newHead = skillList->head->next;
free(skillList->head->skill);
if(skillList->head->next != NULL)
{
skillNode * deletedNode = skillList->head;
skillList->head = skillList->head->next;
free(deletedNode);
return 0;
}
else
{
skillNode * currentNode = skillList->head;
skillNode * previousNode = skillList->head;
while(currentNode->skill != skill)
{
if(currentNode->next == NULL)
{
return -1;
}
previousNode = currentNode;
currentNode = currentNode->next;
}
free(currentNode->skill);
previousNode->next = currentNode->next;
free(currentNode);
return 0;
}
free(skillList->head);
skillList->head = newHead;
return 0;
}
else
{
skillNode * currentNode = skillList->head;
skillNode * previousNode = skillList->head;
while(currentNode->skill != skill)
{
if(currentNode->next == NULL)
{
return -1;
}
previousNode = currentNode;
currentNode = currentNode->next;
}
free(currentNode->skill);
previousNode->next = currentNode->next;
free(currentNode);
return 0;
}
return -1;
}
// Take a skill and add it to the player's skill list:

View File

@ -27,12 +27,14 @@
typedef struct sockaddr sockaddr;
void sigintHandler(int signal)
{
printf("Caught signal %d.\n", signal);
exit(EXIT_SUCCESS);
}
int main(int argc, char ** argv)
{
time_t currentTime;
unsigned delay = 4000;
int socketFileDesc, connectionFileDesc, length, clientsAmount,
socketCheck, activityCheck, returnVal;
fd_set connectedClients;
@ -45,6 +47,20 @@ int main(int argc, char ** argv)
inputMessageQueue * inputQueue = createInputMessageQueue();
outputMessageQueue * outputQueue = createOutputMessageQueue();
// Parse command-line options:
int currentopt = 0;
while ((currentopt = getopt(argc, argv, "d:")) != -1)
{
switch(currentopt)
{
case 'd':
{
delay = atoi(optarg);
break;
}
}
}
// Set the handler for SIGINT:
signal(2, sigintHandler);
@ -97,8 +113,8 @@ int main(int argc, char ** argv)
// -==[ TEST GAME-STATE INITIALIZATION END ]==-
// Give an intro: Display the Silverkin Industries logo and splash text.
slowPrint(logostring, 3000);
slowPrint("\n--==== \033[33;40mSILVERKIN INDUSTRIES\033[0m COMM-LINK SERVER ====--\nVersion Alpha 0.4\n", 5000);
slowPrint(logostring, delay);
slowPrint("\n--==== \033[33;40mSILVERKIN INDUSTRIES\033[0m COMM-LINK SERVER ====--\nVersion Alpha 0.4\n", delay);
// Seed random number generator from the current time:
srandom((unsigned)time(&currentTime));
@ -119,7 +135,7 @@ int main(int argc, char ** argv)
else
{
slowPrint("\tSocket Creation is:\t\033[32;40mGREEN.\033[0m\n", 5000);
slowPrint("\tSocket Creation is:\t\033[32;40mGREEN.\033[0m\n", delay);
}
//
@ -139,7 +155,7 @@ int main(int argc, char ** argv)
else
{
slowPrint("\tSocket Binding is:\t\033[32;40mGREEN.\033[0m\n", 5000);
slowPrint("\tSocket Binding is:\t\033[32;40mGREEN.\033[0m\n", delay);
}
// Let's start listening:
@ -150,7 +166,7 @@ int main(int argc, char ** argv)
}
else
{
slowPrint("\tServer Listener is:\t\033[32;40mGREEN.\033[0m\n", 5000);
slowPrint("\tServer Listener is:\t\033[32;40mGREEN.\033[0m\n", delay);
}
length = sizeof(clientAddress);
@ -173,7 +189,7 @@ int main(int argc, char ** argv)
gnutls_credentials_set(tlssessions[index], GNUTLS_CRD_ANON, &serverkey);
gnutls_handshake_set_timeout(tlssessions[index], GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
}
slowPrint("\tTLS Preparation is:\t\033[32;40mGREEN.\033[0m\n", 5000);
slowPrint("\tTLS Preparation is:\t\033[32;40mGREEN.\033[0m\n", delay);
// Prepare the game logic thread:
gameLogicParameters * gameLogicThreadParameters = malloc(sizeof(gameLogicParameters));
@ -185,8 +201,8 @@ int main(int argc, char ** argv)
gameLogicThreadParameters->areaList = areas;
pthread_create(&gameLogicThread, NULL, &gameLogicLoop, gameLogicThreadParameters);
slowPrint("\tEvent Thread is:\t\033[32;40mGREEN.\033[0m\n", 5000);
slowPrint("=====\n", 5000);
slowPrint("\tEvent Thread is:\t\033[32;40mGREEN.\033[0m\n", delay);
slowPrint("=====\n", delay);
struct timeval timeout = {0, 500};
while(true)