All pastes #287744 Raw Edit

abandon server patch

public text v1 · immutable
#287744 ·published 2006-12-21 19:04 UTC
rendered paste body
Index: src/server/sv_main.c
===================================================================
--- src/server/sv_main.c	(revision 5360)
+++ src/server/sv_main.c	(working copy)
@@ -62,6 +62,8 @@
 
 void Master_Shutdown(void);
 
+qboolean abandon = qfalse;		/* shutdown server when all clients disconnect and don't accept new connections */
+qboolean killserver = qfalse;	/* will initiate shutdown once abandon is set */
 
 /*============================================================================ */
 
@@ -85,6 +87,16 @@
 	drop->player->inuse = qfalse;
 	drop->state = cs_zombie;	/* become free in a few seconds */
 	drop->name[0] = 0;
+
+	if (abandon) {
+		int count = 0;
+		int i;
+		for (i = 0; i < sv_maxclients->value; i++)
+			if (svs.clients[i].state >= cs_connected)
+				count++;
+		if (count == 0)
+			killserver = qtrue;
+	}
 }
 
 
@@ -742,6 +754,12 @@
 
 	/* TODO: send a heartbeat to the master if needed */
 	Master_Heartbeat();
+
+	if (abandon && killserver) {
+		abandon = qfalse;
+		killserver = qfalse;
+		SV_Shutdown("Server disconnected\n", qfalse);
+	}
 }
 
 /*============================================================================ */
@@ -922,8 +940,6 @@
 			Netchan_Transmit(&cl->netchan, net_message.cursize, net_message.data);
 }
 
-
-
 /**
  * @brief Called when each game quits, before Sys_Quit or Sys_Error
  */
@@ -952,3 +968,13 @@
 	if (sv_maxclients)
 		sv_maxclients->flags &= ~CVAR_LATCH;
 }
+
+/*
+ * @brief Will eventually shutdown the server once all clients have disconnected
+ */
+void SV_ShutdownWhenEmpty() 
+{
+	abandon = qtrue;
+	/* pretend server is already dead, otherwise clients may try and reconnect */
+	Com_SetServerState(ss_dead);
+}
Index: src/qcommon/qcommon.h
===================================================================
--- src/qcommon/qcommon.h	(revision 5360)
+++ src/qcommon/qcommon.h	(working copy)
@@ -325,6 +325,7 @@
 
 void SV_Init(void);
 void SV_Shutdown(char *finalmsg, qboolean reconnect);
+void SV_ShutdownWhenEmpty(void);
 void SV_Frame(int msec);
 
 #endif							/* QCOMMON_DEFINED */
Index: src/client/cl_main.c
===================================================================
--- src/client/cl_main.c	(revision 5360)
+++ src/client/cl_main.c	(working copy)
@@ -511,7 +511,8 @@
  */
 void CL_Disconnect_f(void)
 {
-	Com_Drop();
+	SV_ShutdownWhenEmpty();
+	CL_Drop();
 }