[iortcw] 151/152: All: Don't repeat DNS lookups for masters that don't resolve on startup

Simon McVittie smcv at debian.org
Fri Sep 8 10:40:29 UTC 2017


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to annotated tag 1.5a
in repository iortcw.

commit 871afee9cd3b91a8d456a04123303f7ff43b1e6e
Author: MAN-AT-ARMS <M4N4T4RMS at gmail.com>
Date:   Sun Nov 13 18:56:47 2016 -0500

    All: Don't repeat DNS lookups for masters that don't resolve on startup
---
 MP/code/server/sv_main.c | 123 ++++++++++++++++++++++++++---------------------
 SP/code/server/sv_main.c | 123 ++++++++++++++++++++++++++---------------------
 2 files changed, 136 insertions(+), 110 deletions(-)

diff --git a/MP/code/server/sv_main.c b/MP/code/server/sv_main.c
index a029846..8e97425 100644
--- a/MP/code/server/sv_main.c
+++ b/MP/code/server/sv_main.c
@@ -241,17 +241,17 @@ but not on every player enter or exit.
 #define HEARTBEAT_MSEC  300 * 1000
 void SV_MasterHeartbeat(const char *message)
 {
-	netadr_t	adr[2]; // [2] for v4 and v6 address for the same address string.
-	int		i;
-	int		res;
-	int		netenabled;
+	static netadr_t	adr[MAX_MASTER_SERVERS][2]; // [2] for v4 and v6 address for the same address string.
+	int			i;
+	int			res;
+	int			netenabled;
+	static qboolean		firstRes = qtrue;
 
 	// DHM - Nerve :: Update Server doesn't send heartbeat
 #ifdef UPDATE_SERVER
 	return;
 #endif
 
-	Com_Memset(adr, 0, sizeof(netadr_t) * 2);
 	netenabled = Cvar_VariableIntegerValue("net_enabled");
 
 	// "dedicated 1" is for lan play, "dedicated 2" is for inet public play
@@ -273,64 +273,77 @@ void SV_MasterHeartbeat(const char *message)
 		if(!sv_master[i]->string[0])
 			continue;
 
-		// Enforce IP check everytime in case master moves to a new IP
-		sv_master[i]->modified = qfalse;
-
-		if(netenabled & NET_ENABLEV4)
+		// see if we haven't already resolved the name
+		// if server did not resolve on first attempt, do not attempt another dns lookup
+		// if server did resolve on first attempt, attempt resolution on subsequent heartbeats
+		if(sv_master[i]->modified || (adr[i][0].type == NA_BAD && adr[i][1].type == NA_BAD))
 		{
-			Com_Printf("Resolving %s (IPv4)\n", sv_master[i]->string);
-			res = NET_StringToAdr(sv_master[i]->string, &adr[0], NA_IP);
-
-			if(res == 2)
+			sv_master[i]->modified = qfalse;
+			
+			if(netenabled & NET_ENABLEV4)
 			{
-				// if no port was specified, use the default master port
-				adr[0].port = BigShort(PORT_MASTER);
+				if(firstRes || adr[i][0].type != NA_BAD) {
+					Com_Printf("Resolving %s (IPv4)\n", sv_master[i]->string);
+					res = NET_StringToAdr(sv_master[i]->string, &adr[i][0], NA_IP);
+
+					if(res == 2)
+					{
+						// if no port was specified, use the default master port
+						adr[i][0].port = BigShort(PORT_MASTER);
+					}
+				
+					if(res) {
+						Com_Printf( "%s resolved to %s\n", sv_master[i]->string, NET_AdrToStringwPort(adr[i][0]));
+
+						if(adr[i][0].type != NA_BAD) {
+							Com_Printf ("Sending heartbeat to %s (IPv4)\n", sv_master[i]->string );
+							NET_OutOfBandPrint( NS_SERVER, adr[i][0], "heartbeat %s\n", message);
+						}
+						sv_master[i]->modified = qtrue;
+					} else {
+						Com_Printf( "%s has no IPv4 address.\n", sv_master[i]->string);
+					}
+				}
 			}
-
-			if(res)
-				Com_Printf( "%s resolved to %s\n", sv_master[i]->string, NET_AdrToStringwPort(adr[0]));
-			else
-				Com_Printf( "%s has no IPv4 address.\n", sv_master[i]->string);
-		}
-
-		if(netenabled & NET_ENABLEV6)
-		{
-			Com_Printf("Resolving %s (IPv6)\n", sv_master[i]->string);
-			res = NET_StringToAdr(sv_master[i]->string, &adr[1], NA_IP6);
-
-			if(res == 2)
+			
+			if(netenabled & NET_ENABLEV6)
 			{
-				// if no port was specified, use the default master port
-				adr[1].port = BigShort(PORT_MASTER);
+				if(firstRes || adr[i][1].type != NA_BAD) {
+					Com_Printf("Resolving %s (IPv6)\n", sv_master[i]->string);
+					res = NET_StringToAdr(sv_master[i]->string, &adr[i][1], NA_IP6);
+
+					if(res == 2)
+					{
+						// if no port was specified, use the default master port
+						adr[i][1].port = BigShort(PORT_MASTER);
+					}
+				
+					if(res) {
+						Com_Printf( "%s resolved to %s\n", sv_master[i]->string, NET_AdrToStringwPort(adr[i][1]));
+						if(adr[i][1].type != NA_BAD) {
+							Com_Printf ("Sending heartbeat to %s (IPv6)\n", sv_master[i]->string );
+							NET_OutOfBandPrint( NS_SERVER, adr[i][1], "heartbeat %s\n", message);
+						}
+						sv_master[i]->modified = qtrue;
+					} else {
+						Com_Printf( "%s has no IPv6 address.\n", sv_master[i]->string);
+					}
+				}
 			}
 
-			if(res)
-				Com_Printf( "%s resolved to %s\n", sv_master[i]->string, NET_AdrToStringwPort(adr[1]));
-			else
-				Com_Printf( "%s has no IPv6 address.\n", sv_master[i]->string);
-		}
-
-		if(adr[0].type == NA_BAD && adr[1].type == NA_BAD)
-		{
-			// if the address failed to resolve, clear it
-			// so we don't take repeated dns hits
-			Com_Printf("Couldn't resolve address: %s\n", sv_master[i]->string);
-			Cvar_Set(sv_master[i]->name, "");
-			sv_master[i]->modified = qfalse;
-			continue;
+			if(adr[i][0].type == NA_BAD && adr[i][1].type == NA_BAD)
+			{
+				// if the address failed to resolve in both ipv4 or ipv6, clear it
+				// so we don't take repeated dns hits
+				Com_Printf("Couldn't resolve address: %s\n", sv_master[i]->string);
+				Cvar_Set(sv_master[i]->name, "");
+				sv_master[i]->modified = qfalse;
+				continue;
+			}
 		}
-
-
-		Com_Printf ("Sending heartbeat to %s\n", sv_master[i]->string );
-
-		// this command should be changed if the server info / status format
-		// ever incompatably changes
-
-		if(adr[0].type != NA_BAD)
-			NET_OutOfBandPrint( NS_SERVER, adr[0], "heartbeat %s\n", message);
-		if(adr[1].type != NA_BAD)
-			NET_OutOfBandPrint( NS_SERVER, adr[1], "heartbeat %s\n", message);
 	}
