Skip to content

Commit 597dbab

Browse files
committed
Refactor NET_Config; drop cvar modified checks
NET_Config(bool enableNetworking) was a function to either open the network sockets if the arg is true, or close them if it is false. It checked the modified flag of a bunch of net_ cvars, with the idea that if it is requested to enable networking while it was already enabled, to not do anything unless the configuration is changed. But actually this situation can never happen, so we can drop the modify checks and always reinitialize. NET_Config( true ) was only done in 4 places, none of which could possibly omit the network (re-)initialization: - NET_Init - only called once on startup - NET_Restart - the point of /net_restart is to force reinitialization - SV_Startup - this is only done in clients and starting the server is a state change for clients so restart can't be skipped - SV_Shutdown - this is only done in clients and stopping the server is a state change for clients so restart can't be skipped. Also split NET_Config into two functions NET_EnableNetworking and NET_DisableNetworking for better readability.
1 parent c24b803 commit 597dbab

File tree

3 files changed

+49
-115
lines changed

3 files changed

+49
-115
lines changed

src/engine/qcommon/net_ip.cpp

Lines changed: 45 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Maryland 20850 USA.
3535
#include "qcommon/q_shared.h"
3636
#include "qcommon/qcommon.h"
3737
#include <common/FileSystem.h>
38+
#include "engine/framework/Application.h"
3839
#include "engine/framework/Network.h"
3940
#include "server/server.h"
4041

@@ -120,11 +121,6 @@ namespace net {
120121

121122
static bool usingSocks = false;
122123
static bool networkingEnabled = false;
123-
#ifndef BUILD_SERVER
124-
static bool serverMode = false;
125-
#else
126-
static const bool serverMode = true;
127-
#endif
128124

129125
cvar_t *net_enabled;
130126

@@ -1598,7 +1594,7 @@ static int NET_EnsureValidPortNo( int port )
15981594
NET_OpenIP
15991595
====================
16001596
*/
1601-
static void NET_OpenIP()
1597+
static void NET_OpenIP( bool serverMode )
16021598
{
16031599
int i;
16041600
int err = 0;
@@ -1689,10 +1685,8 @@ static void NET_OpenIP()
16891685
NET_GetCvars
16901686
====================
16911687
*/
1692-
static bool NET_GetCvars()
1688+
static void NET_GetCvars()
16931689
{
1694-
int modified;
1695-
16961690
#ifdef BUILD_SERVER
16971691
// I want server owners to explicitly turn on IPv6 support.
16981692
net_enabled = Cvar_Get( "net_enabled", "1", CVAR_LATCH );
@@ -1702,147 +1696,82 @@ static bool NET_GetCvars()
17021696
* used if available due to ping */
17031697
net_enabled = Cvar_Get( "net_enabled", "3", CVAR_LATCH );
17041698
#endif
1705-
modified = net_enabled->modified;
1706-
net_enabled->modified = false;
17071699

17081700
net_ip = Cvar_Get( "net_ip", "0.0.0.0", CVAR_LATCH );
1709-
modified += net_ip->modified;
1710-
net_ip->modified = false;
1711-
17121701
net_ip6 = Cvar_Get( "net_ip6", "::", CVAR_LATCH );
1713-
modified += net_ip6->modified;
1714-
net_ip6->modified = false;
1715-
17161702
net_port = Cvar_Get( "net_port", va( "%i", PORT_SERVER ), CVAR_LATCH );
1717-
modified += net_port->modified;
1718-
net_port->modified = false;
1719-
17201703
net_port6 = Cvar_Get( "net_port6", va( "%i", PORT_SERVER ), CVAR_LATCH );
1721-
modified += net_port6->modified;
1722-
net_port6->modified = false;
17231704

17241705
// Some cvars for configuring multicast options which facilitates scanning for servers on local subnets.
17251706
net_mcast6addr = Cvar_Get( "net_mcast6addr", NET_MULTICAST_IP6, CVAR_LATCH );
1726-
modified += net_mcast6addr->modified;
1727-
net_mcast6addr->modified = false;
1728-
17291707
#ifdef _WIN32
17301708
net_mcast6iface = Cvar_Get( "net_mcast6iface", "0", CVAR_LATCH );
17311709
#else
17321710
net_mcast6iface = Cvar_Get( "net_mcast6iface", "", CVAR_LATCH );
17331711
#endif
1734-
modified += net_mcast6iface->modified;
1735-
net_mcast6iface->modified = false;
17361712

17371713
net_socksEnabled = Cvar_Get( "net_socksEnabled", "0", CVAR_LATCH );
1738-
modified += net_socksEnabled->modified;
1739-
net_socksEnabled->modified = false;
1740-
17411714
net_socksServer = Cvar_Get( "net_socksServer", "", CVAR_LATCH );
1742-
modified += net_socksServer->modified;
1743-
net_socksServer->modified = false;
1744-
17451715
net_socksPort = Cvar_Get( "net_socksPort", "1080", CVAR_LATCH );
1746-
modified += net_socksPort->modified;
1747-
net_socksPort->modified = false;
1748-
17491716
net_socksUsername = Cvar_Get( "net_socksUsername", "", CVAR_LATCH );
1750-
modified += net_socksUsername->modified;
1751-
net_socksUsername->modified = false;
1752-
17531717
net_socksPassword = Cvar_Get( "net_socksPassword", "", CVAR_LATCH );
1754-
modified += net_socksPassword->modified;
1755-
net_socksPassword->modified = false;
1756-
1757-
return modified ? true : false;
17581718
}
17591719

1760-
/*
1761-
====================
1762-
NET_Config
1763-
====================
1764-
*/
1765-
void NET_Config( bool enableNetworking )
1720+
void NET_EnableNetworking( bool serverMode )
17661721
{
1767-
bool modified;
1768-
bool stop;
1769-
bool start;
1770-
#ifndef BUILD_SERVER
1771-
bool svRunning;
1772-
#endif
1773-
17741722
// get any latched changes to cvars
1775-
modified = NET_GetCvars();
1776-
#ifndef BUILD_SERVER
1777-
svRunning = !!com_sv_running->integer;
1778-
modified |= ( svRunning != serverMode );
1779-
#endif
1723+
NET_GetCvars();
17801724

1781-
if ( !net_enabled->integer )
1782-
{
1783-
enableNetworking = false;
1784-
}
1725+
// always cycle off and on because this function is only called on a state change or forced restart
1726+
NET_DisableNetworking();
17851727

1786-
// if enable state is the same and no cvars were modified, we have nothing to do
1787-
if ( enableNetworking == networkingEnabled && !modified )
1728+
if ( !( net_enabled->integer & ( NET_ENABLEV4 | NET_ENABLEV6 ) ) )
17881729
{
17891730
return;
17901731
}
17911732

1792-
start = enableNetworking;
1793-
if ( enableNetworking == networkingEnabled )
1794-
{
1795-
stop = enableNetworking;
1796-
}
1797-
else
1733+
networkingEnabled = true;
1734+
1735+
NET_OpenIP( serverMode );
1736+
NET_SetMulticast6();
1737+
SV_NET_Config();
1738+
}
1739+
1740+
void NET_DisableNetworking()
1741+
{
1742+
if ( !networkingEnabled )
17981743
{
1799-
stop = !enableNetworking;
1744+
return;
18001745
}
18011746

1802-
#ifndef BUILD_SERVER
1803-
serverMode = svRunning;
1804-
#endif
1805-
networkingEnabled = enableNetworking;
1747+
networkingEnabled = false;
18061748

1807-
if ( stop )
1749+
if ( ip_socket != INVALID_SOCKET )
18081750
{
1809-
if ( ip_socket != INVALID_SOCKET )
1810-
{
1811-
closesocket( ip_socket );
1812-
ip_socket = INVALID_SOCKET;
1813-
}
1751+
closesocket( ip_socket );
1752+
ip_socket = INVALID_SOCKET;
1753+
}
18141754

1815-
if ( multicast6_socket != INVALID_SOCKET )
1755+
if ( multicast6_socket != INVALID_SOCKET )
1756+
{
1757+
if ( multicast6_socket != ip6_socket )
18161758
{
1817-
if ( multicast6_socket != ip6_socket )
1818-
{
1819-
closesocket( multicast6_socket );
1820-
}
1821-
1822-
multicast6_socket = INVALID_SOCKET;
1759+
closesocket( multicast6_socket );
18231760
}
18241761

1825-
if ( ip6_socket != INVALID_SOCKET )
1826-
{
1827-
closesocket( ip6_socket );
1828-
ip6_socket = INVALID_SOCKET;
1829-
}
1762+
multicast6_socket = INVALID_SOCKET;
1763+
}
18301764

1831-
if ( socks_socket != INVALID_SOCKET )
1832-
{
1833-
closesocket( socks_socket );
1834-
socks_socket = INVALID_SOCKET;
1835-
}
1765+
if ( ip6_socket != INVALID_SOCKET )
1766+
{
1767+
closesocket( ip6_socket );
1768+
ip6_socket = INVALID_SOCKET;
18361769
}
18371770

1838-
if ( start )
1771+
if ( socks_socket != INVALID_SOCKET )
18391772
{
1840-
if ( net_enabled->integer )
1841-
{
1842-
NET_OpenIP();
1843-
NET_SetMulticast6();
1844-
SV_NET_Config();
1845-
}
1773+
closesocket( socks_socket );
1774+
socks_socket = INVALID_SOCKET;
18461775
}
18471776
}
18481777

@@ -1868,7 +1797,7 @@ void NET_Init()
18681797
Log::Notice( "Winsock Initialized" );
18691798
#endif
18701799

1871-
NET_Config( true );
1800+
NET_EnableNetworking( Application::GetTraits().isServer );
18721801

18731802
Cmd_AddCommand( "net_restart", NET_Restart_f );
18741803
}
@@ -1885,7 +1814,7 @@ void NET_Shutdown()
18851814
return;
18861815
}
18871816

1888-
NET_Config( false );
1817+
NET_DisableNetworking();
18891818

18901819
#ifdef _WIN32
18911820
WSACleanup();
@@ -1948,8 +1877,12 @@ NET_Restart_f
19481877
*/
19491878
void NET_Restart_f()
19501879
{
1951-
NET_Config( false );
1880+
NET_DisableNetworking();
19521881
SV_NET_Config();
19531882
Net::ShutDownDNS();
1954-
NET_Config( true );
1883+
#ifdef BUILD_SERVER
1884+
NET_EnableNetworking( true );
1885+
#else
1886+
NET_EnableNetworking( !!com_sv_running->integer );
1887+
#endif
19551888
}

src/engine/qcommon/qcommon.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ extern cvar_t *net_enabled;
149149
void NET_Init();
150150
void NET_Shutdown();
151151
void NET_Restart_f();
152-
void NET_Config( bool enableNetworking );
152+
void NET_EnableNetworking( bool serverMode );
153+
void NET_DisableNetworking();
153154

154155
void NET_SendPacket( netsrc_t sock, int length, const void *data, const netadr_t& to );
155156

src/engine/server/sv_init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ void SV_Startup()
342342

343343
Cvar_Set( "sv_running", "1" );
344344
#ifndef BUILD_SERVER
345-
NET_Config( true );
345+
NET_EnableNetworking( true );
346346
#endif
347347

348348
// Join the IPv6 multicast group now that a map is running, so clients can scan for us on the local network.
@@ -756,7 +756,7 @@ void SV_Shutdown( const char *finalmsg )
756756

757757
Cvar_Set( "sv_running", "0" );
758758
#ifndef BUILD_SERVER
759-
NET_Config( true );
759+
NET_EnableNetworking( false );
760760
#endif
761761

762762
SV_NET_Config(); // clear master server DNS queries

0 commit comments

Comments
 (0)