wm: glendy

Download patch

ref: 9536bd99b2b99d771d88210b08ee9862b986dfe2
parent: 1c674256a11419bbb84d9ec9a021111800b6369e
author: mkf <mkf@cloud9p.org>
date: Sat Jun 8 17:25:28 EDT 2024

add keepalive code

--- a/srv5.c
+++ b/srv5.c
@@ -13,6 +13,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <ctype.h>
+#include <signal.h>
 
 #include "unix.h"
 #include "engine.h"
@@ -20,7 +21,7 @@
 #include "srv.h"
 
 int listenfd;
-int port = 1768;
+int port = 1769;
 int debug = 1;
 
 char syncmsg[8];
@@ -53,6 +54,8 @@
 	
 	g = (Game*)lookup(games, gid);
 	
+	if(g == nil)
+		return;
 	if(g->state != Finished)
 	{
 		g->state = Finished;
@@ -69,6 +72,7 @@
 		free(g->clients[1]->nick);
 		free(g->clients[1]);
 		free(g);
+		g = nil;
 	}
 }
 
@@ -474,11 +478,15 @@
 	
 	pthread_mutex_lock(&game_lock);
 	gcount++;
-	
+
 	sockfd[0] = c1->fd;
 	sockfd[1] = c2->fd;
 
+	c1->state = Init;
+	c2->state = Init;
+
 	g = (Game*)emalloc(sizeof(Game));
+
 	llappend(games, g);
 	initlevel();
 	setgame(gcount);
@@ -532,6 +540,7 @@
 		goto die;
 	}
 	
+	/* this is wrong, but we run strdup anyway */
 	if(!strcmp(nick, "@"))
 		nick = "Guest";
 	
@@ -611,7 +620,8 @@
 	c->opts = parseopts(fd, opts);
 	c->firstseen = time(nil);
 	c->lastseen = c->firstseen;
-	
+	c->state = Connect;
+
 	if(c->nick == nil || c->game == -1 || c->side == -1)
 	{
 		free(c);
@@ -623,6 +633,35 @@
 	return c;
 }
 
+
+void
+checkquene(void)
+{
+	Client *c;
+	long t;
+
+	t = time(nil);
+
+	dprint("checkquene(%d)\n", t);
+
+	if(clients.len == 0)
+		return;
+
+	for(List *l = clients.head ; l != nil ; l = l->next)
+	{
+		c = (Client*)l->data;
+		if(c->lastseen + TIMEOUT < t)
+		{
+			dprint("checkquene(): found match t: %d lastseen: %d nick: %s fd: %d\n",
+			t, c->lastseen, c->nick, c->fd);
+			free(c->nick);
+		//	free(c->thread);
+			qdel(&clients, l);
+			free(c);
+		}
+	}
+}
+
 int
 makematch(Client *c)
 {
@@ -631,6 +670,8 @@
 	dprint("makematch(%p)\n", c);
 	pthread_mutex_lock(&game_lock);
 	
+	checkquene();
+
 	if(clients.head == nil)
 	{
 		dprint("clients.head == nil\n");
@@ -668,6 +709,30 @@
 }
 
 void
+keepalive(Client *c)
+{
+	char *s;
+
+	while(c->state == Connect)
+	{
+		fprint(c->fd, "UGUD\n");
+		s = rawinput(c->fd);
+		if(!strcmp(s, "y"))
+			c->lastseen = time(nil);
+		else
+		{
+			dprint("keepalive(): rude client %d sent me %s!!11\n", c->fd, s);
+			if(strcmp(s, "") != 0)
+				fprint(c->fd, "DIE rude\n");
+			c->lastseen = 0;
+			break;
+		}
+		free(s);
+		sleep(TIMEOUT);
+	}
+}
+
+void
 registerclient(void *clientfd)
 {
 	char c, *s;
@@ -708,7 +773,11 @@
 	cl = newclient(s, fd);
 	
 	if(cl != nil)
-		makematch(cl);
+	{
+		if(!makematch(cl))
+			keepalive(cl);
+	}
+		
 	die:
 		free(s);
 }
@@ -720,7 +789,7 @@
 	socklen_t clilen;
 	struct sockaddr_in clientaddr;
 	pthread_t t;
-	
+
 	for(;;)
 	{
 		listen(listenfd, 64);
@@ -738,6 +807,7 @@
 	}
 }
 
+
 int 
 main(int argc, char **argv)
 {
@@ -747,7 +817,8 @@
 	
 	listenfd = setuplistener(port);
 	pthread_mutex_init(&game_lock, NULL);
-	
+	signal(SIGPIPE, SIG_IGN);
+
 	/* OpenBSD ignores this */
 	srand(time(nil));