Removed dead code (and a few fixes)

This commit is contained in:
Steinar Hamre 2008-10-11 00:00:40 +00:00
parent 160e63e91c
commit ffa0ef6154
5 changed files with 6 additions and 222 deletions

View File

@ -1,4 +1,5 @@
CFLAGS=-g -std=gnu99 -Wall -pedantic CFLAGS=-g -std=gnu99 -Wall -pedantic
LDFLAGS=$(CFLAGS)
all: server all: server

View File

@ -3,6 +3,7 @@
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#include "net.h" #include "net.h"
#include "game.h" #include "game.h"

129
src/net.c
View File

@ -30,8 +30,6 @@ struct client clients[MAX_CLIENTS];
struct pollfd pollfds[SRV_FDS+MAX_CLIENTS]; struct pollfd pollfds[SRV_FDS+MAX_CLIENTS];
int nclients = 0; int nclients = 0;
FILE *net_side[2];
int int
errx(int eval, const char *fmt, ...) { errx(int eval, const char *fmt, ...) {
va_list ap; va_list ap;
@ -41,21 +39,11 @@ errx(int eval, const char *fmt, ...) {
exit(eval); exit(eval);
} }
/*
void
parse_line(int clientid, char *line) {
printf("line: %d \"%s\"\n", clientid, line);
}
*/
void void
parse_linetoolong(int clientid, char *line) { parse_linetoolong(int clientid, char *line) {
printf("linetoolong: %d \"%s\"\n", clientid, line); printf("linetoolong: %d \"%s\"\n", clientid, line);
} }
struct file sfile[2];
void void
net_server(int port) { net_server(int port) {
struct sockaddr_in serveraddr; struct sockaddr_in serveraddr;
@ -77,29 +65,6 @@ net_server(int port) {
errx(1, "bind port %d: %s\n", port, strerror(errno)); errx(1, "bind port %d: %s\n", port, strerror(errno));
if (listen(servsock, 10) < 0) if (listen(servsock, 10) < 0)
errx(1, "listen: %s\n", strerror(errno)); 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].fd=servsock;
pollfds[0].events=POLLIN|POLLERR|POLLHUP; pollfds[0].events=POLLIN|POLLERR|POLLHUP;
for (int i=0; i<MAX_CLIENTS; i++) { for (int i=0; i<MAX_CLIENTS; i++) {
@ -107,23 +72,9 @@ net_server(int port) {
pollfds[SRV_FDS+i].events=0; pollfds[SRV_FDS+i].events=0;
} }
printf("Waiting for connections on port %d...\n", port); printf("Waiting for connections on port %d...\n", port);
#endif
} }
#if 0
int
net_getc(int side) {
char c;
int n;
assert(side==0 || side==1);
n=read(sfile[side].fd, &c, 1);
if (n<0) return -1;
return c;
}
#endif
#if 1
void void
net_all_flush() { net_all_flush() {
} }
@ -170,80 +121,8 @@ net_all_printf(const char *fmt, ...) {
} while (bufp < buf+len); } while (bufp < buf+len);
} }
} }
#endif
#define min(a,b) ((a<b) ? (a) : (b))
int
net_getc_fillbuf(struct file *f) {
int n;
n=read(f->fd, 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_accept(int servsock);
void net_client_read(int clientid); void net_client_read(int clientid);
@ -363,11 +242,3 @@ net_client_read(int clientid) {
} }
} }
/*
int
main() {
net_server(6243);
for (;;)
net_poll();
}
*/

View File

@ -3,35 +3,14 @@
#ifndef KHET_NET_H #ifndef KHET_NET_H
#define KHET_NET_H #define KHET_NET_H
#include <stdio.h> #define MAX_CLIENTS 10
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 void net_poll(); 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_all_printf(const char *fmt, ...);
extern void net_client_printf(int side, const char *fmt, ...); extern void net_client_printf(int side, const char *fmt, ...);
extern void net_all_flush();
#endif
#endif // KHET_NET_H

View File

@ -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; game_t game;