From 10f8e5542686760b772abf40929a51fb63afad93 Mon Sep 17 00:00:00 2001 From: rodri Date: Wed, 21 Jul 2021 19:31:36 +0000 Subject: implemented very basic server broadcasting. got rid of unnecessary (at least for now) code. brought some e-funcs. --- alloc.c | 44 +++++++++++++++++++++++++++++++++++++++++ dat.h | 15 ++------------ fns.h | 15 ++++++++++++++ mkfile | 5 ++++- musw.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ muswd.c | 31 ++++++++++++++++++++++------- readme.md | 3 +++ stats.c | 18 ----------------- 8 files changed, 159 insertions(+), 39 deletions(-) create mode 100644 alloc.c delete mode 100644 stats.c diff --git a/alloc.c b/alloc.c new file mode 100644 index 0000000..037d726 --- /dev/null +++ b/alloc.c @@ -0,0 +1,44 @@ +#include +#include +#include + +void* +emalloc(ulong n) +{ + void *p; + + p = malloc(n); + if(p == nil) + sysfatal("malloc: %r"); + setmalloctag(p, getcallerpc(&n)); + return p; +} + +void* +erealloc(void *p, ulong n) +{ + void *np; + + np = realloc(p, n); + if(np == nil){ + if(n == 0) + return nil; + sysfatal("realloc: %r"); + } + if(p == nil) + setmalloctag(np, getcallerpc(&p)); + else + setrealloctag(np, getcallerpc(&p)); + return np; +} + +//Image* +//eallocimage(Display *d, Rectangle r, ulong chan, int repl, ulong col) +//{ +// Image *i; +// +// i = allocimage(d, r, chan, repl, col); +// if(i == nil) +// sysfatal("allocimage: %r"); +// return i; +//} diff --git a/dat.h b/dat.h index c93b24e..97e0719 100644 --- a/dat.h +++ b/dat.h @@ -1,22 +1,11 @@ +#define FPS2MS(fps) (1000/(fps)) + typedef struct GameState GameState; typedef struct Derivative Derivative; -typedef struct Stats Stats; -typedef struct Sprite Sprite; - -struct Stats -{ - double cur; - double total; - double min, avg, max; - uvlong nupdates; - - void (*update)(Stats*, double); -}; struct GameState { double x, v; - Stats stats; }; struct Derivative diff --git a/fns.h b/fns.h index f46c8bd..d94ff5b 100644 --- a/fns.h +++ b/fns.h @@ -1 +1,16 @@ +/* + * alloc + */ +void *emalloc(ulong); +void *erealloc(void*, ulong); +//Image *eallocimage(Display*, Rectangle, ulong, int, ulong); + +/* + * physics + */ +void integrate(GameState*, double, double); + +/* + * nanosec + */ uvlong nanosec(void); diff --git a/mkfile b/mkfile index 6579d68..c3b9295 100644 --- a/mkfile +++ b/mkfile @@ -5,7 +5,10 @@ TARG=\ musw\ muswd\ -OFILES= +OFILES=\ + alloc.$O\ + physics.$O\ + nanosec.$O\ HFILES=\ dat.h\ diff --git a/musw.c b/musw.c index e69de29..f58765c 100644 --- a/musw.c +++ b/musw.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include "dat.h" +#include "fns.h" + +void +threadnetin(void *arg) +{ + char buf[256]; + int fd, n; + Ioproc *io; + + fd = *((int*)arg); + io = ioproc(); + + while((n = ioread(io, fd, buf, sizeof buf)) > 0) + if(iowrite(io, 1, buf, n) != n) + fprint(2, "iowrite: %r\n"); + closeioproc(io); +} + +void +threadnetout(void *arg) +{ + char buf[256]; + int fd, n; + Ioproc *io; + + fd = *((int*)arg); + io = ioproc(); + + while((n = ioread(io, 0, buf, sizeof buf)) > 0) + if(iowrite(io, fd, buf, n) != n) + fprint(2, "iowrite: %r\n"); + closeioproc(io); +} + +void +usage(void) +{ + fprint(2, "usage: %s [-d] server\n", argv0); + threadexitsall("usage"); +} + +void +threadmain(int argc, char *argv[]) +{ + char *server; + int fd; + + ARGBEGIN{ + default: + usage(); + }ARGEND; + if(argc != 1) + usage(); + server = argv[0]; + + fd = dial(server, nil, nil, nil); + if(fd < 0) + sysfatal("dial: %r"); + + threadcreate(threadnetin, &fd, 4096); + threadcreate(threadnetout, &fd, 4096); + threadexits(nil); +} diff --git a/muswd.c b/muswd.c index 983470b..4ebdb5e 100644 --- a/muswd.c +++ b/muswd.c @@ -6,8 +6,12 @@ int debug; +GameState state; double t, Δt; +int *conns; +int nconns; + static long _iolisten(va_list *arg) { @@ -28,7 +32,7 @@ iolisten(Ioproc *io, char *adir, char *ldir) void threadlisten(void *arg) { - int lcfd; + int lcfd, dfd; char *adir, ldir[40]; Ioproc *io; @@ -45,27 +49,37 @@ threadlisten(void *arg) * handle connection and allocate user on a seat, ready * to play */ + dfd = accept(lcfd, ldir); + if(dfd < 0){ + fprint(2, "accept: %r\n"); + continue; + } + + conns = erealloc(conns, ++nconns*sizeof(*conns)); + conns[nconns-1] = dfd; } } void resetsim(void) { - memset(&state, 0, sizeof(GameState)); - state.x = 100; - state.stats.update = statsupdate; t = 0; + memset(&state, 0, sizeof state); + state.x = 100; } void threadsim(void *) { + int i; uvlong then, now; double frametime, timeacc; + Ioproc *io; Δt = 0.01; then = nanosec(); timeacc = 0; + io = ioproc(); resetsim(); @@ -75,13 +89,16 @@ threadsim(void *) then = now; timeacc += frametime/1e9; + for(i = 0; i < nconns; i++) + fprint(conns[i], "state: x=%g v=%g\n", state.x, state.v); + while(timeacc >= Δt){ integrate(&state, t, Δt); timeacc -= Δt; t += Δt; } - sleep(66); + iosleep(io, FPS2MS(1)); } } @@ -112,7 +129,7 @@ threadmain(int argc, char *argv[]) if(acfd < 0) sysfatal("announce: %r"); - threadcreate(threadlisten, adir, 1024); - threadcreate(threadsim, nil, 8192); + threadcreate(threadlisten, adir, 4096); + threadcreate(threadsim, nil, 4096); threadexits(nil); } diff --git a/readme.md b/readme.md index e69de29..4ce9321 100644 --- a/readme.md +++ b/readme.md @@ -0,0 +1,3 @@ +# musw + +_Musw_ is the multi-user Spacewar! diff --git a/stats.c b/stats.c deleted file mode 100644 index 2232937..0000000 --- a/stats.c +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include -#include -#include "dat.h" -#include "fns.h" - -static double min(double a, double b) { return a < b? a: b; } -static double max(double a, double b) { return a > b? a: b; } - -void -statsupdate(Stats *s, double n) -{ - s->cur = n; - s->total += s->cur; - s->avg = s->total/++s->nupdates; - s->min = min(s->cur, s->min); - s->max = max(s->cur, s->max); -} -- cgit v1.2.3