wm: glendy

Download patch

ref: 413720140ffd10768af41ea2668daec4b5684353
parent: d4470e0e463f45f155442ac599541184ae041c56
author: mkf <mkf@cloud9p.org>
date: Thu Jun 6 09:09:01 EDT 2024

srv: clean up code, fix some memory leaks

--- a/engine.h
+++ b/engine.h
@@ -36,6 +36,7 @@
 	Playing,
 	Won,	
 	Lost,
+	Finished, /* for server */
 
 	Prev = 100,
 	Wall = 999,
--- a/srv5.c
+++ b/srv5.c
@@ -54,6 +54,12 @@
 	shutdown(g->sockfd[1], SHUT_RDWR);
 	close(g->sockfd[0]);
 	close(g->sockfd[1]);
+	
+	if(g->state != Finished)
+		g->state = Finished;
+	else
+	/* we can't delete whole list because we need to update gid for rest of games too! */
+		free(g);
 }
 
 static void
@@ -331,6 +337,7 @@
 	memset(s, 0, INPUTSIZE);
 	
 	g = (Game*)lookup(games, gid);
+	
 	/* we could use local variables, but that not worth the trouble */
 	while((c = read(g->sockfd[player], s+n, 1) == 1) && n < INPUTSIZE)
 	{
@@ -344,7 +351,7 @@
 		}
 		n++;
 	}
-	if(!strcmp(s, ""))
+	if(strcmp(s, ""))
 		dprint("input(%d, %d): got input: 0x%x, %s\n", gid, player, *s, s);
 
 	return s;
@@ -417,12 +424,8 @@
 		/* most of time is spent here */
 		s = input(gid, player);
 		
-
 		if(!strcmp(s, ""))
-		{
-			cleanup(gid);
 			break;
-		}
 		
 		pthread_mutex_lock(&game_lock);
 		
@@ -433,6 +436,7 @@
 		pthread_mutex_unlock(&game_lock);
 		free(s);
 	}
+	cleanup(gid);
 	free(data);
 }
 static void