Skip to content

Commit 58809a3

Browse files
BoondorlRicardoLuis0
authored andcommitted
Fixed up game id
Store it in a proper buffer.
1 parent 7877018 commit 58809a3

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

src/common/engine/i_net.cpp

+13-15
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,14 @@ int consoleplayer = 0;
163163
int Net_Arbitrator = 0;
164164
FClientStack NetworkClients = {};
165165

166-
uint64_t GameID = 0u;
167166
uint8_t TicDup = 1u;
168167
int MaxClients = 1;
169168
int RemoteClient = -1;
170169
size_t NetBufferLength = 0u;
171170
uint8_t NetBuffer[MAX_MSGLEN] = {};
172171

173172
static FRandom GameIDGen = {};
173+
static uint8_t GameID[8] = {};
174174
static u_short GamePort = (IPPORT_USERRESERVED + 29);
175175
static SOCKET MySocket = INVALID_SOCKET;
176176
static FConnection Connected[MAXPLAYERS] = {};
@@ -348,6 +348,12 @@ static bool ClientsOnSameNetwork()
348348
return true;
349349
}
350350

351+
static void GenerateGameID()
352+
{
353+
const uint64_t val = GameIDGen.GenRand64();
354+
memcpy(GameID, &val, sizeof(val));
355+
}
356+
351357
// Print a network-related message to the console. This doesn't print to the window so should
352358
// not be used for that and is mainly for logging.
353359
static void I_NetLog(const char* text, ...)
@@ -506,7 +512,7 @@ static void SendPacket(const sockaddr_in& to)
506512
I_Error("Failed to compress data down to acceptable transmission size");
507513

508514
// If a connection packet, don't check the game id since they might not have it yet.
509-
const uint32_t crc = (NetBuffer[0] & NCMD_SETUP) ? CalcCRC32(dataStart, size) : AddCRC32(CalcCRC32(dataStart, size), (uint8_t*)&GameID, sizeof(GameID));
515+
const uint32_t crc = (NetBuffer[0] & NCMD_SETUP) ? CalcCRC32(dataStart, size) : AddCRC32(CalcCRC32(dataStart, size), GameID, std::extent_v<decltype(GameID)>);
510516
TransmitBuffer[0] = crc >> 24;
511517
TransmitBuffer[1] = crc >> 16;
512518
TransmitBuffer[2] = crc >> 8;
@@ -573,7 +579,7 @@ static void GetPacket(sockaddr_in* const from = nullptr)
573579
}
574580
else
575581
{
576-
const uint32_t check = (*dataStart & NCMD_SETUP) ? CalcCRC32(dataStart, msgSize - 4) : AddCRC32(CalcCRC32(dataStart, msgSize - 4), (uint8_t*)GameID, sizeof(GameID));
582+
const uint32_t check = (*dataStart & NCMD_SETUP) ? CalcCRC32(dataStart, msgSize - 4) : AddCRC32(CalcCRC32(dataStart, msgSize - 4), GameID, std::extent_v<decltype(GameID)>);
577583
const uint32_t crc = (TransmitBuffer[0] << 24) | (TransmitBuffer[1] << 16) | (TransmitBuffer[2] << 8) | TransmitBuffer[3];
578584
if (check != crc)
579585
{
@@ -874,14 +880,7 @@ static bool Host_CheckForConnections(void* connected)
874880
{
875881
NetBuffer[1] = PRE_GAME_INFO;
876882
NetBuffer[2] = TicDup;
877-
NetBuffer[3] = GameID >> 56;
878-
NetBuffer[4] = GameID >> 48;
879-
NetBuffer[5] = GameID >> 40;
880-
NetBuffer[6] = GameID >> 32;
881-
NetBuffer[7] = GameID >> 24;
882-
NetBuffer[8] = GameID >> 16;
883-
NetBuffer[9] = GameID >> 8;
884-
NetBuffer[10] = GameID;
883+
memcpy(&NetBuffer[3], GameID, 8);
885884
NetBufferLength = 11u;
886885

887886
uint8_t* stream = &NetBuffer[NetBufferLength];
@@ -966,7 +965,7 @@ static bool HostGame(int arg, bool forcedNetMode)
966965
if (MaxClients > MAXPLAYERS)
967966
I_FatalError("Cannot host a game with %u players. The limit is currently %u", MaxClients, MAXPLAYERS);
968967

969-
GameID = GameIDGen.GenRand64();
968+
GenerateGameID();
970969
NetworkClients += 0;
971970
Connected[consoleplayer].Status = CSTAT_READY;
972971
Net_SetupUserInfo();
@@ -1128,8 +1127,7 @@ static bool Guest_ContactHost(void* unused)
11281127
if (!Connected[consoleplayer].bHasGameInfo)
11291128
{
11301129
TicDup = clamp<int>(NetBuffer[2], 1, MAXTICDUP);
1131-
GameID = ((uint64_t)NetBuffer[3] << 56) | ((uint64_t)NetBuffer[4] << 48) | ((uint64_t)NetBuffer[5] << 40) | ((uint64_t)NetBuffer[6] << 32)
1132-
| ((uint64_t)NetBuffer[7] << 24) | ((uint64_t)NetBuffer[8] << 16) | ((uint64_t)NetBuffer[9] << 8) | (uint64_t)NetBuffer[10];
1130+
memcpy(GameID, &NetBuffer[3], 8);
11331131
uint8_t* stream = &NetBuffer[11];
11341132
Net_ReadGameInfo(stream);
11351133
Connected[consoleplayer].bHasGameInfo = true;
@@ -1293,7 +1291,7 @@ bool I_InitNetwork()
12931291
else
12941292
{
12951293
// single player game
1296-
GameID = GameIDGen.GenRand64();
1294+
GenerateGameID();
12971295
TicDup = 1;
12981296
NetworkClients += 0;
12991297
Connected[0].Status = CSTAT_READY;

src/common/engine/i_net.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ extern size_t NetBufferLength;
6666
extern uint8_t TicDup;
6767
extern int RemoteClient;
6868
extern int MaxClients;
69-
extern uint64_t GameID;
7069

7170
bool I_InitNetwork();
7271
void I_ClearClient(size_t client);
7372
void I_NetCmd(ENetCommand cmd);
7473
void I_NetDone();
7574
void HandleIncomingConnection();
75+
void CloseNetwork();
7676

7777
#endif

src/d_main.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ extern void M_SetDefaultMode ();
150150
extern void G_NewInit ();
151151
extern void SetupPlayerClasses ();
152152
void DeinitMenus();
153-
void CloseNetwork();
154153
void P_Shutdown();
155154
void M_SaveDefaultsFinal();
156155
void R_Shutdown();

src/d_net.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ static struct NetEventData
324324

325325
void Net_ClearBuffers()
326326
{
327+
CloseNetwork();
328+
327329
for (int i = 0; i < MAXPLAYERS; ++i)
328330
{
329331
playeringame[i] = false;
@@ -361,6 +363,7 @@ void Net_ClearBuffers()
361363
gametic = ClientTic = 0;
362364
SkipCommandTimer = SkipCommandAmount = CommandsAhead = 0;
363365
NetEvents.ResetStream();
366+
bCommandsReset = false;
364367

365368
LevelStartAck = 0u;
366369
LevelStartDelay = LevelStartDebug = 0;

0 commit comments

Comments
 (0)