From 86e2bb041bd52092a29e85105398afa275618072 Mon Sep 17 00:00:00 2001 From: steinarh Date: Thu, 9 Oct 2008 22:38:45 +0000 Subject: [PATCH] Buffered 'getc' from network. --- src/net.c | 52 +++++++++++++++++++++++++++++++--------------------- src/net.h | 22 +++++++++++++++++++++- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/src/net.c b/src/net.c index 1067c55..2535ba7 100644 --- a/src/net.c +++ b/src/net.c @@ -32,15 +32,7 @@ errx(int eval, const char *fmt, ...) { exit(eval); } -struct file { - int fd; - char ibuf[BUFSIZ]; - char obuf[BUFSIZ]; - int istart; - int iend; - int oend; -} sfile[2]; - +struct file sfile[2]; void net_server(int port) { @@ -54,9 +46,10 @@ net_server(int port) { if (servsock<0) errx(1, "socket: %s\n", strerror(errno)); int r; + port--; do { + serveraddr.sin_port = htons(++port); r=bind(servsock, (struct sockaddr *)&serveraddr, sizeof(serveraddr)); - serveraddr.sin_port = htons(port++); } while ((r<0) && (errno==EADDRINUSE)); if (r < 0) errx(1, "bind port %d: %s\n", port, strerror(errno)); @@ -64,7 +57,7 @@ net_server(int port) { errx(1, "listen: %s\n", strerror(errno)); #if 1 // FIXME Temporary startup. Use net_poll for this later: - printf("Waiting for connections on port %d...\n", port-1); + printf("Waiting for connections on port %d...\n", port); for (int i=0; i<2; i++) { struct sockaddr_in clientaddr; int cfd; @@ -75,6 +68,10 @@ net_server(int port) { // 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_side_printf(i, "Welcome!\n"); if (i==0) net_side_printf(i, "Waiting for another player...\n"); @@ -91,7 +88,7 @@ net_server(int port) { } -#if 1 +#if 0 int net_getc(int side) { char c; @@ -101,6 +98,9 @@ net_getc(int side) { if (n<0) return -1; return c; } +#endif + +#if 1 void net_all_flush() { } @@ -145,24 +145,34 @@ net_all_printf(const char *fmt, ...) { } while (bufp < buf+len); } } +#endif - -#else -#define net_getc2(f) (f.iendfd, 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->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_side_flush(int side) { start=0; diff --git a/src/net.h b/src/net.h index fd1576f..b9c35bb 100644 --- a/src/net.h +++ b/src/net.h @@ -5,10 +5,30 @@ #include +struct file { + int fd; + char ibuf[BUFSIZ]; + char obuf[BUFSIZ]; + int istart; + int iend; + int oend; +}; + +extern struct file sfile[2]; + extern void net_server(int port); -extern int net_getc(int side); +#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_side_printf(int side, const char *fmt, ...); extern void net_all_flush();