+
+	firstRes = qfalse;
 }
 
 /*
diff --git a/SP/code/server/sv_main.c b/SP/code/server/sv_main.c
index 16023a9..d98e82d 100644
--- a/SP/code/server/sv_main.c
+++ b/SP/code/server/sv_main.c
@@ -253,12 +253,12 @@ but not on every player enter or exit.
 #define HEARTBEAT_MSEC  300 * 1000
 void SV_MasterHeartbeat(const char *message)
 {
-	netadr_t	adr[2]; // [2] for v4 and v6 address for the same address string.
-	int		i;
-	int		res;
-	int		netenabled;
+	static netadr_t	adr[MAX_MASTER_SERVERS][2]; // [2] for v4 and v6 address for the same address string.
+	int			i;
+	int			res;
+	int			netenabled;
+	static qboolean		firstRes = qtrue;
 
-	Com_Memset(adr, 0, sizeof(netadr_t) * 2);
 	netenabled = Cvar_VariableIntegerValue("net_enabled");
 
 	// "dedicated 1" is for lan play, "dedicated 2" is for inet public play
@@ -280,64 +280,77 @@ void SV_MasterHeartbeat(const char *message)
 		if(!sv_master[i]->string[0])
 			continue;
 
-		// Enforce IP check everytime in case master moves to a new IP
-		sv_master[i]->modified = qfalse;
-
-		if(netenabled & NET_ENABLEV4)
+		// see if we haven't already resolved the name
+		// if server did not resolve on first attempt, do not attempt another dns lookup
+		// if server did resolve on first attempt, attempt resolution on subsequent heartbeats
+		if(sv_master[i]->modified || (adr[i][0].type == NA_BAD && adr[i][1].type == NA_BAD))
 		{
-			Com_Printf("Resolving %s (IPv4)\n", sv_master[i]->string);
-			res = NET_StringToAdr(sv_master[i]->string, &adr[0], NA_IP);
-
-			if(res == 2)
+			sv_master[i]->modified = qfalse;
+			
+			if(netenabled & NET_ENABLEV4)
 			{
-				// if no port was specified, use the default master port
-				adr[0].port = BigShort(PORT_MASTER);
+				if(firstRes || adr[i][0].type != NA_BAD) {
+					Com_Printf("Resolving %s (IPv4)\n", sv_master[i]->string);
+					res = NET_StringToAdr(sv_master[i]->string, &adr[i][0], NA_IP);
+
+					if(res == 2)
+					{
+						// if no port was specified, use the default master port
+						adr[i][0].port = BigShort(PORT_MASTER);
+					}
+				
+					if(res) {
+						Com_Printf( "%s resolved to %s\n", sv_master[i]->string, NET_AdrToStringwPort(adr[i][0]));
+
+						if(adr[i][0].type != NA_BAD) {
+							Com_Printf ("Sending heartbeat to %s (IPv4)\n", sv_master[i]->string );
+							NET_OutOfBandPrint( NS_SERVER, adr[i][0], "heartbeat %s\n", message);
+						}
+						sv_master[i]->modified = qtrue;
+					} else {
+						Com_Printf( "%s has no IPv4 address.\n", sv_master[i]->string);
+					}
+				}
 			}
-
-			if(res)
-				Com_Printf( "%s resolved to %s\n", sv_master[i]->string, NET_AdrToStringwPort(adr[0]));
-			else
-				Com_Printf( "%s has no IPv4 address.\n", sv_master[i]->string);
-		}
-
-		if(netenabled & NET_ENABLEV6)
-		{
-			Com_Printf("Resolving %s (IPv6)\n", sv_master[i]->string);
-			res = NET_StringToAdr(sv_master[i]->string, &adr[1], NA_IP6);
-
-			if(res == 2)
+			
+			if(netenabled & NET_ENABLEV6)
 			{
-				// if no port was specified, use the default master port
-				adr[1].port = BigShort(PORT_MASTER);
+				if(firstRes || adr[i][1].type != NA_BAD) {
+					Com_Printf("Resolving %s (IPv6)\n", sv_master[i]->string);
+					res = NET_StringToAdr(sv_master[i]->string, &adr[i][1], NA_IP6);
+
+					if(res == 2)
+					{
+						// if no port was specified, use the default master port
+						adr[i][1].port = BigShort(PORT_MASTER);
+					}
+				
+					if(res) {
+						Com_Printf( "%s resolved to %s\n", sv_master[i]->string, NET_AdrToStringwPort(adr[i][1]));
+						if(adr[i][1].type != NA_BAD) {
+							Com_Printf ("Sending heartbeat to %s (IPv6)\n", sv_master[i]->string );
+							NET_OutOfBandPrint( NS_SERVER, adr[i][1], "heartbeat %s\n", message);
+						}
+						sv_master[i]->modified = qtrue;
+					} else {
+						Com_Printf( "%s has no IPv6 address.\n", sv_master[i]->string);
+					}
+				}
 			}
 
-			if(res)
-				Com_Printf( "%s resolved to %s\n", sv_master[i]->string, NET_AdrToStringwPort(adr[1]));
-			else
-				Com_Printf( "%s has no IPv6 address.\n", sv_master[i]->string);
-		}
-
-		if(adr[0].type == NA_BAD && adr[1].type == NA_BAD)
-		{
-			// if the address failed to resolve, clear it
-			// so we don't take repeated dns hits
-			Com_Printf("Couldn't resolve address: %s\n", sv_master[i]->string);
-			Cvar_Set(sv_master[i]->name, "");
-			sv_master[i]->modified = qfalse;
-			continue;
+			if(adr[i][0].type == NA_BAD && adr[i][1].type == NA_BAD)
+			{
+				// if the address failed to resolve in both ipv4 or ipv6, clear it
+				// so we don't take repeated dns hits
+				Com_Printf("Couldn't resolve address: %s\n", sv_master[i]->string);
+				Cvar_Set(sv_master[i]->name, "");
+				sv_master[i]->modified = qfalse;
+				continue;
+			}
 		}
-
-
-		Com_Printf ("Sending heartbeat to %s\n", sv_master[i]->string );
-
-		// this command should be changed if the server info / status format
-		// ever incompatably changes
-
-		if(adr[0].type != NA_BAD)
-			NET_OutOfBandPrint( NS_SERVER, adr[0], "heartbeat %s\n", message);
-		if(adr[1].type != NA_BAD)
-			NET_OutOfBandPrint( NS_SERVER, adr[1], "heartbeat %s\n", message);
 	}
+
+	firstRes = qfalse;
 }
 
 /*

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/iortcw.git



More information about the Pkg-games-commits mailing list