shithub: musw

Download patch

ref: 941d146cea7d0c2ce872e599622a5c9e01d37fe2
parent: 9712ec712cc6a57d416ef2b1e86d15ef161016df
author: rodri <rgl@antares-labs.eu>
date: Sat Mar 4 18:32:47 EST 2023

implemented client reconnection loop.

currently not working. there seems to be a problem with 9front's udp stack. needs investigation.

--- a/dat.h
+++ b/dat.h
@@ -57,6 +57,8 @@
 
 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;
@@ -97,6 +99,22 @@
 
 	void (*step)(Sprite*, ulong);
 	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
--- 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 @@
 VModel *needlemdl, *wedgemdl;
 Image *screenb;
 Image *skymap;
+Scene *curscene;
 Channel *ingress;
 Channel *egress;
 NetConn netconn;
@@ -311,7 +305,7 @@
 		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 @@
 void
 threadmain(int argc, char *argv[])
 {
-	uvlong then, now;
+	uvlong then, now, lastpktsent;
 	double frametime;
 	char *server;
 	int fd;
@@ -625,6 +619,7 @@
 	threadcreate(threadresize, mc, mainstacksize);
 
 	then = nanosec();
+	lastpktsent = 0;
 	io = ioproc();
 	for(;;){
 		now = nanosec();
@@ -631,12 +626,18 @@
 		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));
 	}
--- a/muswd.c
+++ b/muswd.c
@@ -98,14 +98,22 @@
 		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 @@
 		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);
 		}
 	}
--- 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.