From 52c0fed848a647d41352bdd8255b1b748008fa19 Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Wed, 15 Sep 2021 00:22:15 +0100 Subject: [PATCH] Alpha 0.3 release of SilverMUD: Features Added: - Basic naming system. - playerdata.h, containing datastructures related to storing player information. - Ability to change name via '/NAME'. Features Changed: - Client now shows message sender's name. Features Removed: - None. Squashed commit of the following: commit 0ef71dbfce09c65dd988489557d0acdb3e20114e Author: Barry Kane Date: Wed Sep 15 00:12:05 2021 +0100 Incremented Version Number. - Incremented version number in preperation for merge. commit 18a4f416f6970bd826a6a5157cb03a61e1702048 Author: Barry Kane Date: Wed Sep 15 00:07:13 2021 +0100 Added basic name system - Added basic name system. - Added playerdata.h. - Added basic /NAME command. TODO: Create proper command system. - Added datastructures for user messages and user names. commit 94118039427c81e047424c73f2f6c3ccb2e88f94 Author: Barry Kane Date: Fri Sep 10 15:07:42 2021 +0100 Increment version message for merge. Incremented the version number by 0.1 for the server. Added version splash to the client. commit 7047d0ee08dd522709d3130fa340d33f4ab5e23f Author: Barry Kane Date: Fri Sep 10 15:03:02 2021 +0100 Added two-window messaging to the client. Client now has two seperate Ncurses windows for sending and receiving. Added SIGINT handler which sets a global boolean to gracefully exit and free memory. Sending and Receiving are now on their own threads. A pointer-to-struct is now passed to the threads. The main thread will now wait to cancel the threads upon receiving SIGINT. slowPrintNcurses now takes a window argument. The server now doesn't check that a client receives the message that they sent, allowing for full chat history. commit 33bc9bcda0c5d4afbbfa9b5371ad2ef83b5e6f1b Author: Barry Kane Date: Fri Sep 3 18:47:11 2021 +0100 Adapted client to use Ncurses instead of raw terminal output: Created "slowPrintNcurses", which is a version of "slowPrint" compatible with Ncurses screens. Ncurses is now used in place of raw-terminal output. The screen clears after inital start-up messages. C-d no longer exits, and still doesn't spam. Added Ncurses to the ld options of client in the Makefile. Created ld options for server in the Makefile. commit 849a80bd377ffad8c3f4cad4880540d45c36173c Author: Barry Kane Date: Thu Aug 19 23:07:58 2021 +0100 Basic input sanatization: Created new library to deal with user input. Implemented check in client to prevent C-d spamming the server. C-d now exits. Implemented check in client to prevent clients sending messages containing only newlines to the server. commit 2c093903a4f5c32a659f085922f9cab28dd8a2b0 Author: Barry Kane Date: Tue Aug 17 18:57:56 2021 +0100 Git Sanity Check --- src/SilverMUDClient.c | 16 ++++++++----- src/SilverMUDServer.c | 53 ++++++++++++++++++++++++++++++++++++------- src/misc/playerdata.h | 17 ++++++++++++++ 3 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 src/misc/playerdata.h diff --git a/src/SilverMUDClient.c b/src/SilverMUDClient.c index bde2293..f64bb66 100644 --- a/src/SilverMUDClient.c +++ b/src/SilverMUDClient.c @@ -9,6 +9,7 @@ #include #include #include +#include "misc/playerdata.h" #include "misc/texteffects.h" #include "misc/inputhandling.h" #define MAX 1024 @@ -61,13 +62,16 @@ void * messageReceiver(void * parameters) { // Takes messages from the server and prints them to the chat log window: struct threadparameters *threadParameters = parameters; - char receiveBuffer[MAX]; + userMessage receiveBuffer; while (!shouldExit) { - read(threadParameters->socketDescriptor, receiveBuffer, MAX); - slowPrintNcurses("USER-MESSAGE: ", 8000, threadParameters->window); - slowPrintNcurses(receiveBuffer, 8000, threadParameters->window); - bzero(receiveBuffer, MAX); + read(threadParameters->socketDescriptor, &receiveBuffer.senderName, sizeof(receiveBuffer.senderName)); + read(threadParameters->socketDescriptor, &receiveBuffer.messageContent, sizeof(receiveBuffer.messageContent)); + slowPrintNcurses(receiveBuffer.senderName, 8000, threadParameters->window); + slowPrintNcurses(": ", 8000, threadParameters->window); + slowPrintNcurses(receiveBuffer.messageContent, 8000, threadParameters->window); + bzero(receiveBuffer.senderName, sizeof(receiveBuffer.senderName)); + bzero(receiveBuffer.messageContent, sizeof(receiveBuffer.messageContent)); } pthread_exit(NULL); } @@ -83,7 +87,7 @@ int main(int argc, char **argv) signal(SIGINT, sigintHandler); // Print welcome message: - slowPrint("\n--==== \033[33;40mSILVERKIN INDUSTRIES\033[0m COMM-LINK CLIENT ====--\nVersion Alpha 0.2\n", 5000); + slowPrint("\n--==== \033[33;40mSILVERKIN INDUSTRIES\033[0m COMM-LINK CLIENT ====--\nVersion Alpha 0.3\n", 5000); // Give me a socket, and make sure it's working: sockfd = socket(AF_INET, SOCK_STREAM, 0); diff --git a/src/SilverMUDServer.c b/src/SilverMUDServer.c index 3eb8724..c2bde11 100644 --- a/src/SilverMUDServer.c +++ b/src/SilverMUDServer.c @@ -1,4 +1,4 @@ -// Silverkin Industries Comm-Link Server, Engineering Sample Alpha 0.1. +// Silverkin Industries Comm-Link Server, Engineering Sample Alpha 0.3. // PROJECT CODENAME: WHAT DO I PAY YOU FOR? | Level-3 Clearance. // Barry Kane, 2021 #include @@ -12,6 +12,7 @@ #include #include #include +#include "misc/playerdata.h" #include "misc/texteffects.h" const int PORT = 5000; const int MAX = 1024; @@ -23,13 +24,21 @@ int main() socketCheck, activityCheck, readLength; int clientSockets[64]; int maxClients = 64; + userMessage sendBuffer; char receiveBuffer[MAX]; fd_set connectedClients; - struct sockaddr_in serverAddress, clientAddress; + playerInfo connectedPlayers[64]; + struct sockaddr_in serverAddress, clientAddress; + // Initialize playerdata: + for (int index = 0; index < maxClients; index++) + { + strcpy(connectedPlayers[index].playerName, "UNNAMED"); + } + // 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.2\n", 5000); + slowPrint("\n--==== \033[33;40mSILVERKIN INDUSTRIES\033[0m COMM-LINK SERVER ====--\nVersion Alpha 0.3\n", 5000); // Initialize the sockets to 0, so we don't crash. for (int index = 0; index < maxClients; index++) @@ -163,21 +172,49 @@ int main() // Close the socket and mark as 0 in list for reuse: close(socketCheck); clientSockets[i] = 0; - } - + } + // Name change command: Move logic to a command interpreter later: + else if (receiveBuffer[0] == '/') + { + char newName[32]; + if(strncmp(receiveBuffer, "/NAME", 5) == 0) + { + strncpy(newName, &receiveBuffer[6], 32); + // Remove newlines: + for (int index = 0; index < 32; index++) + { + if (newName[index] == '\n') + { + newName[index] = '\0'; + } + } + for (int index = 0; index < maxClients; index++) + { + if(strncmp(newName, connectedPlayers[index].playerName, 32) == 0) + { + break; + } + } + strncpy(connectedPlayers[i].playerName, newName, 32); + } + } // Echo back the message that came in: else { - printf("%d: %s", clientSockets[i], receiveBuffer); + printf("%d/%s: %s", clientSockets[i], connectedPlayers[i].playerName, receiveBuffer); fflush(stdout); + strcpy(sendBuffer.senderName, connectedPlayers[i].playerName); + strcpy(sendBuffer.messageContent, receiveBuffer); for (int sendIndex = 0; sendIndex < clientsAmount; sendIndex++) { if(clientSockets[sendIndex] != STDIN_FILENO && clientSockets[sendIndex] != STDOUT_FILENO && clientSockets[sendIndex] != STDERR_FILENO) { - write(clientSockets[sendIndex], receiveBuffer, sizeof(receiveBuffer)); + write(clientSockets[sendIndex], sendBuffer.senderName, sizeof(sendBuffer.senderName)); + write(clientSockets[sendIndex], sendBuffer.messageContent, sizeof(sendBuffer.messageContent)); } } - bzero(receiveBuffer, sizeof(receiveBuffer)); + bzero(sendBuffer.senderName, sizeof(sendBuffer.senderName)); + bzero(sendBuffer.messageContent, sizeof(sendBuffer.messageContent)); } } } diff --git a/src/misc/playerdata.h b/src/misc/playerdata.h new file mode 100644 index 0000000..4cf1d8c --- /dev/null +++ b/src/misc/playerdata.h @@ -0,0 +1,17 @@ +// playerdata.h: Header file containing data structures for player data and function +// definitions for interacting with said data. +// Barry Kane, 2021. + +typedef struct userMessage +{ + char senderName[32]; + char messageContent[1024]; +} userMessage; + +typedef struct playerInfo +{ + char playerName[32]; +} playerInfo; + + +