Skip to content

Commit c1bce4f

Browse files
authored
perf(network): Replace extern global network variables with const ones (TheSuperHackers#2726)
1 parent f7ecdb4 commit c1bce4f

4 files changed

Lines changed: 20 additions & 50 deletions

File tree

Core/GameEngine/Include/GameNetwork/NetworkDefs.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@
2727
#include "Lib/BaseType.h"
2828
#include "Common/MessageStream.h"
2929

30-
static const Int WOL_NAME_LEN = 64;
30+
static constexpr const Int WOL_NAME_LEN = 64;
3131

3232
/// Max number of commands per frame
33-
static const Int MAX_COMMANDS = 256;
33+
static constexpr const Int MAX_COMMANDS = 256;
3434

35-
extern Int MIN_LOGIC_FRAMES;
36-
extern Int MAX_FRAMES_AHEAD;
37-
extern Int MIN_RUNAHEAD;
35+
// TheSuperHackers @tweak Mauller 26/08/2025 reduce the minimum runahead from 10
36+
// This lets network games run at latencies down to 133ms when the network conditions allow
37+
static constexpr const Int MIN_LOGIC_FRAMES = 5;
38+
static constexpr const Int MAX_FRAMES_AHEAD = 128;
39+
static constexpr const Int MIN_RUNAHEAD = 4;
3840

3941
// FRAME_DATA_LENGTH needs to be MAX_FRAMES_AHEAD+1 because a player on a different
4042
// computer can send commands for a frame that is one beyond twice the max runahead.
41-
extern Int FRAME_DATA_LENGTH;
42-
extern Int FRAMES_TO_KEEP;
43+
static constexpr const Int FRAME_DATA_LENGTH = (MAX_FRAMES_AHEAD + 1) * 2;
44+
static constexpr const Int FRAMES_TO_KEEP = (MAX_FRAMES_AHEAD / 2) + 1;
4345

4446
// This is the connection numbering: 1-8 are for players
4547
enum ConnectionNumbers CPP_11(: Int)
@@ -85,7 +87,7 @@ static constexpr const Int MAX_MESSAGES = 256;
8587
* Command packet - contains frame #, total # of commands, and each command. This is what gets sent
8688
* to each player every frame
8789
*/
88-
static const Int numCommandsPerCommandPacket = (MAX_NETWORK_MESSAGE_LEN - sizeof(UnsignedInt) - sizeof(UnsignedShort))/sizeof(GameMessage);
90+
static constexpr const Int numCommandsPerCommandPacket = (MAX_NETWORK_MESSAGE_LEN - sizeof(UnsignedInt) - sizeof(UnsignedShort))/sizeof(GameMessage);
8991
#pragma pack(push, 1)
9092
struct CommandPacket
9193
{
@@ -193,38 +195,38 @@ enum PlayerLeaveCode CPP_11(: Int) {
193195
};
194196

195197
// Magic number for identifying a Generals packet.
196-
static const UnsignedShort GENERALS_MAGIC_NUMBER = 0xF00D;
198+
static constexpr const UnsignedShort GENERALS_MAGIC_NUMBER = 0xF00D;
197199

198200
// The number of fps history entries.
199-
//static const Int NETWORK_FPS_HISTORY_LENGTH = 30;
201+
//static constexpr const Int NETWORK_FPS_HISTORY_LENGTH = 30;
200202

201203
// The number of ping history entries.
202-
//static const Int NETWORK_LATENCY_HISTORY_LENGTH = 200;
204+
//static constexpr const Int NETWORK_LATENCY_HISTORY_LENGTH = 200;
203205

204206
// The number of miliseconds between run ahead metrics things
205-
//static const Int NETWORK_RUN_AHEAD_METRICS_TIME = 5000;
207+
//static constexpr const Int NETWORK_RUN_AHEAD_METRICS_TIME = 5000;
206208

207209
// The number of cushion values to keep.
208-
//static const Int NETWORK_CUSHION_HISTORY_LENGTH = 10;
210+
//static constexpr const Int NETWORK_CUSHION_HISTORY_LENGTH = 10;
209211

210212
// The amount of slack in the run ahead value. This is the percentage of the calculated run ahead that is added.
211-
//static const Int NETWORK_RUN_AHEAD_SLACK = 20;
213+
//static constexpr const Int NETWORK_RUN_AHEAD_SLACK = 20;
212214

213215
// The number of seconds between when the connections to each player send a keep-alive packet.
214216
// This should be less than 30 just to keep firewall ports open.
215-
//static const Int NETWORK_KEEPALIVE_DELAY = 20;
217+
//static constexpr const Int NETWORK_KEEPALIVE_DELAY = 20;
216218

217219
// The number of milliseconds between when the game gets stuck on a frame for a network stall and
218220
// and when the disconnect dialog comes up.
219-
//static const Int NETWORK_DISCONNECT_TIME = 5000;
221+
//static constexpr const Int NETWORK_DISCONNECT_TIME = 5000;
220222

221223
// The number of miliseconds between when a player's last disconnect keep alive command
222224
// was received and when they are considered disconnected from the game.
223-
//static const Int NETWORK_PLAYER_TIMEOUT_TIME = 60000;
225+
//static constexpr const Int NETWORK_PLAYER_TIMEOUT_TIME = 60000;
224226

225227
// The base port number used for the transport socket. A players slot number is added to this
226228
// value to get their actual port number.
227-
static const Int NETWORK_BASE_PORT_NUMBER = 8088;
229+
static constexpr const Int NETWORK_BASE_PORT_NUMBER = 8088;
228230

229231
// the singleton
230232
class NetworkInterface;

Core/GameEngine/Source/GameNetwork/NetworkUtil.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,6 @@
2727

2828
#include "GameNetwork/networkutil.h"
2929

30-
// TheSuperHackers @tweak Mauller 26/08/2025 reduce the minimum runahead from 10
31-
// This lets network games run at latencies down to 133ms when the network conditions allow
32-
Int MIN_LOGIC_FRAMES = 5;
33-
Int MAX_FRAMES_AHEAD = 128;
34-
Int MIN_RUNAHEAD = 4;
35-
Int FRAME_DATA_LENGTH = (MAX_FRAMES_AHEAD+1)*2;
36-
Int FRAMES_TO_KEEP = (MAX_FRAMES_AHEAD/2) + 1;
37-
3830
#ifdef DEBUG_LOGGING
3931

4032
void dumpBufferToLog(const void *vBuf, Int len, const char *fname, Int line)

Generals/Code/GameEngine/Source/Common/CommandLine.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -868,17 +868,6 @@ Int parseSelectAll( char *args[], int num )
868868

869869
return 1;
870870
}
871-
872-
Int parseRunAhead( char *args[], Int num )
873-
{
874-
if (num > 2)
875-
{
876-
MIN_RUNAHEAD = atoi(args[1]);
877-
MAX_FRAMES_AHEAD = atoi(args[2]);
878-
FRAME_DATA_LENGTH = (MAX_FRAMES_AHEAD + 1)*2;
879-
}
880-
return 3;
881-
}
882871
#endif
883872

884873

@@ -1279,7 +1268,6 @@ static CommandLineParam paramsForEngineInit[] =
12791268
{ "-logToCon", parseLogToConsole },
12801269
{ "-vTune", parseVTune },
12811270
{ "-selectTheUnselectable", parseSelectAll },
1282-
{ "-RunAhead", parseRunAhead },
12831271
#if ENABLE_CONFIGURABLE_SHROUD
12841272
{ "-noshroud", parseNoShroud },
12851273
#endif

GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -868,17 +868,6 @@ Int parseSelectAll( char *args[], int num )
868868

869869
return 1;
870870
}
871-
872-
Int parseRunAhead( char *args[], Int num )
873-
{
874-
if (num > 2)
875-
{
876-
MIN_RUNAHEAD = atoi(args[1]);
877-
MAX_FRAMES_AHEAD = atoi(args[2]);
878-
FRAME_DATA_LENGTH = (MAX_FRAMES_AHEAD + 1)*2;
879-
}
880-
return 3;
881-
}
882871
#endif
883872

884873

@@ -1279,7 +1268,6 @@ static CommandLineParam paramsForEngineInit[] =
12791268
{ "-logToCon", parseLogToConsole },
12801269
{ "-vTune", parseVTune },
12811270
{ "-selectTheUnselectable", parseSelectAll },
1282-
{ "-RunAhead", parseRunAhead },
12831271
#if ENABLE_CONFIGURABLE_SHROUD
12841272
{ "-noshroud", parseNoShroud },
12851273
#endif

0 commit comments

Comments
 (0)