diff options
author | rodri <rgl@antares-labs.eu> | 2021-07-21 19:31:36 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2021-07-21 19:31:36 +0000 |
commit | 10f8e5542686760b772abf40929a51fb63afad93 (patch) | |
tree | ca8bc50957ade5f4846f6b77dedfeb0c37cda4be /muswd.c | |
parent | d276cd9961e05c184d4fd653bb9e92a288a09ec3 (diff) | |
download | musw-10f8e5542686760b772abf40929a51fb63afad93.tar.gz musw-10f8e5542686760b772abf40929a51fb63afad93.tar.bz2 musw-10f8e5542686760b772abf40929a51fb63afad93.zip |
implemented very basic server broadcasting.
got rid of unnecessary (at least for now) code.
brought some e-funcs.
Diffstat (limited to 'muswd.c')
-rw-r--r-- | muswd.c | 31 |
1 files changed, 24 insertions, 7 deletions
@@ -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); } |