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.
This commit is contained in:
parent
9411803942
commit
18a4f416f6
|
@ -9,6 +9,7 @@
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include "misc/playerdata.h"
|
||||||
#include "misc/texteffects.h"
|
#include "misc/texteffects.h"
|
||||||
#include "misc/inputhandling.h"
|
#include "misc/inputhandling.h"
|
||||||
#define MAX 1024
|
#define MAX 1024
|
||||||
|
@ -61,13 +62,16 @@ void * messageReceiver(void * parameters)
|
||||||
{
|
{
|
||||||
// Takes messages from the server and prints them to the chat log window:
|
// Takes messages from the server and prints them to the chat log window:
|
||||||
struct threadparameters *threadParameters = parameters;
|
struct threadparameters *threadParameters = parameters;
|
||||||
char receiveBuffer[MAX];
|
userMessage receiveBuffer;
|
||||||
while (!shouldExit)
|
while (!shouldExit)
|
||||||
{
|
{
|
||||||
read(threadParameters->socketDescriptor, receiveBuffer, MAX);
|
read(threadParameters->socketDescriptor, &receiveBuffer.senderName, sizeof(receiveBuffer.senderName));
|
||||||
slowPrintNcurses("USER-MESSAGE: ", 8000, threadParameters->window);
|
read(threadParameters->socketDescriptor, &receiveBuffer.messageContent, sizeof(receiveBuffer.messageContent));
|
||||||
slowPrintNcurses(receiveBuffer, 8000, threadParameters->window);
|
slowPrintNcurses(receiveBuffer.senderName, 8000, threadParameters->window);
|
||||||
bzero(receiveBuffer, MAX);
|
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);
|
pthread_exit(NULL);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include "misc/playerdata.h"
|
||||||
#include "misc/texteffects.h"
|
#include "misc/texteffects.h"
|
||||||
const int PORT = 5000;
|
const int PORT = 5000;
|
||||||
const int MAX = 1024;
|
const int MAX = 1024;
|
||||||
|
@ -23,10 +24,18 @@ int main()
|
||||||
socketCheck, activityCheck, readLength;
|
socketCheck, activityCheck, readLength;
|
||||||
int clientSockets[64];
|
int clientSockets[64];
|
||||||
int maxClients = 64;
|
int maxClients = 64;
|
||||||
|
userMessage sendBuffer;
|
||||||
char receiveBuffer[MAX];
|
char receiveBuffer[MAX];
|
||||||
fd_set connectedClients;
|
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.
|
// Give an intro: Display the Silverkin Industries logo and splash text.
|
||||||
slowPrint(logostring, 3000);
|
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.2\n", 5000);
|
||||||
|
@ -163,21 +172,49 @@ int main()
|
||||||
// Close the socket and mark as 0 in list for reuse:
|
// Close the socket and mark as 0 in list for reuse:
|
||||||
close(socketCheck);
|
close(socketCheck);
|
||||||
clientSockets[i] = 0;
|
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:
|
// Echo back the message that came in:
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("%d: %s", clientSockets[i], receiveBuffer);
|
printf("%d/%s: %s", clientSockets[i], connectedPlayers[i].playerName, receiveBuffer);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
strcpy(sendBuffer.senderName, connectedPlayers[i].playerName);
|
||||||
|
strcpy(sendBuffer.messageContent, receiveBuffer);
|
||||||
for (int sendIndex = 0; sendIndex < clientsAmount; sendIndex++)
|
for (int sendIndex = 0; sendIndex < clientsAmount; sendIndex++)
|
||||||
{
|
{
|
||||||
if(clientSockets[sendIndex] != STDIN_FILENO && clientSockets[sendIndex] != STDOUT_FILENO && clientSockets[sendIndex] != STDERR_FILENO)
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue