From: molivier Date: Wed, 2 Oct 2002 06:50:45 +0000 (+0000) Subject: Tried to be a little smarter with the heartbeats, using a simple priority mecanism... X-Git-Tag: RELEASE_0_2_0_RC1~163 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=09097432e79e5ab995a9ea5bea73df10edf61b57;p=xonotic%2Fdarkplaces.git Tried to be a little smarter with the heartbeats, using a simple priority mecanism. Added a few "const" here and there in the net code. Fixed heartbeat code on Win32 (added Loop_Heartbeat and Datagram_Heartbeat to net_win.c; oups...) git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@2488 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/host.c b/host.c index c0a07774..ecb1d317 100644 --- a/host.c +++ b/host.c @@ -462,7 +462,7 @@ void SV_DropClient (qboolean crash) MSG_WriteByte (&client->message, 0); } - NET_Heartbeat (); + NET_Heartbeat (1); } /* @@ -490,8 +490,8 @@ void Host_ShutdownServer(qboolean crash) // stop all client sounds immediately CL_Disconnect (); - NET_Heartbeat (); - NET_Heartbeat (); + NET_Heartbeat (2); + NET_Heartbeat (2); // flush any pending messages - like the score!!! start = Sys_DoubleTime(); @@ -687,6 +687,9 @@ void Host_ServerFrame (void) // send all messages to the clients SV_SendClientMessages (); + +// send an heartbeat if enough time has passed since the last one + NET_Heartbeat (0); } diff --git a/net.h b/net.h index e4be15b8..d7c315f1 100644 --- a/net.h +++ b/net.h @@ -191,8 +191,8 @@ typedef struct int (*Init) (void); void (*Listen) (qboolean state); void (*SearchForHosts) (qboolean xmit); - qboolean (*SearchForInetHosts) (char *master); - qsocket_t *(*Connect) (char *host); + qboolean (*SearchForInetHosts) (const char *master); + qsocket_t *(*Connect) (const char *host); qsocket_t *(*CheckNewConnections) (void); int (*QGetMessage) (qsocket_t *sock); int (*QSendMessage) (qsocket_t *sock, sizebuf_t *data); @@ -201,7 +201,7 @@ typedef struct qboolean (*CanSendUnreliableMessage) (qsocket_t *sock); void (*Close) (qsocket_t *sock); void (*Shutdown) (void); - void (*Heartbeat) (char *host); + void (*Heartbeat) (const char *host); int controlSock; } net_driver_t; @@ -277,8 +277,8 @@ struct qsocket_s *NET_CheckNewConnections (void); struct qsocket_s *NET_Connect (char *host); // called by client to connect to a host. Returns -1 if not able to -void NET_Heartbeat (void); -// Send an heartbeat to the master server(s) +void NET_Heartbeat (int priority); +// Send an heartbeat to the master server(s). priority: 0 = lowest, 1 = state change, 2 = force qboolean NET_CanSendMessage (qsocket_t *sock); // Returns true or false if the given qsocket can currently accept a diff --git a/net_dgrm.c b/net_dgrm.c index 66ec880d..ede95806 100644 --- a/net_dgrm.c +++ b/net_dgrm.c @@ -1211,7 +1211,7 @@ void Datagram_SearchForHosts (qboolean xmit) } -static qboolean _Datagram_SearchForInetHosts (char *master) +static qboolean _Datagram_SearchForInetHosts (const char *master) { qboolean result = false; struct qsockaddr masteraddr; @@ -1253,7 +1253,7 @@ static qboolean _Datagram_SearchForInetHosts (char *master) } -qboolean Datagram_SearchForInetHosts (char *master) +qboolean Datagram_SearchForInetHosts (const char *master) { qboolean result = false; for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++) @@ -1269,7 +1269,7 @@ qboolean Datagram_SearchForInetHosts (char *master) } -static qsocket_t *_Datagram_Connect (char *host) +static qsocket_t *_Datagram_Connect (const char *host) { struct qsockaddr sendaddr; struct qsockaddr readaddr; @@ -1434,7 +1434,7 @@ ErrorReturn2: return NULL; } -qsocket_t *Datagram_Connect (char *host) +qsocket_t *Datagram_Connect (const char *host) { qsocket_t *ret = NULL; @@ -1445,7 +1445,7 @@ qsocket_t *Datagram_Connect (char *host) return ret; } -static void _Datagram_Heartbeat (char *master) +static void _Datagram_Heartbeat (const char *master) { struct qsockaddr masteraddr; int portnum; @@ -1465,7 +1465,7 @@ static void _Datagram_Heartbeat (char *master) dfunc.Send (net_message.data, net_message.cursize, &masteraddr); } -void Datagram_Heartbeat (char *master) +void Datagram_Heartbeat (const char *master) { for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++) if (net_landrivers[net_landriverlevel].initialized) diff --git a/net_dgrm.h b/net_dgrm.h index da3f28bc..ad2bf556 100644 --- a/net_dgrm.h +++ b/net_dgrm.h @@ -25,8 +25,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. int Datagram_Init (void); void Datagram_Listen (qboolean state); void Datagram_SearchForHosts (qboolean xmit); -qboolean Datagram_SearchForInetHosts (char *master); -qsocket_t *Datagram_Connect (char *host); +qboolean Datagram_SearchForInetHosts (const char *master); +qsocket_t *Datagram_Connect (const char *host); qsocket_t *Datagram_CheckNewConnections (void); int Datagram_GetMessage (qsocket_t *sock); int Datagram_SendMessage (qsocket_t *sock, sizebuf_t *data); @@ -35,7 +35,7 @@ qboolean Datagram_CanSendMessage (qsocket_t *sock); qboolean Datagram_CanSendUnreliableMessage (qsocket_t *sock); void Datagram_Close (qsocket_t *sock); void Datagram_Shutdown (void); -void Datagram_Heartbeat (char *master); +void Datagram_Heartbeat (const char *master); #endif diff --git a/net_loop.c b/net_loop.c index 0cd6662e..47d8d0e1 100644 --- a/net_loop.c +++ b/net_loop.c @@ -39,7 +39,7 @@ void Loop_Shutdown (void) } -void Loop_Heartbeat (char *master) +void Loop_Heartbeat (const char *master) { } @@ -67,13 +67,13 @@ void Loop_SearchForHosts (qboolean xmit) } -qboolean Loop_SearchForInetHosts (char *master) +qboolean Loop_SearchForInetHosts (const char *master) { return false; } -qsocket_t *Loop_Connect (char *host) +qsocket_t *Loop_Connect (const char *host) { if (strcmp(host,"local") != 0) return NULL; diff --git a/net_loop.h b/net_loop.h index 2d62a6c0..0938fb96 100644 --- a/net_loop.h +++ b/net_loop.h @@ -25,8 +25,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. int Loop_Init (void); void Loop_Listen (qboolean state); void Loop_SearchForHosts (qboolean xmit); -qboolean Loop_SearchForInetHosts (char *master); -qsocket_t *Loop_Connect (char *host); +qboolean Loop_SearchForInetHosts (const char *master); +qsocket_t *Loop_Connect (const char *host); qsocket_t *Loop_CheckNewConnections (void); int Loop_GetMessage (qsocket_t *sock); int Loop_SendMessage (qsocket_t *sock, sizebuf_t *data); @@ -35,7 +35,7 @@ qboolean Loop_CanSendMessage (qsocket_t *sock); qboolean Loop_CanSendUnreliableMessage (qsocket_t *sock); void Loop_Close (qsocket_t *sock); void Loop_Shutdown (void); -void Loop_Heartbeat (char *master); +void Loop_Heartbeat (const char *master); #endif diff --git a/net_main.c b/net_main.c index ed2d341d..39340373 100644 --- a/net_main.c +++ b/net_main.c @@ -228,7 +228,7 @@ static void NET_Port_f (void) static void NET_Heartbeat_f (void) { - NET_Heartbeat (); + NET_Heartbeat (2); } @@ -352,7 +352,7 @@ static void Slist_Poll(void) static void InetSlist_Send(void) { - char* host; + const char* host; if (!slistInProgress) return; @@ -683,9 +683,13 @@ NET_Heartbeat Send an heartbeat to the master server(s) ==================== */ -void NET_Heartbeat (void) +void NET_Heartbeat (int priority) { - char* host; + const char* host; + + if (! Master_AllowHeartbeat (priority)) + return; + while ((host = Master_BuildHeartbeat ()) != NULL) { for (net_driverlevel=0 ; net_driverlevel 30.0) + { + nextheartbeattime = realtime + 30.0; + return false; + } + + if (priority <= 1 && realtime < nextheartbeattime) + return false; + } + + // send an heartbeat every 3 minutes by default (every 5 sec if we haven't yet found a master server) + // TODO: some cvars would be better than hardcoded values + nextheartbeattime = realtime + (masteravailable ? (3.0 * 60.0) : 5.0); + return true; +} + /* ==================== @@ -37,7 +79,7 @@ Master_BuildGetServers Build a getservers request for a master server ==================== */ -char* Master_BuildGetServers (void) +const char* Master_BuildGetServers (void) { static int nextmaster = 0; cvar_t* sv_master; @@ -77,7 +119,7 @@ Master_BuildHeartbeat Build an heartbeat for a master server ==================== */ -char* Master_BuildHeartbeat (void) +const char* Master_BuildHeartbeat (void) { static int nextmaster = 0; cvar_t* sv_master; @@ -125,6 +167,8 @@ int Master_HandleMessage (void) char response [512]; size_t length; + masteravailable = true; + length = snprintf (response, sizeof (response), "infoResponse\x0A" "\\gamename\\%s\\modname\\%s\\sv_maxclients\\%d" "\\clients\\%d\\mapname\\%s\\hostname\\%s\\protocol\\%d", diff --git a/net_master.h b/net_master.h index 93257124..f7cbd67e 100644 --- a/net_master.h +++ b/net_master.h @@ -24,10 +24,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define MASTER_PORT 27950 -char* Master_BuildGetServers (void); -char* Master_BuildHeartbeat (void); -int Master_HandleMessage (void); -void Master_Init (void); -void Master_ParseServerList (net_landriver_t* dfunc); +qboolean Master_AllowHeartbeat (int priority); +const char* Master_BuildGetServers (void); +const char* Master_BuildHeartbeat (void); +int Master_HandleMessage (void); +void Master_Init (void); +void Master_ParseServerList (net_landriver_t* dfunc); #endif diff --git a/net_win.c b/net_win.c index 725eef4f..4b43b5c9 100644 --- a/net_win.c +++ b/net_win.c @@ -39,7 +39,8 @@ net_driver_t net_drivers[MAX_NET_DRIVERS] = Loop_CanSendMessage, Loop_CanSendUnreliableMessage, Loop_Close, - Loop_Shutdown + Loop_Shutdown, + Loop_Heartbeat } , { @@ -57,7 +58,8 @@ net_driver_t net_drivers[MAX_NET_DRIVERS] = Datagram_CanSendMessage, Datagram_CanSendUnreliableMessage, Datagram_Close, - Datagram_Shutdown + Datagram_Shutdown, + Datagram_Heartbeat } }; diff --git a/sv_main.c b/sv_main.c index 9dd462f3..c3fbf2b9 100644 --- a/sv_main.c +++ b/sv_main.c @@ -383,7 +383,7 @@ void SV_CheckForNewClients (void) SV_ConnectClient (i); net_activeconnections++; - NET_Heartbeat (); + NET_Heartbeat (1); } } @@ -1843,6 +1843,6 @@ void SV_SpawnServer (const char *server) SV_SendServerinfo (host_client); Con_DPrintf ("Server spawned.\n"); - NET_Heartbeat (); + NET_Heartbeat (2); }