wm: glendy

Download patch

ref: 0dda52b4df41b397787f89a9076d1fe158896e4b
parent: b3cc83d39291d1d4f91bd899e5d7a085cfd5a924
author: mkf <mkf@cloud9p.org>
date: Sun May 19 13:44:30 EDT 2024

bunch of bug fixes

don't read on closed sockets
add cleanup() for easier clean up.
ensure thread quits and be sure gcount doesn't go negative
don't print empty (and just newline) lines
fix a bug where it would take one extra turn to declare game as over

--- a/srv4.c
+++ b/srv4.c
@@ -42,8 +42,19 @@
 }
 
 static void
+cleanup(int game)
+{
+	dprint("cleanup(%d)\n", game);
+	close(games[game].sockfd[0]);
+	close(games[game].sockfd[1]);
+	if(game > 0)
+		gcount--;	
+}
+
+static void
 printclients(char *fmt, ...)
 {
+	int i;
 	/* it seems arg gets changed during the first call to vprint, thus we need two */
 	va_list arg, arg2;
 	
@@ -151,6 +162,7 @@
 	}
 	else
 	{
+		printclients("SYNC %d %s\n", turn, syncmsg);
 		if(state == Won)
 		{
 			dprint("hah, trapper won\n");
@@ -163,8 +175,7 @@
 			fprint(sockfd[0], "LOST\n");
 			fprint(sockfd[1], "WON\n");
 		}
-		close(sockfd[0]);
-		close(sockfd[1]);
+		cleanup(id);
 	}
 	dprint("TURN is %d\n", turn);
 	fprint(playersock, "TURN\n");
@@ -273,9 +284,7 @@
 		fprint(sockfd[!player], "DIE other client have been disconnected\n");
 		
 		/* mmhm... what happens if we close a fd we are reading from? */
-		close(sockfd[0]);
-		close(sockfd[1]);
-		gcount--;
+		cleanup(id);
 		return Err;
 	}
 	else if(turn % 2 != player)
@@ -320,11 +329,11 @@
 	int n = 0;
 	
 	/* sang bozorg */
-	s = malloc(1024);
+	s = emalloc(1024);
 	memset(s, 0, 1024);
 	
 	/* we could use local variables, but that not worth the trouble */
-	while(read(games[game].sockfd[player], s+n, 1) == 1 && n < 1024)
+	while((c = read(games[game].sockfd[player], s+n, 1) == 1) && n < 1024)
 	{
 		if(s[n] == '\n' || s[n] == '\0')
 		{
@@ -333,7 +342,8 @@
 		}
 		n++;
 	}
-	dprint("got input: %s\n", s);
+	if(!strcmp(s, ""))
+		dprint("got input: %s\n", s);
 
 	return s;
 }
@@ -365,7 +375,7 @@
 setgame(int n)
 {
 	if(n > sizeof(games) / sizeof(Game))
-		sysfatal("setgame(): invalid game");
+		sysfatal("setgame(%d): invalid game", n);
 	
 	/* id got to change when we set game */
 	id = n;
@@ -384,7 +394,7 @@
 static void
 clienthandler(void *data)
 {
-	int player, game;
+	int res, player, game;
 	char *s;
 	
 	game = ((int*)data)[0];
@@ -395,10 +405,17 @@
 		/* most of time is spent here */
 		s = input(game, player);
 		
+
+		if(!strcmp(s, ""))
+		{
+			cleanup(game);
+			break;
+		}
+		
 		pthread_mutex_lock(&game_lock);
 		
 		loadgame(game);
-		proc(player, s);
+		res = proc(player, s);
 		setgame(game);
 		
 		pthread_mutex_unlock(&game_lock);