Skip to content

Commit 08c8f8b

Browse files
committed
refactor(network): convert some if-else chains to switch statements
1 parent 35b3f01 commit 08c8f8b

File tree

3 files changed

+134
-134
lines changed

3 files changed

+134
-134
lines changed

Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp

Lines changed: 69 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -420,25 +420,14 @@ 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-
428-
processAck(msg);
429-
return FALSE;
430-
}
431-
425+
// Early validation checks
432426
if ((m_connections[msg->getPlayerID()] == NULL) && (msg->getPlayerID() != m_localSlot)) {
433427
// if this is from a player that is no longer in the game, then ignore them.
434428
return TRUE;
435429
}
436430

437-
if (msg->getNetCommandType() == NETCOMMANDTYPE_WRAPPER) {
438-
processWrapper(ref); // need to send the NetCommandRef since we have to construct the relay for the wrapped command.
439-
return FALSE;
440-
}
441-
442431
if ((msg->getPlayerID() >= 0) && (msg->getPlayerID() < MAX_SLOTS) && (msg->getPlayerID() != m_localSlot)) {
443432
if (m_connections[msg->getPlayerID()] == NULL) {
444433
return TRUE;
@@ -451,93 +440,93 @@ Bool ConnectionManager::processNetCommand(NetCommandRef *ref) {
451440
// This was a fix for a command count bug where a command would be
452441
// executed, then a command for that old frame would be added to the
453442
// FrameData for that frame + 256, and would screw up the command count.
454-
455-
if (IsCommandSynchronized(msg->getNetCommandType())) {
443+
if (IsCommandSynchronized(cmdType)) {
456444
if (ref->getCommand()->getExecutionFrame() < TheGameLogic->getFrame()) {
457445
return TRUE;
458446
}
459447
}
460448

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;
449+
// Handle disconnect commands as a range
450+
if ((cmdType > NETCOMMANDTYPE_DISCONNECTSTART) && (cmdType < NETCOMMANDTYPE_DISCONNECTEND)) {
451+
m_disconnectManager->processDisconnectCommand(ref, this);
452+
return TRUE;
469453
}
470454

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);
455+
// Process command by type
456+
switch (cmdType) {
457+
case NETCOMMANDTYPE_ACKSTAGE1:
458+
case NETCOMMANDTYPE_ACKSTAGE2:
459+
case NETCOMMANDTYPE_ACKBOTH:
460+
processAck(msg);
461+
return FALSE;
475462

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-
}
463+
case NETCOMMANDTYPE_WRAPPER:
464+
processWrapper(ref); // need to send the NetCommandRef since we have to construct the relay for the wrapped command.
465+
return FALSE;
482466

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-
}
467+
case NETCOMMANDTYPE_FRAMEINFO: {
468+
processFrameInfo((NetFrameCommandMsg *)msg);
469+
// need to set the relay so we don't send it to ourselves.
470+
UnsignedByte relay = ref->getRelay();
471+
relay = relay & (0xff ^ (1 << m_localSlot));
472+
ref->setRelay(relay);
473+
return FALSE;
474+
}
489475

490-
if (msg->getNetCommandType() == NETCOMMANDTYPE_RUNAHEADMETRICS) {
491-
processRunAheadMetrics((NetRunAheadMetricsCommandMsg *)msg);
492-
return TRUE;
493-
}
476+
case NETCOMMANDTYPE_PROGRESS: {
477+
//DEBUG_LOG(("ConnectionManager::processNetCommand - got a progress net command from player %d", msg->getPlayerID()));
478+
processProgress((NetProgressCommandMsg *) msg);
479+
// need to set the relay so we don't send it to ourselves.
480+
UnsignedByte relay = ref->getRelay();
481+
relay = relay & (0xff ^ (1 << m_localSlot));
482+
ref->setRelay(relay);
483+
return FALSE;
484+
}
494485

495-
if (msg->getNetCommandType() == NETCOMMANDTYPE_KEEPALIVE) {
496-
return TRUE;
497-
}
486+
case NETCOMMANDTYPE_TIMEOUTSTART:
487+
DEBUG_LOG(("ConnectionManager::processNetCommand - got a TimeOut GameStart net command from player %d", msg->getPlayerID()));
488+
processTimeOutGameStart(msg);
489+
return FALSE;
498490

499-
if ((msg->getNetCommandType() > NETCOMMANDTYPE_DISCONNECTSTART) && (msg->getNetCommandType() < NETCOMMANDTYPE_DISCONNECTEND)) {
500-
m_disconnectManager->processDisconnectCommand(ref, this);
501-
return TRUE;
502-
}
491+
case NETCOMMANDTYPE_RUNAHEADMETRICS:
492+
processRunAheadMetrics((NetRunAheadMetricsCommandMsg *)msg);
493+
return TRUE;
503494

504-
if (msg->getNetCommandType() == NETCOMMANDTYPE_DISCONNECTCHAT) {
505-
processDisconnectChat((NetDisconnectChatCommandMsg *)msg);
506-
}
495+
case NETCOMMANDTYPE_KEEPALIVE:
496+
return TRUE;
507497

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-
}
498+
case NETCOMMANDTYPE_DISCONNECTCHAT:
499+
processDisconnectChat((NetDisconnectChatCommandMsg *)msg);
500+
return FALSE;
514501

515-
if (msg->getNetCommandType() == NETCOMMANDTYPE_CHAT) {
516-
processChat((NetChatCommandMsg *)msg);
517-
return FALSE;
518-
}
502+
case NETCOMMANDTYPE_LOADCOMPLETE:
503+
DEBUG_LOG(("ConnectionManager::processNetCommand - got a Load Complete net command from player %d", msg->getPlayerID()));
504+
processLoadComplete(msg);
505+
return FALSE;
519506

520-
if (msg->getNetCommandType() == NETCOMMANDTYPE_FILE) {
521-
processFile((NetFileCommandMsg *)msg);
522-
return FALSE;
523-
}
507+
case NETCOMMANDTYPE_CHAT:
508+
processChat((NetChatCommandMsg *)msg);
509+
return FALSE;
524510

525-
if (msg->getNetCommandType() == NETCOMMANDTYPE_FILEANNOUNCE) {
526-
processFileAnnounce((NetFileAnnounceCommandMsg *)msg);
527-
return FALSE;
528-
}
511+
case NETCOMMANDTYPE_FILE:
512+
processFile((NetFileCommandMsg *)msg);
513+
return FALSE;
529514

530-
if (msg->getNetCommandType() == NETCOMMANDTYPE_FILEPROGRESS) {
531-
processFileProgress((NetFileProgressCommandMsg *)msg);
532-
return FALSE;
533-
}
515+
case NETCOMMANDTYPE_FILEANNOUNCE:
516+
processFileAnnounce((NetFileAnnounceCommandMsg *)msg);
517+
return FALSE;
534518

535-
if (msg->getNetCommandType() == NETCOMMANDTYPE_FRAMERESENDREQUEST) {
536-
processFrameResendRequest((NetFrameResendRequestCommandMsg *)msg);
537-
return TRUE;
538-
}
519+
case NETCOMMANDTYPE_FILEPROGRESS:
520+
processFileProgress((NetFileProgressCommandMsg *)msg);
521+
return FALSE;
522+
523+
case NETCOMMANDTYPE_FRAMERESENDREQUEST:
524+
processFrameResendRequest((NetFrameResendRequestCommandMsg *)msg);
525+
return TRUE;
539526

540-
return FALSE;
527+
default:
528+
return FALSE;
529+
}
541530
}
542531

543532
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)