Buffered 'getc' from network.

This commit is contained in:
Steinar Hamre 2008-10-09 22:38:45 +00:00
parent 2926da45ac
commit 86e2bb041b
2 changed files with 52 additions and 22 deletions

View File

@ -32,15 +32,7 @@ errx(int eval, const char *fmt, ...) {
exit(eval); exit(eval);
} }
struct file { struct file sfile[2];
int fd;
char ibuf[BUFSIZ];
char obuf[BUFSIZ];
int istart;
int iend;
int oend;
} sfile[2];
void void
net_server(int port) { net_server(int port) {
@ -54,9 +46,10 @@ net_server(int port) {
if (servsock<0) if (servsock<0)
errx(1, "socket: %s\n", strerror(errno)); errx(1, "socket: %s\n", strerror(errno));
int r; int r;
port--;
do { do {
serveraddr.sin_port = htons(++port);
r=bind(servsock, (struct sockaddr *)&serveraddr, sizeof(serveraddr)); r=bind(servsock, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
serveraddr.sin_port = htons(port++);
} while ((r<0) && (errno==EADDRINUSE)); } while ((r<0) && (errno==EADDRINUSE));
if (r < 0) if (r < 0)
errx(1, "bind port %d: %s\n", port, strerror(errno)); 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)); errx(1, "listen: %s\n", strerror(errno));
#if 1 #if 1
// FIXME Temporary startup. Use net_poll for this later: // 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++) { for (int i=0; i<2; i++) {
struct sockaddr_in clientaddr; struct sockaddr_in clientaddr;
int cfd; int cfd;
@ -75,6 +68,10 @@ net_server(int port) {
// FIXME log, shout, or something // FIXME log, shout, or something
printf("client log on...\n"); printf("client log on...\n");
sfile[i].fd=cfd; sfile[i].fd=cfd;
sfile[i].istart=0;
sfile[i].iend=0;
sfile[i].oend=0;
net_side_printf(i, "Welcome!\n"); net_side_printf(i, "Welcome!\n");
if (i==0) if (i==0)
net_side_printf(i, "Waiting for another player...\n"); net_side_printf(i, "Waiting for another player...\n");
@ -91,7 +88,7 @@ net_server(int port) {
} }
#if 1 #if 0
int int
net_getc(int side) { net_getc(int side) {
char c; char c;
@ -101,6 +98,9 @@ net_getc(int side) {
if (n<0) return -1; if (n<0) return -1;
return c; return c;
} }
#endif
#if 1
void void
net_all_flush() { net_all_flush() {
} }
@ -145,24 +145,34 @@ net_all_printf(const char *fmt, ...) {
} while (bufp < buf+len); } while (bufp < buf+len);
} }
} }
#endif
int
#else net_getc_fillbuf(struct file *f) {
#define net_getc2(f) (f.iend<BUFSIZ||net_fillbuf(f), f.ibuf[f.iend++]) int n;
#define net_getc(side) net_getc2(sfile[side]) n=read(f->fd, f->ibuf, BUFSIZ);
void
net_fillbuf(struct file f) {
n=read(f.fd, f.ibuf, BUFSIZ);
if (n==0) if (n==0)
errx(1, "end of file\n"); errx(1, "end of file\n");
if (n<0) if (n<0)
errx(1, "read: %s\n", strerror(errno)); 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 void
net_side_flush(int side) { net_side_flush(int side) {
start=0; start=0;

View File

@ -5,10 +5,30 @@
#include <stdio.h> #include <stdio.h>
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 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_all_printf(const char *fmt, ...);
extern void net_side_printf(int side, const char *fmt, ...); extern void net_side_printf(int side, const char *fmt, ...);
extern void net_all_flush(); extern void net_all_flush();