Made client exit gracefully upon server exit:

- The client now checks the return value of messageReceive.
- Renamed lists.c/.h to areadata.c/.h.
This commit is contained in:
Barry Kane 2022-10-16 21:28:32 +01:00
parent b8189ae2de
commit 60110d3abd
5 changed files with 28 additions and 16 deletions

View File

@ -1,6 +1,6 @@
// Implementation of lists library for SilverMUD. // areadata.c: Implements functions for playerAreas and playerPaths in SilverMUD:
// Barry Kane, 2021 // Barra Ó Catháin, 2022.
#include "lists.h" #include "areadata.h"
#include "playerdata.h" #include "playerdata.h"
areaNode * createAreaList(playerArea * initialArea) areaNode * createAreaList(playerArea * initialArea)

View File

@ -1,7 +1,7 @@
// lists.h: A header file for the lists library for SilverMUD. // areadata.h: Contains data structures and functions for playerAreas and playerPaths in SilverMUD:
// Barry Kane, 2021. // Barra Ó Catháin, 2022.
#ifndef LISTS_H #ifndef AREADATA_H
#define LISTS_H #define AREADATA_H
#include "playerdata.h" #include "playerdata.h"
typedef struct areaNode areaNode; typedef struct areaNode areaNode;

View File

@ -13,6 +13,7 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <gnutls/gnutls.h> #include <gnutls/gnutls.h>
#include "../constants.h" #include "../constants.h"
#include "../playerdata.h" #include "../playerdata.h"
#include "../texteffects.h" #include "../texteffects.h"
@ -66,21 +67,29 @@ void * messageSender(void * parameters)
// Send the message off to the server: // Send the message off to the server:
messageSend(threadParameters->tlsSession, &sendBuffer); messageSend(threadParameters->tlsSession, &sendBuffer);
} }
// Rejoin the main thread:
pthread_exit(NULL); pthread_exit(NULL);
} }
void * messageReceiver(void * parameters) void * messageReceiver(void * parameters)
{ {
struct threadparameters *threadParameters = parameters; int returnValue = 0;
bool serverMessage = false;
userMessage receiveBuffer; userMessage receiveBuffer;
bool serverMessage = false;
struct threadparameters *threadParameters = parameters;
int screenWidth = getmaxx(threadParameters->window); int screenWidth = getmaxx(threadParameters->window);
// Repeatedly take messages from the server and print them to the chat log window: // Repeatedly take messages from the server and print them to the chat log window:
while (!shouldExit) while (!shouldExit)
{ {
messageReceive(threadParameters->tlsSession, &receiveBuffer); returnValue = messageReceive(threadParameters->tlsSession, &receiveBuffer);
if (receiveBuffer.senderName[0] == '\0') // Check we haven't been disconnected:
if(returnValue == -10 || returnValue == 0)
{
shouldExit = true;
}
else if (receiveBuffer.senderName[0] == '\0')
{ {
wrapString(receiveBuffer.messageContent, wrapString(receiveBuffer.messageContent,
strlen(receiveBuffer.messageContent) - 1, screenWidth); strlen(receiveBuffer.messageContent) - 1, screenWidth);
@ -276,6 +285,8 @@ int main(int argc, char **argv)
messageArea->window = newwin(3, COLS - 2, LINES - 4, 1); messageArea->window = newwin(3, COLS - 2, LINES - 4, 1);
messageArea->tlsSession = tlsSession; messageArea->tlsSession = tlsSession;
messageArea->loggingFlag = gameLogging; messageArea->loggingFlag = gameLogging;
// Set the appropriate log pointers:
if (gameLog != NULL) if (gameLog != NULL)
{ {
messageArea->loggingStream = gameLog; messageArea->loggingStream = gameLog;

View File

@ -3,7 +3,7 @@
// Barry Kane, 2022. // Barry Kane, 2022.
#ifndef GAMELOGIC_H #ifndef GAMELOGIC_H
#define GAMELOGIC_H #define GAMELOGIC_H
#include "lists.h" #include "areadata.h"
#include "constants.h" #include "constants.h"
#include "playerdata.h" #include "playerdata.h"
#include "inputoutput.h" #include "inputoutput.h"

View File

@ -16,7 +16,7 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <gnutls/gnutls.h> #include <gnutls/gnutls.h>
#include "../lists.h" #include "../areadata.h"
#include "../gamelogic.h" #include "../gamelogic.h"
#include "../constants.h" #include "../constants.h"
#include "../playerdata.h" #include "../playerdata.h"
@ -115,6 +115,7 @@ int main()
slowPrint("\tSocket Creation is:\t\033[32;40mGREEN.\033[0m\n", 5000); slowPrint("\tSocket Creation is:\t\033[32;40mGREEN.\033[0m\n", 5000);
} }
//
bzero(&serverAddress, sizeof(serverAddress)); bzero(&serverAddress, sizeof(serverAddress));
// Assign IP and port: // Assign IP and port: