From: Bryan Worth Date: Mon, 5 Dec 2005 12:34:13 +0000 (+0000) Subject: 2005-12-04 Bryan Worth X-Git-Url: https://git.cworth.org/git?p=ttt;a=commitdiff_plain;h=8fbec78bf02e67469ffaea254d49c291f9987105 2005-12-04 Bryan Worth * src/ttt-socket.c: (ttt_socket_create_client) fixed socket to be non-blocking. (ttt_socket_read) added return value for number of bytes read * Makefile.am: added necessary code for new curses-client program. * src/ttt-curses-client: new ncurses based client --- diff --git a/ChangeLog b/ChangeLog index ebf09f7..7cb6add 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2005-12-04 Bryan Worth + * src/ttt-socket.c: (ttt_socket_create_client) fixed socket to be + non-blocking. (ttt_socket_read) added return value for number + of bytes read + * Makefile.am: added necessary code for new curses-client program. + * src/ttt-curses-client: new ncurses based client + 2005-12-03 Richard D. Worth * PROTOCOL: Add RETRACT, DECLINE. Add game identifier for all game diff --git a/src/Makefile.am b/src/Makefile.am index 1d5de3f..9ca2e53 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -bin_PROGRAMS = ttt-server ttt-client +bin_PROGRAMS = ttt-server ttt-client ttt-curses-client noinst_PROGRAMS = test-board ttt_common_sources = \ @@ -27,9 +27,14 @@ test_board_SOURCES = \ $(ttt_common_sources) \ test-board.c +ttt_curses_client_SOURCES = \ + $(ttt_common_sources) \ + curses.h \ + ttt-curses-client.c + AM_CFLAGS = $(WARN_CFLAGS) $(TTT_CFLAGS) AM_LFLAGS = --header-file=ttt-lex.h -Cr ttt_client_LDFLAGS = $(TTT_LIBS) ttt_server_LDFLAGS = $(TTT_LIBS) -lpthread test_board_LDFLAGS = $(TTT_LIBS) - +ttt_curses_client_LDFLAGS = $(TTT_LIBS) -lncurses diff --git a/src/ttt-curses-client.c b/src/ttt-curses-client.c new file mode 100644 index 0000000..b6e5981 --- /dev/null +++ b/src/ttt-curses-client.c @@ -0,0 +1,221 @@ +/* ttt-client.c - client handling code for tic-tac-toe game server + * + * Copyright © 2005 Carl Worth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Author: Bryan Worth + */ + + +/* TODO: see /usr/share/doc/ncurses-devel-5.3/test/view.c for example code + * to handle window resizing + * + * add code to read username from config file or prompt user if config + * file does not exist. (username is currently hardcoded and needs + * changed before compiling) + * + */ + + +#include "ttt-socket.h" +#include +#include + +static void finish(int sig); + +void mvprintstr(int, int, char *, chtype); +void mvwprintstr(WINDOW *, int, int, char *, chtype); +void wprint(WINDOW *, char *); + +void mvprintstr(int y, int x, char *string, chtype attrs) +{ + int xx = 0; + int l; + chtype cline[1000]; + l = strlen(string); + while (xx < l) { + cline[xx] = string[xx] | attrs; + xx++; + } + cline[l] = '\0'; + mvaddchstr(y, x, cline); +} + + +void mvwprintstr(WINDOW * window, int y, int x, char *string, chtype attrs) +{ + int xx = 0; + int l; + chtype cline[1000]; + l = strlen(string); + while (xx < l) { + cline[xx] = string[xx] | attrs; + xx++; + } + cline[l] = '\0'; + mvwaddchstr(window, y, x, cline); +} + + +void wprint(WINDOW * window, char *string) +{ + int xx = 0; + int l; + chtype outc; + l = strlen(string); + + while (xx < l) { + outc = string[xx]; + if (string[xx] != '\r') + waddch(window, outc); + xx++; + } +} + + +char *message2; +int _socket; +size_t maxread; + + +int main(int argc, char **argv) +{ + char *message; + char buffer[1000]; + char *host; + char *port; + chtype c; + ssize_t numread; + + static WINDOW *mainwnd; + static WINDOW *dispwin; + static WINDOW *inpwin; + + mainwnd = initscr(); + int dlines = LINES - 2, cols = COLS; + + (void) nonl(); + noecho(); + cbreak(); + nodelay(mainwnd, TRUE); + curs_set(0); + refresh(); + dispwin = newwin(dlines - 1, cols - 2, 0, 0); + inpwin = newwin(1, cols - 2, dlines, 0); + keypad(mainwnd, TRUE); // enable keyboard mapping + keypad(inpwin, TRUE); // enable keyboard mapping + nodelay(inpwin, TRUE); + wrefresh(mainwnd); + wrefresh(dispwin); + wrefresh(inpwin); + (void) scrollok(mainwnd, TRUE); + (void) scrollok(dispwin, TRUE); + (void) scrollok(inpwin, TRUE); + (void) idlok(mainwnd, TRUE); + (void) idlok(dispwin, TRUE); + (void) idlok(inpwin, TRUE); + wsetscrreg(dispwin, 0, dlines - 2); + + (void) signal(SIGINT, finish); /* arrange interrupts to terminate */ + if (has_colors()) { + start_color(); + + init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK); + init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK); + init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK); + init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK); + init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK); + init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK); + init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK); + init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK); + } + wbkgd(dispwin, COLOR_PAIR(COLOR_WHITE)); + wbkgd(inpwin, COLOR_PAIR(COLOR_CYAN)); + wrefresh(dispwin); + wrefresh(inpwin); + + xasprintf(&host, "localhost"); + xasprintf(&port, "5334"); + xasprintf(&message, "HELO bryan\r\n"); + + ttt_socket_create_client(host, port, &_socket); + ttt_socket_write(_socket, message, strlen(message)); + + maxread = 100; + numread = read(_socket, buffer, maxread); + if (numread > 0) { + buffer[numread] = '\0'; + xasprintf(&message, "%s", buffer); + wprint(dispwin, message); + wrefresh(dispwin); + } + xasprintf(&message2, "MESSAGE \""); + while (1) { + curs_set(1); + c = wgetch(inpwin); + if (c != ERR) { + xasprintf(&message2, "%s%c", message2, (int) c); + waddch(inpwin, c); + wrefresh(inpwin); + } + + numread = 0; + maxread = 100; + numread = read(_socket, buffer, maxread); + if (numread > 0) { + curs_set(0); + buffer[numread] = '\0'; + wprint(dispwin, buffer); + wrefresh(dispwin); + wrefresh(inpwin); + } + if ((int) c == 13) { + curs_set(0); + xasprintf(&message2, "%s\"\r\n", message2); + ttt_socket_write(_socket, message2, strlen(message2)); + xasprintf(&message2, "MESSAGE \""); + maxread = 100; + numread = read(_socket, buffer, maxread); + if (numread > 0) { + buffer[numread] = '\0'; + wprint(dispwin, buffer); + } + wrefresh(dispwin); + werase(inpwin); + wrefresh(inpwin); + } + } + + + +} + + +static void finish(int sig) +{ + char buffer[1024]; + ssize_t numread; + endwin(); + + /* do your non-curses wrapup here */ + xasprintf(&message2, "QUIT\r\n"); + ttt_socket_write(_socket, message2, strlen(message2)); + maxread = 100; + numread = read(_socket, buffer, maxread); + buffer[numread] = '\0'; + xasprintf(&message2, "%s", buffer); + exit(0); +} diff --git a/src/ttt-socket.c b/src/ttt-socket.c index 20b96e2..5bf3671 100644 --- a/src/ttt-socket.c +++ b/src/ttt-socket.c @@ -166,6 +166,7 @@ ttt_socket_create_client (const char *host, ttt_status_t status; struct sockaddr_in addr; int socket; + int flags; *socket_ret = -1; @@ -178,13 +179,15 @@ ttt_socket_create_client (const char *host, shutdown (socket, SHUT_RDWR); return status; } - + flags = fcntl(socket, F_GETFL); + flags |= O_NONBLOCK; + fcntl(socket, F_SETFL, flags); *socket_ret = socket; return TTT_STATUS_SUCCESS; } /* Exported: see ttt-socket.h for documentation. */ -void +ssize_t ttt_socket_read (int socket, void *buf, size_t count) @@ -192,11 +195,8 @@ ttt_socket_read (int socket, int bytes_read; char *cbuf = buf; - while (count) { bytes_read = xread (socket, cbuf, count); - cbuf += bytes_read; - count -= bytes_read; - } + return(bytes_read); } /* Exported: see ttt-socket.h for documentation. */ diff --git a/src/ttt-socket.h b/src/ttt-socket.h index c3ca0bd..e3cd9bc 100644 --- a/src/ttt-socket.h +++ b/src/ttt-socket.h @@ -78,7 +78,7 @@ ttt_socket_create_client (const char *host, * * Errors: If any errors occur, this function does not return. */ -void +ssize_t ttt_socket_read (int socket, void *buf, size_t count);