diff options
| author | Franklin Wei <frankhwei536@gmail.com> | 2014-07-14 17:51:16 -0400 |
|---|---|---|
| committer | Franklin Wei <frankhwei536@gmail.com> | 2014-07-14 17:51:16 -0400 |
| commit | 7d3df37b572192b6915f5fc3ea9803b195e3a5b3 (patch) | |
| tree | 0917985c5ee621acf0740d6d32d7f298b3cf7b46 | |
| parent | 06e815f51fcd38653a10f391325277d3db178a4b (diff) | |
| download | wargames-server-7d3df37b572192b6915f5fc3ea9803b195e3a5b3.zip wargames-server-7d3df37b572192b6915f5fc3ea9803b195e3a5b3.tar.gz wargames-server-7d3df37b572192b6915f5fc3ea9803b195e3a5b3.tar.bz2 wargames-server-7d3df37b572192b6915f5fc3ea9803b195e3a5b3.tar.xz | |
Minor fixes
| -rw-r--r-- | chatbot.c | 2 | ||||
| -rw-r--r-- | joshua.c | 6 | ||||
| -rw-r--r-- | server.c | 47 | ||||
| -rw-r--r-- | util.c | 13 | ||||
| -rw-r--r-- | util.h | 2 |
5 files changed, 40 insertions, 30 deletions
@@ -104,7 +104,7 @@ void do_chatbot(void) print_string("\n\n"); print_string(exit_responses[rand()%(sizeof(exit_responses)/sizeof(const char*))]); print_string("\n--CONNECTION TERMINATED--"); - return; + exit(EXIT_SUCCESS); } } for(int i=0;i<sizeof(greetings_triggers)/sizeof(const char*);++i) @@ -46,7 +46,6 @@ void random_stuff(void) /* print random junk on the screen for about 3 seconds * } void be_joshua(int fd) { - printf("joshua started.\n"); out_fd=fd; clear(); signal(SIGINT, &cleanup); @@ -80,7 +79,7 @@ void be_joshua(int fd) else if(ret==ERR || strcmp(buf, "joshua") && !gamesPhase) { print_string("\nIDENTIFICATION NOT RECOGNIZED BY SYSTEM\n--CONNECTION TERMINATED--"); - return; + exit(EXIT_SUCCESS); } } while(strcmp(buf, "joshua") || gamesPhase); random_stuff(); @@ -97,10 +96,11 @@ void be_joshua(int fd) print_string("\n\n"); print_string(exit_responses[rand()%sizeof(exit_responses)/sizeof(const char*)]); print_string("\n--CONNECTION TERMINATED--"); - return; + exit(EXIT_SUCCESS); } } print_string("\n\nHOW ARE YOU FEELING TODAY?\n\n"); refresh(); do_chatbot(); + exit(EXIT_SUCCESS); } @@ -1,4 +1,4 @@ -x/* +/* * WarGames - a WOPR emulator written in C * Copyright (C) 2014 Franklin Wei * @@ -18,17 +18,18 @@ x/* * Contact the author at contact@fwei.tk */ -#include <sys/types.h> -#include <sys/socket.h> -#include <unistd.h> +#include "joshua.h" #include <errno.h> -#include <netinet/in.h> #include <netdb.h> -#include <string.h> -#include <stdlib.h> -#include <stdio.h> +#include <netinet/in.h> #include <signal.h> -#include "joshua.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <unistd.h> + int server_socket; uint16_t port; int pipes[FD_SETSIZE][2]; @@ -58,9 +59,15 @@ int process_data(int fd) char buf[1024]; memset(buf, 0, sizeof(buf)); int ret=read(fd, buf, sizeof(buf)); + char ctrl_c[]={0xff, 0xf4, 0xff, 0xfd, 0x06, 0x00}; + if(strcmp(ctrl_c, buf)==0) + { + printf("Got CTRL-C from client.\n"); + return -1; + } if(ret<0) /* error */ { - printf("error in read()\n"); + printf("Error in read()\n"); return -1; } if(ret==0) @@ -80,7 +87,6 @@ void serv_cleanup() printf("preparing to exit...\n"); fflush(stdout); shutdown(server_socket, SHUT_RDWR); - close(server_socket); } int main(int argc, char* argv[]) { @@ -89,8 +95,8 @@ int main(int argc, char* argv[]) printf("Usage: %s PORT\n", argv[0]); return 2; } - printf("starting server...\n"); port=atoi(argv[1]); + printf("Initializing server on port %u...\n", port); signal(SIGINT, &serv_cleanup); int sock=make_server_socket(port); server_socket=sock; @@ -98,19 +104,19 @@ int main(int argc, char* argv[]) struct sockaddr_in client; if(listen(sock, 1)<0) { - printf("error listening.\n"); + printf("Error opening socket.\n"); return 1; } FD_ZERO(&active_fd_set); FD_SET(sock, &active_fd_set); - printf("listening on port %d\n", port); + printf("Server ready, listening on port %d.\n", port); while(1) { read_fd_set=active_fd_set; int ret=select(FD_SETSIZE, &read_fd_set, 0,0,0); if(ret<0) { - printf("select() returned error.\n"); + printf("Error in select().\n"); return 1; } for(int i=0;i<FD_SETSIZE;++i) @@ -125,21 +131,22 @@ int main(int argc, char* argv[]) new=accept(sock, (struct sockaddr*) &client, &size); if(new<0) { - printf("error accepting connection.\n"); + printf("Error accepting new connection.\n"); return 1; } - printf("new connection from \n"); + printf("New connection.\n"); FD_SET(new, &active_fd_set); int ret=pipe(pipes[new]); if(ret<0) { - printf("pipe error.\n"); + printf("Pipe error.\n"); } pid_t pid=fork(); if(pid==0) /* child */ { be_joshua(new); - close(new); + printf("Client exits\n"); + shutdown(new, SHUT_RDWR); FD_CLR(new, &active_fd_set); exit(0); } @@ -149,7 +156,7 @@ int main(int argc, char* argv[]) /* data from existing connection */ if(process_data(i)<0) { - close(i); + shutdown(i, SHUT_RDWR); FD_CLR(i, &active_fd_set); } } @@ -40,6 +40,7 @@ void print_string(const char* str) /* print string, slowly */ { write(out_fd, &str[i], 1); fsync(out_fd); + usleep(SLEEP_TIME); ++i; } } @@ -58,6 +59,9 @@ void remove_punct(char* buf) } void clear(void) { + unsigned char clear_sequence[]={ 0x1B, 'c' }; + write(out_fd, clear_sequence, sizeof(clear_sequence)); + fsync(out_fd); } void refresh(void) { @@ -65,16 +69,15 @@ void refresh(void) } int getnstr(char* buf, int max) { - printf("reading...\n"); memset(buf, 0, max); int ret=read(pipes[out_fd][0], buf, max); if(ret!=0) { - printf("last char is 0x%02x\n", buf[strlen(buf)-1]); - /* remove the newline */ - buf[strlen(buf)-2]='\0'; + /* prevent buffer underflow */ + if(strlen(buf)-2>=0) + /* remove the newline */ + buf[strlen(buf)-2]='\0'; } - printf("got string \"%s\"\n", buf); if(ret<0) return ERR; return OK; @@ -14,7 +14,7 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * * Contact the author at contact@fwei.tk */ |