ref: 10f8e5542686760b772abf40929a51fb63afad93
parent: d276cd9961e05c184d4fd653bb9e92a288a09ec3
author: rodri <rgl@antares-labs.eu>
date: Wed Jul 21 15:31:36 EDT 2021
implemented very basic server broadcasting. got rid of unnecessary (at least for now) code. brought some e-funcs.
--- /dev/null
+++ b/alloc.c
@@ -1,0 +1,44 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+
+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;
+//}
--- 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
--- a/fns.h
+++ b/fns.h
@@ -1,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);
--- a/mkfile
+++ b/mkfile
@@ -5,7 +5,10 @@
musw\
muswd\
-OFILES=
+OFILES=\
+ alloc.$O\
+ physics.$O\
+ nanosec.$O\
HFILES=\
dat.h\
--- a/musw.c
+++ b/musw.c
@@ -1,0 +1,67 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#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);
+}
--- 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 @@
void
threadlisten(void *arg)
{
- int lcfd;
+ int lcfd, dfd;
char *adir, ldir[40];
Ioproc *io;
@@ -45,6 +49,14 @@
* 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;
}
}
@@ -51,21 +63,23 @@
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,6 +89,9 @@
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;
@@ -81,7 +98,7 @@
t += Δt;
}
- sleep(66);
+ iosleep(io, FPS2MS(1));
}
}
@@ -112,7 +129,7 @@
if(acfd < 0)
sysfatal("announce: %r");
- threadcreate(threadlisten, adir, 1024);
- threadcreate(threadsim, nil, 8192);
+ threadcreate(threadlisten, adir, 4096);
+ threadcreate(threadsim, nil, 4096);
threadexits(nil);
}
--- a/readme.md
+++ b/readme.md
@@ -1,0 +1,3 @@
+# musw
+
+_Musw_ is the multi-user Spacewar!
--- a/stats.c
+++ /dev/null
@@ -1,18 +1,0 @@
-#include <u.h>
-#include <libc.h>
-#include <draw.h>
-#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);
-}