diff --git a/src/Makefile b/src/Makefile index 7b2d5c0..cf311be 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,4 +1,5 @@ CFLAGS=-g -std=gnu99 -Wall -pedantic +LDFLAGS=$(CFLAGS) all: server diff --git a/src/game.c b/src/game.c index 2600f4c..32362e8 100644 --- a/src/game.c +++ b/src/game.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "net.h" #include "game.h" diff --git a/src/net.c b/src/net.c index 9f7940b..287d014 100644 --- a/src/net.c +++ b/src/net.c @@ -30,8 +30,6 @@ struct client clients[MAX_CLIENTS]; struct pollfd pollfds[SRV_FDS+MAX_CLIENTS]; int nclients = 0; -FILE *net_side[2]; - int errx(int eval, const char *fmt, ...) { va_list ap; @@ -41,21 +39,11 @@ errx(int eval, const char *fmt, ...) { exit(eval); } -/* -void -parse_line(int clientid, char *line) { - printf("line: %d \"%s\"\n", clientid, line); -} -*/ - void parse_linetoolong(int clientid, char *line) { printf("linetoolong: %d \"%s\"\n", clientid, line); } - -struct file sfile[2]; - void net_server(int port) { struct sockaddr_in serveraddr; @@ -77,29 +65,6 @@ net_server(int port) { errx(1, "bind port %d: %s\n", port, strerror(errno)); if (listen(servsock, 10) < 0) errx(1, "listen: %s\n", strerror(errno)); -#if 0 - // FIXME Temporary startup. Use net_poll for this later: - printf("Waiting for connections on port %d...\n", port); - for (int i=0; i<2; i++) { - struct sockaddr_in clientaddr; - int cfd; - socklen_t slen; - cfd=accept(servsock, (struct sockaddr *)&clientaddr, &slen); - if (cfd<0) - errx(1, "accept: %s\n", strerror(errno)); - // FIXME log, shout, or something - printf("client log on...\n"); - sfile[i].fd=cfd; - sfile[i].istart=0; - sfile[i].iend=0; - sfile[i].oend=0; - - net_client_printf(i, "Welcome!\n"); - if (i==0) - net_client_printf(i, "Waiting for another player...\n"); - } - net_all_printf("Starting game\n"); -#else pollfds[0].fd=servsock; pollfds[0].events=POLLIN|POLLERR|POLLHUP; for (int i=0; ifd, f->ibuf, BUFSIZ); - if (n==0) - errx(1, "end of file\n"); - if (n<0) - errx(1, "read: %s\n", strerror(errno)); - f->iend=n; - f->istart=1; - return f->ibuf[0]; -} - -#if 0 -int -net_getc(int side) { - struct file *f=&sfile[side]; - if (f->istart == f->iend) - net_fillbuf(f); - return f->ibuf[f->istart++]; -} -#endif - - -#if 0 -void -net_client_flush(int side) { - start=0; - do start+=write(fd[side], obuf[side]+start, obufend); - while (start < obufend); -} - -void -net_all_flush() { - for (int i=0; i<2; i++) - net_client_flush(i); -} - -static void -net_client_vprintf(int side, const char *fmt, va_list ap) { - n=vsnprintf(obuf[side], BUFSIZ-obufend, fmt, ap); - if (n > (BUFSIZ-obufend-1)) { - if (n > BUFSIZ-1) - errx(1, "BUFSIZ too small\n"); - net_client_flush(side); - n=vsnprintf(obuf[side], BUFSIZ-obufend, fmt, ap); - } - obufend[side]+=n; -} - -void -net_all_printf(const char *fmt, ...) { - va_list ap; - va_start(ap, fmt); - for (int i=0; i<2; i++) - net_client_vprintf(i, fmt, ap); - va_end(ap); -} - -void -net_client_printf(int side, const char *fmt, ...) { - va_list ap; - assert((side==0) || (side==1)); - va_start(ap, fmt); - net_client_vprintf(side, fmt, ap); - va_end(ap); -} -#endif - void net_client_accept(int servsock); void net_client_read(int clientid); @@ -363,11 +242,3 @@ net_client_read(int clientid) { } } -/* -int -main() { - net_server(6243); - for (;;) - net_poll(); -} -*/ diff --git a/src/net.h b/src/net.h index 226c8fb..9510bc8 100644 --- a/src/net.h +++ b/src/net.h @@ -3,35 +3,14 @@ #ifndef KHET_NET_H #define KHET_NET_H -#include - -struct file { - int fd; - char ibuf[BUFSIZ]; - char obuf[BUFSIZ]; - int istart; - int iend; - int oend; -}; - -extern struct file sfile[2]; - +#define MAX_CLIENTS 10 extern void net_server(int port); extern void net_poll(); -#if 1 -extern int net_getc_fillbuf(struct file *f); -#define net_getc(side) (sfile[side].istart < sfile[side].iend ? \ - sfile[side].ibuf[sfile[side].istart++] : \ - net_getc_fillbuf(&sfile[side])) -#else -extern int _net_getc(int side); -#define net_getc _net_getc -#endif extern void net_all_printf(const char *fmt, ...); extern void net_client_printf(int side, const char *fmt, ...); -extern void net_all_flush(); -#endif + +#endif // KHET_NET_H diff --git a/src/server.c b/src/server.c index a024881..a23b37c 100644 --- a/src/server.c +++ b/src/server.c @@ -161,74 +161,6 @@ terminal_game(void) } } -int -read_move(int side, move_t *m) -{ - char buf[MAX_MSG_LEN+1]; - char c; - int i; - for (i = 0; i < MAX_MSG_LEN+1; i++) { - c = net_getc(side); - printf("input '%c'\n", c); - if (c == '\n') - break; - buf[i] = c; - } - buf[i] = '\0'; - while (c != '\n') c = net_getc(side); - - return parse_move(buf, m); -} - -void -simple_game(void) -{ - int side = 0; - - game_t game; - char aabuf[ASCIIART_SMALL_BUFSZ]; - - init_game(&game); - setup_board(&game, classic_setup); - asciiart_small_draw_board(aabuf, &game); - net_all_printf("%s", aabuf); - //print_board_small(); - - for (;;) { - move_t m; - int r; - - net_all_printf("%s's move\n", sideinfo[side].name); - prompt: - net_client_printf(side, "> "); - net_all_flush(); - - r = read_move(side, &m); - if (r != HUGE_SUCCESS) { - net_client_printf(side, "unknown command\n"); - goto prompt; - } - - if (perform_move(&game, side, m) != HUGE_SUCCESS) { - net_client_printf(side, "illegal move\n"); - goto prompt; - } - - asciiart_small_draw_board(aabuf, &game); - net_all_printf("%s", aabuf); - - switch (game.winner) { - case SILVER: - net_all_printf("SILVER WINS!\n"); - return; - case RED: - net_all_printf("RED WINS!\n"); - return; - } - side = game.move % 2; - } -} - game_t game;