From a5ec983c5210801e89e43a86156789d7623fb1cc Mon Sep 17 00:00:00 2001 From: cloudwalk Date: Tue, 13 Apr 2021 16:31:40 +0000 Subject: [PATCH] host, netconn: Use cvar callbacks on (cl_)port to reduce branching in main loop git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@13120 d7cf8633-e32d-0410-b094-e92efae38249 --- cl_main.c | 4 ++++ netconn.c | 56 ++++++++++++++++++++++++++++++++++++------------------- netconn.h | 1 + sv_main.c | 1 + 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/cl_main.c b/cl_main.c index d9256f16..7ecf1bf8 100644 --- a/cl_main.c +++ b/cl_main.c @@ -428,6 +428,8 @@ void CL_Disconnect(void) cls.demoplayback = cls.timedemo = host.restless = false; cls.signon = 0; + Cvar_Callback(&cl_netport); + // If we're dropped mid-connection attempt, it won't clear otherwise. SCR_ClearLoadingScreen(false); } @@ -3084,6 +3086,8 @@ void CL_Init (void) CL_Video_Init(); + NetConn_UpdateSockets_Client(); + host.hook.ConnectLocal = CL_EstablishConnection_Local; host.hook.Disconnect = CL_Disconnect; host.hook.CL_Intermission = CL_Intermission; diff --git a/netconn.c b/netconn.c index 61ffeeda..1d52b5ad 100755 --- a/netconn.c +++ b/netconn.c @@ -1146,36 +1146,51 @@ void NetConn_Close(netconn_t *conn) static int clientport = -1; static int clientport2 = -1; static int hostport = -1; -void NetConn_UpdateSockets(void) -{ - int i, j; - // TODO add logic to automatically close sockets if needed - LHNET_DefaultDSCP(net_tos_dscp.integer); +// Call on disconnect, during startup, or if cl_netport is changed +void NetConn_UpdateSockets_Client(void) +{ + if (cls.state == ca_disconnected && clientport != clientport2) + { + clientport = clientport2; + NetConn_CloseClientPorts(); + } + if (cl_numsockets == 0) + NetConn_OpenClientPorts(); +} - if (cls.state != ca_dedicated) +// Call when cl_port is changed +static void NetConn_cl_netport_Callback(cvar_t *var) +{ + if(cls.state != ca_dedicated) { - if (clientport2 != cl_netport.integer) + if (clientport2 != var->integer) { - clientport2 = cl_netport.integer; + clientport2 = var->integer; if (cls.state == ca_connected) Con_Print("Changing \"cl_port\" will not take effect until you reconnect.\n"); } - if (cls.state == ca_disconnected && clientport != clientport2) - { - clientport = clientport2; - NetConn_CloseClientPorts(); - } - if (cl_numsockets == 0) - NetConn_OpenClientPorts(); + NetConn_UpdateSockets_Client(); } +} - if (hostport != sv_netport.integer) +// Call when port is changed +static void NetConn_sv_netport_Callback(cvar_t *var) +{ + if (hostport != var->integer) { - hostport = sv_netport.integer; + hostport = var->integer; if (sv.active) Con_Print("Changing \"port\" will not take effect until \"map\" command is executed.\n"); } +} + +void NetConn_UpdateSockets(void) +{ + int i, j; + + // TODO add logic to automatically close sockets if needed + LHNET_DefaultDSCP(net_tos_dscp.integer); for (j = 0;j < MAX_RCONS;j++) { @@ -3885,8 +3900,9 @@ void NetConn_Init(void) Cvar_RegisterCallback(&net_slist_favorites, NetConn_UpdateFavorites_c); #endif Cvar_RegisterVariable(&net_slist_pause); - if(LHNET_DefaultDSCP(-1) >= 0) // register cvar only if supported - Cvar_RegisterVariable(&net_tos_dscp); +#ifdef IP_TOS // register cvar only if supported + Cvar_RegisterVariable(&net_tos_dscp); +#endif Cvar_RegisterVariable(&net_messagetimeout); Cvar_RegisterVariable(&net_connecttimeout); Cvar_RegisterVariable(&net_connectfloodblockingtimeout); @@ -3902,7 +3918,9 @@ void NetConn_Init(void) Cvar_RegisterVariable(&hostname); Cvar_RegisterVariable(&developer_networking); Cvar_RegisterVariable(&cl_netport); + Cvar_RegisterCallback(&cl_netport, NetConn_cl_netport_Callback); Cvar_RegisterVariable(&sv_netport); + Cvar_RegisterCallback(&sv_netport, NetConn_sv_netport_Callback); Cvar_RegisterVariable(&net_address); Cvar_RegisterVariable(&net_address_ipv6); Cvar_RegisterVariable(&sv_public); diff --git a/netconn.h b/netconn.h index 9249af10..f8e9f7a4 100755 --- a/netconn.h +++ b/netconn.h @@ -443,6 +443,7 @@ void NetConn_CloseClientPorts(void); void NetConn_OpenClientPorts(void); void NetConn_CloseServerPorts(void); void NetConn_OpenServerPorts(int opennetports); +void NetConn_UpdateSockets_Client(void); void NetConn_UpdateSockets(void); lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address); lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address); diff --git a/sv_main.c b/sv_main.c index ddb509f3..4730fa50 100644 --- a/sv_main.c +++ b/sv_main.c @@ -711,6 +711,7 @@ void SV_Init (void) sv_mempool = Mem_AllocPool("server", 0, NULL); SV_ServerOptions(); + Cvar_Callback(&sv_netport); } static void SV_SaveEntFile_f(cmd_state_t *cmd) -- 2.39.2