From 941d146cea7d0c2ce872e599622a5c9e01d37fe2 Mon Sep 17 00:00:00 2001
From: rodri <rgl@antares-labs.eu>
Date: Sat, 4 Mar 2023 23:32:47 +0000
Subject: implemented client reconnection loop.

currently not working. there seems to be a problem with 9front's udp stack. needs investigation.
---
 dat.h   | 18 ++++++++++++++++++
 musw.c  | 25 +++++++++++++------------
 muswd.c | 26 +++++++++++++++++---------
 todo    |  3 ++-
 4 files changed, 50 insertions(+), 22 deletions(-)

diff --git a/dat.h b/dat.h
index 9a27f17..67fcf3b 100644
--- a/dat.h
+++ b/dat.h
@@ -57,6 +57,8 @@ enum {
 
 typedef struct VModel VModel;
 typedef struct Sprite Sprite;
+typedef struct Keymap Keymap;
+typedef struct Scene Scene;
 typedef struct Particle Particle;
 typedef struct Bullet Bullet;
 typedef struct Ship Ship;
@@ -99,6 +101,22 @@ struct Sprite
 	void (*draw)(Sprite*, Image*, Point);
 };
 
+struct Keymap
+{
+	Rune key;
+	KeyOp op;
+};
+
+struct Scene
+{
+	char *title;
+	int state;
+
+	Scene *(*δ)(Scene*);
+	void (*step)(Scene*);
+	void (*draw)(Scene*);
+};
+
 struct Particle
 {
 	Point2 p, v;
diff --git a/musw.c b/musw.c
index 32bf155..ea3803a 100644
--- a/musw.c
+++ b/musw.c
@@ -12,13 +12,6 @@
 #include "dat.h"
 #include "fns.h"
 
-typedef struct Keymap Keymap;
-struct Keymap
-{
-	Rune key;
-	KeyOp op;
-};
-
 Keymap kmap[] = {
 	{.key = Kup,	.op = K↑},
 	{.key = Kleft,	.op = K↺},
@@ -38,6 +31,7 @@ Universe *universe;
 VModel *needlemdl, *wedgemdl;
 Image *screenb;
 Image *skymap;
+Scene *curscene;
 Channel *ingress;
 Channel *egress;
 NetConn netconn;
@@ -311,7 +305,7 @@ threadnetrecv(void *arg)
 		if(debug){
 			rport = frame->udp.rport[0]<<8 | frame->udp.rport[1];
 			lport = frame->udp.lport[0]<<8 | frame->udp.lport[1];
-			fprint(2, "%I!%ud → %I!%ud | rcvd %Φ\n",
+			fprint(2, "%I!%ud ← %I!%ud | rcvd %Φ\n",
 				frame->udp.laddr, lport, frame->udp.raddr, rport, frame);
 		}
 	}
@@ -557,7 +551,7 @@ usage(void)
 void
 threadmain(int argc, char *argv[])
 {
-	uvlong then, now;
+	uvlong then, now, lastpktsent;
 	double frametime;
 	char *server;
 	int fd;
@@ -625,19 +619,26 @@ threadmain(int argc, char *argv[])
 	threadcreate(threadresize, mc, mainstacksize);
 
 	then = nanosec();
+	lastpktsent = 0;
 	io = ioproc();
 	for(;;){
 		now = nanosec();
 		frametime = now - then;
 		then = now;
 
+		if(netconn.state != NCSConnected)
+			lastpktsent += frametime/1e6;
+
+		if(netconn.state == NCSDisconnected ||
+		  (netconn.state == NCSConnecting && lastpktsent >= 1000)){
+			initconn();
+			lastpktsent = 0;
+		}
+
 		universe->star.spr->step(universe->star.spr, frametime/1e6);
 
 		redraw();
 
-		if(netconn.state == NCSDisconnected)
-			initconn();
-
 		iosleep(io, HZ2MS(30));
 	}
 }
diff --git a/muswd.c b/muswd.c
index fd4f183..cc5c73a 100644
--- a/muswd.c
+++ b/muswd.c
@@ -98,14 +98,22 @@ nudgeconns(ulong curts)
 		elapsed = curts - (*ncp)->lastrecvts;
 		elapsednudge = curts - (*ncp)->lastnudgets;
 
-		if((*ncp)->state == NCSConnected && elapsed > ConnTimeout)
-			popconn(*ncp);
-		else if((*ncp)->state == NCSConnected && elapsednudge > 1000){ /* every second */
-			f = newframe(&(*ncp)->udp, NSnudge, (*ncp)->lastseq+1, 0, 0, nil);
-			signframe(f, (*ncp)->dh.priv);
-			sendp(egress, f);
-
-			(*ncp)->lastnudgets = curts;
+		switch((*ncp)->state){
+		case NCSConnected:
+			if(elapsed > ConnTimeout)
+				popconn(*ncp);
+			else if(elapsednudge > 1000){ /* every second */
+				f = newframe(&(*ncp)->udp, NSnudge, (*ncp)->lastseq+1, 0, 0, nil);
+				signframe(f, (*ncp)->dh.priv);
+				sendp(egress, f);
+	
+				(*ncp)->lastnudgets = curts;
+			}
+			break;
+		case NCSConnecting:
+			if(elapsed > ConnTimeout)
+				popconn(*ncp);
+			break;
 		}
 	}
 }
@@ -132,7 +140,7 @@ threadnetrecv(void *arg)
 		if(debug){
 			rport = frame->udp.rport[0]<<8 | frame->udp.rport[1];
 			lport = frame->udp.lport[0]<<8 | frame->udp.lport[1];
-			fprint(2, "%I!%ud → %I!%ud | rcvd %Φ\n",
+			fprint(2, "%I!%ud ← %I!%ud | rcvd %Φ\n",
 				frame->udp.laddr, lport, frame->udp.raddr, rport, frame);
 		}
 	}
diff --git a/todo b/todo
index bfc0a3d..6827ae2 100644
--- a/todo
+++ b/todo
@@ -13,4 +13,5 @@
 	[ ] waiting for a player
 	[ ] main game
 [ ] reduce the amount of data sent on every NSsimstate packet
-[ ] the client must try to connect continously
+[?] the client must try to connect continously
+	> there's an error in the udp stack that doesn't allow the client to receive packets if run before the server is up.
-- 
cgit v1.2.3