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.
This commit is contained in:
parent
849a80bd37
commit
33bc9bcda0
6
Makefile
6
Makefile
|
@ -5,13 +5,13 @@ clientobj = $(clientsrc:.c=.o)
|
||||||
serversrc = $(wildcard src/misc/*.c) \
|
serversrc = $(wildcard src/misc/*.c) \
|
||||||
src/SilverMUDServer.c
|
src/SilverMUDServer.c
|
||||||
serverobj = $(serversrc:.c=.o)
|
serverobj = $(serversrc:.c=.o)
|
||||||
CLIENTLDFLAGS= -lpthread
|
CLIENTLDFLAGS= -lpthread -lncurses
|
||||||
|
SERVERLDFLAGS= -lncurses
|
||||||
SilverMUDClient: $(clientobj)
|
SilverMUDClient: $(clientobj)
|
||||||
gcc -o $@ $^ $(CLIENTLDFLAGS)
|
gcc -o $@ $^ $(CLIENTLDFLAGS)
|
||||||
|
|
||||||
SilverMUDServer: $(serverobj)
|
SilverMUDServer: $(serverobj)
|
||||||
gcc -o $@ $^
|
gcc -o $@ $^ $(SERVERLDFLAGS)
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <ncurses.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include "misc/texteffects.h"
|
#include "misc/texteffects.h"
|
||||||
|
@ -20,8 +21,8 @@ void * messageSender(void * sockfd)
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
bzero(sendBuffer, MAX);
|
bzero(sendBuffer, MAX);
|
||||||
printf("COMM-LINK> ");
|
wprintw(stdscr, "COMM-LINK> ");
|
||||||
if(fgets(sendBuffer, MAX, stdin) == NULL)
|
if(wgetnstr(stdscr, sendBuffer, MAX) == ERR)
|
||||||
{
|
{
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
@ -41,9 +42,9 @@ void * messageReceiver(void * sockfd)
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
read((long)sockfd, receiveBuffer, MAX);
|
read((long)sockfd, receiveBuffer, MAX);
|
||||||
slowPrint("\nUSER-MESSAGE: ", 8000);
|
slowPrintNcurses("\nUSER-MESSAGE: ", 8000);
|
||||||
slowPrint(receiveBuffer, 8000);
|
slowPrintNcurses(receiveBuffer, 8000);
|
||||||
slowPrint("\nCOMM-LINK (CONT.)> ", 8000);
|
slowPrintNcurses("\nCOMM-LINK (CONT.)> ", 8000);
|
||||||
bzero(receiveBuffer, MAX);
|
bzero(receiveBuffer, MAX);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,7 +54,6 @@ int main(int argc, char **argv)
|
||||||
int sockfd, connfd;
|
int sockfd, connfd;
|
||||||
struct sockaddr_in servaddr, cli;
|
struct sockaddr_in servaddr, cli;
|
||||||
pthread_t messagingThread;
|
pthread_t messagingThread;
|
||||||
|
|
||||||
// Give me a socket, and make sure it's working:
|
// Give me a socket, and make sure it's working:
|
||||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (sockfd == -1)
|
if (sockfd == -1)
|
||||||
|
@ -89,6 +89,11 @@ int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
slowPrint("Connected to the Silverkin Industries Comm-Link Server:\nHave a pleasant day.\n", 8000);
|
slowPrint("Connected to the Silverkin Industries Comm-Link Server:\nHave a pleasant day.\n", 8000);
|
||||||
}
|
}
|
||||||
|
usleep(100000);
|
||||||
|
|
||||||
|
// Setup Ncurses:
|
||||||
|
initscr();
|
||||||
|
scrollok(stdscr, true);
|
||||||
|
|
||||||
// Run a thread to send messages, and use main to recieve.
|
// Run a thread to send messages, and use main to recieve.
|
||||||
pthread_create(&messagingThread, NULL, messageSender, (void *)(long)sockfd);
|
pthread_create(&messagingThread, NULL, messageSender, (void *)(long)sockfd);
|
||||||
|
@ -96,4 +101,7 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
// Close the socket.
|
// Close the socket.
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
|
|
||||||
|
// Unsetup Ncurses:
|
||||||
|
endwin();
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <ncurses.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// Barry Kane, 2021.
|
// Barry Kane, 2021.
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <ncurses.h>
|
||||||
|
|
||||||
void slowPrint(char * stringToPrint, int delay)
|
void slowPrint(char * stringToPrint, int delay)
|
||||||
{
|
{
|
||||||
|
@ -15,3 +16,16 @@ void slowPrint(char * stringToPrint, int delay)
|
||||||
characterIndex++;
|
characterIndex++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void slowPrintNcurses(char * stringToPrint, int delay)
|
||||||
|
{
|
||||||
|
int characterIndex = 0;
|
||||||
|
while(stringToPrint[characterIndex] != '\0')
|
||||||
|
{
|
||||||
|
addch(stringToPrint[characterIndex]);
|
||||||
|
// Refresh the ncurses screen.
|
||||||
|
refresh();
|
||||||
|
usleep(delay);
|
||||||
|
characterIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,9 @@
|
||||||
// A fancy, character by character print. Similar to a serial terminal with lower baud rate.
|
// A fancy, character by character print. Similar to a serial terminal with lower baud rate.
|
||||||
void slowPrint(char * stringToPrint, int delay);
|
void slowPrint(char * stringToPrint, int delay);
|
||||||
|
|
||||||
|
// The same, altered to work with Ncurses.
|
||||||
|
void slowPrintNcurses(char * stringToPrint, int delay);
|
||||||
|
|
||||||
// A string containing an ASCII art version of the Silverkin Industries logo.
|
// A string containing an ASCII art version of the Silverkin Industries logo.
|
||||||
char * logostring = " ///////\n //////////////////////////////////////////\n ///////////////////////////////////////////////////////////\n ////////// ////////////////////////////\n ### # # # # ##### ### # # # # # /////////////////\n ### # # # # ## # # ## # ## # //////////////\n ## # # # # # ### # # # # # # /////////\n #### # ### # ##### # # # # # # ## ///////\n # ## # ##### # # ### ### ### # ##### ### ////// \n # # # # # # # # ## # # # # ## ## ////\n # # # # # # # # ## # ### # # ## //\n # # ### ##### ##### ### # # # # #### ### //\n";
|
char * logostring = " ///////\n //////////////////////////////////////////\n ///////////////////////////////////////////////////////////\n ////////// ////////////////////////////\n ### # # # # ##### ### # # # # # /////////////////\n ### # # # # ## # # ## # ## # //////////////\n ## # # # # # ### # # # # # # /////////\n #### # ### # ##### # # # # # # ## ///////\n # ## # ##### # # ### ### ### # ##### ### ////// \n # # # # # # # # ## # # # # ## ## ////\n # # # # # # # # ## # ### # # ## //\n # # ### ##### ##### ### # # # # #### ### //\n";
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue