ref: a5c44d9c396b02c013c9d76709382d7e67aee458
parent: b8379bd4d9bd65ae85c7d08554dc6ded0d9e1b7d
author: rodri <rgl@antares-labs.eu>
date: Wed Jun 8 03:29:14 EDT 2022
changed the transport from tcp to udp. got rid of some unnecessary connection management and party setup code—needs further refactoring. fixed some nomenclatures.
--- a/lobby.c
+++ b/lobby.c
@@ -83,7 +83,7 @@
Lobby *l;
l = emalloc(sizeof(Lobby));
- memset(l, 0, sizeof(*l));
+ memset(l, 0, sizeof(Lobby));
l->takeseat = lobby_takeseat;
l->getcouple = lobby_getcouple;
l->leaveseat = lobby_leaveseat;
--- a/musw.c
+++ b/musw.c
@@ -68,7 +68,7 @@
if(bin == nil)
sysfatal("Bopen: %r");
- mdl = emalloc(sizeof *mdl);
+ mdl = emalloc(sizeof(VModel));
mdl->pts = nil;
mdl->npts = 0;
mdl->strokefmt = nil;
@@ -354,6 +354,8 @@
kchan = chancreate(sizeof kdown, 1);
proccreate(kbdproc, nil, 4096);
+ /* TODO: draw a CONNECTING... sign */
+ /* TODO: set up an alarm for n secs and update the sign */
fd = dial(server, nil, nil, nil);
if(fd < 0)
sysfatal("dial: %r");
--- a/muswd.c
+++ b/muswd.c
@@ -1,5 +1,6 @@
#include <u.h>
#include <libc.h>
+#include <ip.h>
#include <thread.h>
#include <draw.h>
#include "libgeometry/geometry.h"
@@ -11,75 +12,30 @@
Lobby *lobby;
-static long
-_iolisten(va_list *arg)
-{
- char *adir, *ldir;
-
- adir = va_arg(*arg, char*);
- ldir = va_arg(*arg, char*);
-
- return listen(adir, ldir);
-}
-
-long
-iolisten(Ioproc *io, char *adir, char *ldir)
-{
- return iocall(io, _iolisten, adir, ldir);
-}
-
void
-procnetrecv(void *arg)
+threadlisten(void *arg)
{
- uchar buf[1024];
+ uchar buf[1024], *p, *e;
int fd, n;
ulong kdown;
Ioproc *io;
+ Udphdr *udp;
fd = *(int*)arg;
io = ioproc();
while((n = ioread(io, fd, buf, sizeof buf)) > 0){
- unpack(buf, n, "k", &kdown);
- fprint(2, "%d (%d) [%d] rcvd %.*lub\n", threadid(), threadpid(threadid()), getpid(), sizeof(kdown)*8, kdown);
- }
- closeioproc(io);
- threadexits(nil);
-}
-
-void
-threadlisten(void *arg)
-{
- int lcfd, dfd, pid;
- char *adir, ldir[40];
- Ioproc *io;
-
- adir = arg;
- io = ioproc();
-
- for(;;){
- lcfd = iolisten(io, adir, ldir);
- if(lcfd < 0){
- fprint(2, "iolisten: %r\n");
+ if(n < Udphdrsize)
continue;
- }
- /*
- * handle connection and allocate user on a seat, ready
- * to play
- */
- dfd = accept(lcfd, ldir);
- if(dfd < 0){
- fprint(2, "accept: %r\n");
- continue;
- }
- lobby->takeseat(lobby, ldir, lcfd, dfd);
+ udp = (Udphdr*)buf;
+ p = buf+Udphdrsize;
+ e = buf+n;
- pid = proccreate(procnetrecv, &dfd, 4096);
-
- if(debug)
- fprint(2, "forked %d for new conn\n", pid);
- }
+ unpack(p, e-p, "k", &kdown);
+ fprint(2, "%I → %I | %d (%d) rcvd %.*lub\n",
+ udp->raddr, udp->laddr, threadid(), getpid(), sizeof(kdown)*8, kdown);}
+ closeioproc(io);
}
void
@@ -234,11 +190,12 @@
void
threadmain(int argc, char *argv[])
{
- int acfd;
- char adir[40], *addr;
+ int acfd, adfd;
+ char adir[40], *addr, aux[64];
GEOMfmtinstall();
- addr = "tcp!*!112"; /* for testing. will work out udp soon */
+ fmtinstall('I', eipfmt);
+ addr = "udp!*!112";
ARGBEGIN{
case 'a':
addr = EARGF(usage());
@@ -256,6 +213,15 @@
if(acfd < 0)
sysfatal("announce: %r");
+ /* we don't want a line per client. we want it RAW */
+ if(fprint(acfd, "headers") < 0)
+ sysfatal("couldn't set udp headers option: %r");
+
+ snprint(aux, sizeof aux, "%s/data", adir);
+ adfd = open(aux, ORDWR);
+ if(adfd < 0)
+ sysfatal("open: %r");
+
if(debug)
fprint(2, "listening on %s\n", addr);
@@ -263,7 +229,7 @@
inittheparty();
threadcreate(threadC2, nil, 4096);
- threadcreate(threadlisten, adir, 4096);
+ threadcreate(threadlisten, &adfd, 4096);
threadcreate(threadsim, nil, 4096);
threadexits(nil);
}