shithub: riscv

Download patch

ref: bf6ba56817e6bb083d5c411b1660f696144d5ac6
parent: 481ae719406aa288aa28ab040e13f3f573fe6776
author: cinap_lenrek <[email protected]>
date: Mon Mar 7 15:27:34 EST 2016

aan: use sync messages as keep alives

both server and client need to be convinced that the connection
is broken for a connection reestablishment to happen as the server
will only start looking for incoming clients when the connection
already broke. so use the 8 second interval sync messages
to check for connection lifeness. if we miss two syncs in time,
we declare the connecton to be broken and will try to reconnect.

--- a/sys/src/cmd/aan.c
+++ b/sys/src/cmd/aan.c
@@ -21,14 +21,6 @@
 	Hdrsz = 3*4,
 };
 
-typedef struct Endpoints Endpoints;
-struct Endpoints {
-	char	*lsys;
-	char	*lserv;
-	char	*rsys;
-	char	*rserv;
-};
-
 typedef struct {
 	uchar	nb[4];		// Number of data bytes in this message
 	uchar	msg[4];		// Message number
@@ -35,7 +27,7 @@
 	uchar	acked[4];	// Number of messages acked
 } Hdr;
 
-typedef struct t_Buf {
+typedef struct {
 	Hdr	hdr;
 	uchar	buf[Bufsize];
 } Buf;
@@ -52,6 +44,8 @@
 static int	maxto = Maxto;
 static char	*Logname = "aan";
 static int	client;
+static int	reader = -1;
+static int	lostsync;
 
 static Alt a[] = {
 	/*	c	v	 op   */
@@ -62,7 +56,7 @@
 
 static void		fromnet(void*);
 static void		fromclient(void*);
-static void		reconnect(int);
+static int		reconnect(int);
 static void		synchronize(void);
 static int 		sendcommand(ulong, ulong);
 static void		showmsg(int, char *, Buf *);
@@ -77,6 +71,7 @@
 	exits("usage");
 }
 
+ 
 static int
 catch(void *, char *s)
 {
@@ -107,6 +102,7 @@
 	vlong synctime;
 	int i, n, failed;
 	Channel *timer;
+	Hdr hdr;
 	Buf *b;
 
 	ARGBEGIN {
@@ -151,8 +147,7 @@
 	 * long if there is some general connection problem
 	 * (like NAT).
 	 */
-	netfd = -1;
-	reconnect(60);
+	netfd = reconnect(60);
 
 	unsent = chancreate(sizeof(Buf *), Nbuf);
 	unacked = chancreate(sizeof(Buf *), Nbuf);
@@ -164,7 +159,8 @@
 	for (i = 0; i < Nbuf; i++)
 		sendp(empty, emalloc(sizeof(Buf)));
 
-	if (proccreate(fromnet, nil, Stacksize) < 0)
+	reader = proccreate(fromnet, nil, Stacksize);
+	if (reader < 0)
 		sysfatal("Cannot start fromnet; %r");
 
 	if (proccreate(fromclient, nil, Stacksize) < 0)
@@ -177,24 +173,29 @@
 	a[Unsent].c = unsent;
 	a[Unsent].v = &b;
 
+Restart:
 	synctime = nsec() + Synctime;
 	failed = 0;
+	lostsync = 0;
 	while (!done) {
 		if (failed) {
 			// Wait for the netreader to die.
 			while (netfd >= 0) {
 				dmessage(1, "main; waiting for netreader to die\n");
+				threadint(reader);
 				sleep(1000);
 			}
 
 			// the reader died; reestablish the world.
-			reconnect(maxto);
+			netfd = reconnect(maxto);
 			synchronize();
-			failed = 0;
+			goto Restart;
 		}
 
-		if (nsec() >= synctime) {
-			Hdr hdr;
+		switch (alt(a)) {
+		case Timer:
+			if (netfd < 0 || nsec() < synctime)
+				break;
 
 			PBIT32(hdr.nb, 0);
 			PBIT32(hdr.acked, inmsg);
@@ -205,11 +206,13 @@
 				failed = 1;
 				continue;
 			}
-			synctime = nsec() + Synctime;
-		}
 
-		switch (alt(a)) {
-		case Timer:
+			if(++lostsync > 2){
+				dmessage(2, "main; lost sync\n");
+				failed = 1;
+				continue;
+			}
+			synctime = nsec() + Synctime;
 			break;
 
 		case Unsent:
@@ -268,10 +271,13 @@
 static void
 fromnet(void*)
 {
+	extern void _threadnote(void *, char *);
 	static int lastacked;
 	int n, m, len, acked;
 	Buf *b;
 
+	notify(_threadnote);
+
 	threadsetname("fromnet");
 
 	b = emalloc(sizeof(Buf));
@@ -294,6 +300,7 @@
 			netfd = -1;
 			continue;
 		}
+		lostsync = 0;	// reset timeout
 		n = GBIT32(b->hdr.nb);
 		m = GBIT32(b->hdr.msg);
 		acked = GBIT32(b->hdr.acked);
@@ -353,7 +360,7 @@
 	done = 1;
 }
 
-static void
+static int
 reconnect(int secs)
 {
 	NetConnInfo *nci;
@@ -395,9 +402,8 @@
 		freenetconninfo(nci);
 	} else
 		syslog(0, Logname, "connected");
-	
-	// Wakes up the netreader.
-	netfd = fd;
+
+	return fd;
 }
 
 static void