Skip to content

Commit a2c1d0e

Browse files
authored
refactor(network): Convert some if-else chains to switch statements (#1682)
1 parent 20f88f4 commit a2c1d0e

File tree

3 files changed

+131
-126
lines changed

3 files changed

+131
-126
lines changed

Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp

Lines changed: 66 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -420,21 +420,24 @@ void ConnectionManager::doRelay() {
420420
*/
421421
Bool ConnectionManager::processNetCommand(NetCommandRef *ref) {
422422
NetCommandMsg *msg = ref->getCommand();
423+
NetCommandType cmdType = msg->getNetCommandType();
423424

424-
if ((msg->getNetCommandType() == NETCOMMANDTYPE_ACKSTAGE1) ||
425-
(msg->getNetCommandType() == NETCOMMANDTYPE_ACKSTAGE2) ||
426-
(msg->getNetCommandType() == NETCOMMANDTYPE_ACKBOTH)) {
427-
425+
// Handle ACK commands first (before connection validation)
426+
if ((cmdType == NETCOMMANDTYPE_ACKSTAGE1) ||
427+
(cmdType == NETCOMMANDTYPE_ACKSTAGE2) ||
428+
(cmdType == NETCOMMANDTYPE_ACKBOTH)) {
428429
processAck(msg);
429430
return FALSE;
430431
}
431432

433+
// Early validation checks
432434
if ((m_connections[msg->getPlayerID()] == NULL) && (msg->getPlayerID() != m_localSlot)) {
433435
// if this is from a player that is no longer in the game, then ignore them.
434436
return TRUE;
435437
}
436438

437-
if (msg->getNetCommandType() == NETCOMMANDTYPE_WRAPPER) {
439+
// Handle WRAPPER commands (before second connection validation)
440+
if (cmdType == NETCOMMANDTYPE_WRAPPER) {
438441
processWrapper(ref); // need to send the NetCommandRef since we have to construct the relay for the wrapped command.
439442
return FALSE;
440443
}
@@ -451,93 +454,84 @@ Bool ConnectionManager::processNetCommand(NetCommandRef *ref) {
451454
// This was a fix for a command count bug where a command would be
452455
// executed, then a command for that old frame would be added to the
453456
// FrameData for that frame + 256, and would screw up the command count.
454-
455-
if (IsCommandSynchronized(msg->getNetCommandType())) {
457+
if (IsCommandSynchronized(cmdType)) {
456458
if (ref->getCommand()->getExecutionFrame() < TheGameLogic->getFrame()) {
457459
return TRUE;
458460
}
459461
}
460462

461-
if (msg->getNetCommandType() == NETCOMMANDTYPE_FRAMEINFO) {
462-
processFrameInfo((NetFrameCommandMsg *)msg);
463-
464-
// need to set the relay so we don't send it to ourselves.
465-
UnsignedByte relay = ref->getRelay();
466-
relay = relay & (0xff ^ (1 << m_localSlot));
467-
ref->setRelay(relay);
468-
return FALSE;
463+
// Handle disconnect commands as a range
464+
if ((cmdType > NETCOMMANDTYPE_DISCONNECTSTART) && (cmdType < NETCOMMANDTYPE_DISCONNECTEND)) {
465+
m_disconnectManager->processDisconnectCommand(ref, this);
466+
return TRUE;
469467
}
470468

471-
if (msg->getNetCommandType() == NETCOMMANDTYPE_PROGRESS)
472-
{
473-
//DEBUG_LOG(("ConnectionManager::processNetCommand - got a progress net command from player %d", msg->getPlayerID()));
474-
processProgress((NetProgressCommandMsg *) msg);
469+
// Process command by type
470+
switch (cmdType) {
475471

476-
// need to set the relay so we don't send it to ourselves.
477-
UnsignedByte relay = ref->getRelay();
478-
relay = relay & (0xff ^ (1 << m_localSlot));
479-
ref->setRelay(relay);
480-
return FALSE;
481-
}
472+
case NETCOMMANDTYPE_FRAMEINFO: {
473+
processFrameInfo((NetFrameCommandMsg *)msg);
474+
// need to set the relay so we don't send it to ourselves.
475+
UnsignedByte relay = ref->getRelay();
476+
relay = relay & (0xff ^ (1 << m_localSlot));
477+
ref->setRelay(relay);
478+
return FALSE;
479+
}
482480

483-
if (msg->getNetCommandType() == NETCOMMANDTYPE_TIMEOUTSTART)
484-
{
485-
DEBUG_LOG(("ConnectionManager::processNetCommand - got a TimeOut GameStart net command from player %d", msg->getPlayerID()));
486-
processTimeOutGameStart(msg);
487-
return FALSE;
488-
}
481+
case NETCOMMANDTYPE_PROGRESS: {
482+
//DEBUG_LOG(("ConnectionManager::processNetCommand - got a progress net command from player %d", msg->getPlayerID()));
483+
processProgress((NetProgressCommandMsg *) msg);
484+
// need to set the relay so we don't send it to ourselves.
485+
UnsignedByte relay = ref->getRelay();
486+
relay = relay & (0xff ^ (1 << m_localSlot));
487+
ref->setRelay(relay);
488+
return FALSE;
489+
}
489490

490-
if (msg->getNetCommandType() == NETCOMMANDTYPE_RUNAHEADMETRICS) {
491-
processRunAheadMetrics((NetRunAheadMetricsCommandMsg *)msg);
492-
return TRUE;
493-
}
491+
case NETCOMMANDTYPE_TIMEOUTSTART:
492+
DEBUG_LOG(("ConnectionManager::processNetCommand - got a TimeOut GameStart net command from player %d", msg->getPlayerID()));
493+
processTimeOutGameStart(msg);
494+
return FALSE;
494495

495-
if (msg->getNetCommandType() == NETCOMMANDTYPE_KEEPALIVE) {
496-
return TRUE;
497-
}
496+
case NETCOMMANDTYPE_RUNAHEADMETRICS:
497+
processRunAheadMetrics((NetRunAheadMetricsCommandMsg *)msg);
498+
return TRUE;
498499

499-
if ((msg->getNetCommandType() > NETCOMMANDTYPE_DISCONNECTSTART) && (msg->getNetCommandType() < NETCOMMANDTYPE_DISCONNECTEND)) {
500-
m_disconnectManager->processDisconnectCommand(ref, this);
501-
return TRUE;
502-
}
500+
case NETCOMMANDTYPE_KEEPALIVE:
501+
return TRUE;
503502

504-
if (msg->getNetCommandType() == NETCOMMANDTYPE_DISCONNECTCHAT) {
505-
processDisconnectChat((NetDisconnectChatCommandMsg *)msg);
506-
}
503+
case NETCOMMANDTYPE_DISCONNECTCHAT:
504+
processDisconnectChat((NetDisconnectChatCommandMsg *)msg);
505+
return FALSE;
507506

508-
if (msg->getNetCommandType() == NETCOMMANDTYPE_LOADCOMPLETE)
509-
{
510-
DEBUG_LOG(("ConnectionManager::processNetCommand - got a Load Complete net command from player %d", msg->getPlayerID()));
511-
processLoadComplete(msg);
512-
return FALSE;
513-
}
507+
case NETCOMMANDTYPE_LOADCOMPLETE:
508+
DEBUG_LOG(("ConnectionManager::processNetCommand - got a Load Complete net command from player %d", msg->getPlayerID()));
509+
processLoadComplete(msg);
510+
return FALSE;
514511

515-
if (msg->getNetCommandType() == NETCOMMANDTYPE_CHAT) {
516-
processChat((NetChatCommandMsg *)msg);
517-
return FALSE;
518-
}
512+
case NETCOMMANDTYPE_CHAT:
513+
processChat((NetChatCommandMsg *)msg);
514+
return FALSE;
519515

520-
if (msg->getNetCommandType() == NETCOMMANDTYPE_FILE) {
521-
processFile((NetFileCommandMsg *)msg);
522-
return FALSE;
523-
}
516+
case NETCOMMANDTYPE_FILE:
517+
processFile((NetFileCommandMsg *)msg);
518+
return FALSE;
524519

525-
if (msg->getNetCommandType() == NETCOMMANDTYPE_FILEANNOUNCE) {
526-
processFileAnnounce((NetFileAnnounceCommandMsg *)msg);
527-
return FALSE;
528-
}
520+
case NETCOMMANDTYPE_FILEANNOUNCE:
521+
processFileAnnounce((NetFileAnnounceCommandMsg *)msg);
522+
return FALSE;
529523

530-
if (msg->getNetCommandType() == NETCOMMANDTYPE_FILEPROGRESS) {
531-
processFileProgress((NetFileProgressCommandMsg *)msg);
532-
return FALSE;
533-
}
524+
case NETCOMMANDTYPE_FILEPROGRESS:
525+
processFileProgress((NetFileProgressCommandMsg *)msg);
526+
return FALSE;
534527

535-
if (msg->getNetCommandType() == NETCOMMANDTYPE_FRAMERESENDREQUEST) {
536-
processFrameResendRequest((NetFrameResendRequestCommandMsg *)msg);
537-
return TRUE;
538-
}
528+
case NETCOMMANDTYPE_FRAMERESENDREQUEST:
529+
processFrameResendRequest((NetFrameResendRequestCommandMsg *)msg);
530+
return TRUE;
539531

540-
return FALSE;
532+
default:
533+
return FALSE;
534+
}
541535
}
542536

543537
void ConnectionManager::processFrameResendRequest(NetFrameResendRequestCommandMsg *msg) {

Core/GameEngine/Source/GameNetwork/DisconnectManager.cpp

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -268,20 +268,38 @@ void DisconnectManager::updateWaitForPacketRouter(ConnectionManager *conMgr) {
268268

269269
void DisconnectManager::processDisconnectCommand(NetCommandRef *ref, ConnectionManager *conMgr) {
270270
NetCommandMsg *msg = ref->getCommand();
271-
if (msg->getNetCommandType() == NETCOMMANDTYPE_DISCONNECTKEEPALIVE) {
272-
processDisconnectKeepAlive(msg, conMgr);
273-
} else if (msg->getNetCommandType() == NETCOMMANDTYPE_DISCONNECTPLAYER) {
274-
processDisconnectPlayer(msg, conMgr);
275-
} else if (msg->getNetCommandType() == NETCOMMANDTYPE_PACKETROUTERQUERY) {
276-
processPacketRouterQuery(msg, conMgr);
277-
} else if (msg->getNetCommandType() == NETCOMMANDTYPE_PACKETROUTERACK) {
278-
processPacketRouterAck(msg, conMgr);
279-
} else if (msg->getNetCommandType() == NETCOMMANDTYPE_DISCONNECTVOTE) {
280-
processDisconnectVote(msg, conMgr);
281-
} else if (msg->getNetCommandType() == NETCOMMANDTYPE_DISCONNECTFRAME) {
282-
processDisconnectFrame(msg, conMgr);
283-
} else if (msg->getNetCommandType() == NETCOMMANDTYPE_DISCONNECTSCREENOFF) {
284-
processDisconnectScreenOff(msg, conMgr);
271+
272+
switch (msg->getNetCommandType()) {
273+
case NETCOMMANDTYPE_DISCONNECTKEEPALIVE:
274+
processDisconnectKeepAlive(msg, conMgr);
275+
break;
276+
277+
case NETCOMMANDTYPE_DISCONNECTPLAYER:
278+
processDisconnectPlayer(msg, conMgr);
279+
break;
280+
281+
case NETCOMMANDTYPE_PACKETROUTERQUERY:
282+
processPacketRouterQuery(msg, conMgr);
283+
break;
284+
285+
case NETCOMMANDTYPE_PACKETROUTERACK:
286+
processPacketRouterAck(msg, conMgr);
287+
break;
288+
289+
case NETCOMMANDTYPE_DISCONNECTVOTE:
290+
processDisconnectVote(msg, conMgr);
291+
break;
292+
293+
case NETCOMMANDTYPE_DISCONNECTFRAME:
294+
processDisconnectFrame(msg, conMgr);
295+
break;
296+
297+
case NETCOMMANDTYPE_DISCONNECTSCREENOFF:
298+
processDisconnectScreenOff(msg, conMgr);
299+
break;
300+
301+
default:
302+
break;
285303
}
286304
}
287305

Core/GameEngine/Source/GameNetwork/FirewallHelper.cpp

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -204,47 +204,40 @@ Bool FirewallHelperClass::detectFirewall(void)
204204

205205
Bool FirewallHelperClass::behaviorDetectionUpdate()
206206
{
207-
if (m_currentState == DETECTIONSTATE_IDLE) {
208-
return FALSE;
209-
}
210-
211-
if (m_currentState == DETECTIONSTATE_DONE) {
212-
return TRUE;
213-
}
214-
215-
if (m_currentState == DETECTIONSTATE_BEGIN) {
216-
return detectionBeginUpdate();
217-
}
218-
219-
if (m_currentState == DETECTIONSTATE_TEST1) {
220-
return detectionTest1Update();
221-
}
222-
223-
if (m_currentState == DETECTIONSTATE_TEST2) {
224-
return detectionTest2Update();
225-
}
226-
227-
if (m_currentState == DETECTIONSTATE_TEST3) {
228-
return detectionTest3Update();
229-
}
230-
231-
if (m_currentState == DETECTIONSTATE_TEST3_WAITFORRESPONSES) {
232-
return detectionTest3WaitForResponsesUpdate();
233-
}
234-
235-
if (m_currentState == DETECTIONSTATE_TEST4_1) {
236-
return detectionTest4Stage1Update();
237-
}
238-
239-
if (m_currentState == DETECTIONSTATE_TEST4_2) {
240-
return detectionTest4Stage2Update();
241-
}
242-
243-
if (m_currentState == DETECTIONSTATE_TEST5) {
244-
return detectionTest5Update();
207+
switch (m_currentState) {
208+
case DETECTIONSTATE_IDLE:
209+
return FALSE;
210+
211+
case DETECTIONSTATE_DONE:
212+
return TRUE;
213+
214+
case DETECTIONSTATE_BEGIN:
215+
return detectionBeginUpdate();
216+
217+
case DETECTIONSTATE_TEST1:
218+
return detectionTest1Update();
219+
220+
case DETECTIONSTATE_TEST2:
221+
return detectionTest2Update();
222+
223+
case DETECTIONSTATE_TEST3:
224+
return detectionTest3Update();
225+
226+
case DETECTIONSTATE_TEST3_WAITFORRESPONSES:
227+
return detectionTest3WaitForResponsesUpdate();
228+
229+
case DETECTIONSTATE_TEST4_1:
230+
return detectionTest4Stage1Update();
231+
232+
case DETECTIONSTATE_TEST4_2:
233+
return detectionTest4Stage2Update();
234+
235+
case DETECTIONSTATE_TEST5:
236+
return detectionTest5Update();
237+
238+
default:
239+
return TRUE;
245240
}
246-
247-
return TRUE;
248241
}
249242

250243
/***********************************************************************************************

0 commit comments

Comments
 (0)