]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
host, netconn: Use cvar callbacks on (cl_)port to reduce branching in main loop
authorcloudwalk <cloudwalk@d7cf8633-e32d-0410-b094-e92efae38249>
Tue, 13 Apr 2021 16:31:40 +0000 (16:31 +0000)
committercloudwalk <cloudwalk@d7cf8633-e32d-0410-b094-e92efae38249>
Tue, 13 Apr 2021 16:31:40 +0000 (16:31 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@13120 d7cf8633-e32d-0410-b094-e92efae38249

cl_main.c
netconn.c
netconn.h
sv_main.c

index d9256f16917dc859b178a7e8ce71cbd4c67ef71b..7ecf1bf800c53e0d1beb286d4d84f3e038b28502 100644 (file)
--- 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;
index 61ffeeda57765948744ddaa9818e2a282baa01aa..1d52b5ad78f8f5bf8151669a6a4b303d613740e4 100755 (executable)
--- 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);
index 9249af10336994c20e1941d712e2352c8010d111..f8e9f7a459031b78188819a900b41801144c9474 100755 (executable)
--- 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);
index ddb509f3d59b49fbd795108392f16b300c135248..4730fa504c22064d68ba0cf1f9601c75fa63a510 100644 (file)
--- 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)