Skip to content

Commit ae99f38

Browse files
committed
refactor(network): convert some if-else chains to switch statements in Generals
1 parent 1e402be commit ae99f38

File tree

3 files changed

+134
-134
lines changed

3 files changed

+134
-134
lines changed

Generals/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp

Lines changed: 69 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -390,25 +390,14 @@ void ConnectionManager::doRelay() {
390390
*/
391391
Bool ConnectionManager::processNetCommand(NetCommandRef *ref) {
392392
NetCommandMsg *msg = ref->getCommand();
393+
NetCommandType cmdType = msg->getNetCommandType();
393394

394-
if ((msg->getNetCommandType() == NETCOMMANDTYPE_ACKSTAGE1) ||
395-
(msg->getNetCommandType() == NETCOMMANDTYPE_ACKSTAGE2) ||
396-
(msg->getNetCommandType() == NETCOMMANDTYPE_ACKBOTH)) {
397-
398-
processAck(msg);
399-
return FALSE;
400-
}
401-
395+
// Early validation checks
402396
if ((m_connections[msg->getPlayerID()] == NULL) && (msg->getPlayerID() != m_localSlot)) {
403397
// if this is from a player that is no longer in the game, then ignore them.
404398
return TRUE;
405399
}
406400

407-
if (msg->getNetCommandType() == NETCOMMANDTYPE_WRAPPER) {
408-
processWrapper(ref); // need to send the NetCommandRef since we have to construct the relay for the wrapped command.
409-
return FALSE;
410-
}
411-
412401
if ((msg->getPlayerID() >= 0) && (msg->getPlayerID() < MAX_SLOTS) && (msg->getPlayerID() != m_localSlot)) {
413402
if (m_connections[msg->getPlayerID()] == NULL) {
414403
return TRUE;
@@ -421,93 +410,93 @@ Bool ConnectionManager::processNetCommand(NetCommandRef *ref) {
421410
// This was a fix for a command count bug where a command would be
422411
// executed, then a command for that old frame would be added to the
423412
// FrameData for that frame + 256, and would screw up the command count.
424-
425-
if (IsCommandSynchronized(msg->getNetCommandType())) {
413+
if (IsCommandSynchronized(cmdType)) {
426414
if (ref->getCommand()->getExecutionFrame() < TheGameLogic->getFrame()) {
427415
return TRUE;
428416
}
429417
}
430418

431-
if (msg->getNetCommandType() == NETCOMMANDTYPE_FRAMEINFO) {
432-
processFrameInfo((NetFrameCommandMsg *)msg);
433-
434-
// need to set the relay so we don't send it to ourselves.
435-
UnsignedByte relay = ref->getRelay();
436-
relay = relay & (0xff ^ (1 << m_localSlot));
437-
ref->setRelay(relay);
438-
return FALSE;
419+
// Handle disconnect commands as a range
420+
if ((cmdType > NETCOMMANDTYPE_DISCONNECTSTART) && (cmdType < NETCOMMANDTYPE_DISCONNECTEND)) {
421+
m_disconnectManager->processDisconnectCommand(ref, this);
422+
return TRUE;
439423
}
440424

441-
if (msg->getNetCommandType() == NETCOMMANDTYPE_PROGRESS)
442-
{
443-
//DEBUG_LOG(("ConnectionManager::processNetCommand - got a progress net command from player %d", msg->getPlayerID()));
444-
processProgress((NetProgressCommandMsg *) msg);
425+
// Process command by type
426+
switch (cmdType) {
427+
case NETCOMMANDTYPE_ACKSTAGE1:
428+
case NETCOMMANDTYPE_ACKSTAGE2:
429+
case NETCOMMANDTYPE_ACKBOTH:
430+
processAck(msg);
431+
return FALSE;
445432

446-
// need to set the relay so we don't send it to ourselves.
447-
UnsignedByte relay = ref->getRelay();
448-
relay = relay & (0xff ^ (1 << m_localSlot));
449-
ref->setRelay(relay);
450-
return FALSE;
451-
}
433+
case NETCOMMANDTYPE_WRAPPER:
434+
processWrapper(ref); // need to send the NetCommandRef since we have to construct the relay for the wrapped command.
435+
return FALSE;
452436

453-
if (msg->getNetCommandType() == NETCOMMANDTYPE_TIMEOUTSTART)
454-
{
455-
DEBUG_LOG(("ConnectionManager::processNetCommand - got a TimeOut GameStart net command from player %d", msg->getPlayerID()));
456-
processTimeOutGameStart(msg);
457-
return FALSE;
458-
}
437+
case NETCOMMANDTYPE_FRAMEINFO: {
438+
processFrameInfo((NetFrameCommandMsg *)msg);
439+
// need to set the relay so we don't send it to ourselves.
440+
UnsignedByte relay = ref->getRelay();
441+
relay = relay & (0xff ^ (1 << m_localSlot));
442+
ref->setRelay(relay);
443+
return FALSE;
444+
}
459445

460-
if (msg->getNetCommandType() == NETCOMMANDTYPE_RUNAHEADMETRICS) {
461-
processRunAheadMetrics((NetRunAheadMetricsCommandMsg *)msg);
462-
return TRUE;
463-
}
446+
case NETCOMMANDTYPE_PROGRESS: {
447+
//DEBUG_LOG(("ConnectionManager::processNetCommand - got a progress net command from player %d", msg->getPlayerID()));
448+
processProgress((NetProgressCommandMsg *) msg);
449+
// need to set the relay so we don't send it to ourselves.
450+
UnsignedByte relay = ref->getRelay();
451+
relay = relay & (0xff ^ (1 << m_localSlot));
452+
ref->setRelay(relay);
453+
return FALSE;
454+
}
464455

465-
if (msg->getNetCommandType() == NETCOMMANDTYPE_KEEPALIVE) {
466-
return TRUE;
467-
}
456+
case NETCOMMANDTYPE_TIMEOUTSTART:
457+
DEBUG_LOG(("ConnectionManager::processNetCommand - got a TimeOut GameStart net command from player %d", msg->getPlayerID()));
458+
processTimeOutGameStart(msg);
459+
return FALSE;
468460

469-
if ((msg->getNetCommandType() > NETCOMMANDTYPE_DISCONNECTSTART) && (msg->getNetCommandType() < NETCOMMANDTYPE_DISCONNECTEND)) {
470-
m_disconnectManager->processDisconnectCommand(ref, this);
471-
return TRUE;
472-
}
461+
case NETCOMMANDTYPE_RUNAHEADMETRICS:
462+
processRunAheadMetrics((NetRunAheadMetricsCommandMsg *)msg);
463+
return TRUE;
473464

474-
if (msg->getNetCommandType() == NETCOMMANDTYPE_DISCONNECTCHAT) {
475-
processDisconnectChat((NetDisconnectChatCommandMsg *)msg);
476-
}
465+
case NETCOMMANDTYPE_KEEPALIVE:
466+
return TRUE;
477467

478-
if (msg->getNetCommandType() == NETCOMMANDTYPE_LOADCOMPLETE)
479-
{
480-
DEBUG_LOG(("ConnectionManager::processNetCommand - got a Load Complete net command from player %d", msg->getPlayerID()));
481-
processLoadComplete(msg);
482-
return FALSE;
483-
}
468+
case NETCOMMANDTYPE_DISCONNECTCHAT:
469+
processDisconnectChat((NetDisconnectChatCommandMsg *)msg);
470+
return FALSE;
484471

485-
if (msg->getNetCommandType() == NETCOMMANDTYPE_CHAT) {
486-
processChat((NetChatCommandMsg *)msg);
487-
return FALSE;
488-
}
472+
case NETCOMMANDTYPE_LOADCOMPLETE:
473+
DEBUG_LOG(("ConnectionManager::processNetCommand - got a Load Complete net command from player %d", msg->getPlayerID()));
474+
processLoadComplete(msg);
475+
return FALSE;
489476

490-
if (msg->getNetCommandType() == NETCOMMANDTYPE_FILE) {
491-
processFile((NetFileCommandMsg *)msg);
492-
return FALSE;
493-
}
477+
case NETCOMMANDTYPE_CHAT:
478+
processChat((NetChatCommandMsg *)msg);
479+
return FALSE;
494480

495-
if (msg->getNetCommandType() == NETCOMMANDTYPE_FILEANNOUNCE) {
496-
processFileAnnounce((NetFileAnnounceCommandMsg *)msg);
497-
return FALSE;
498-
}
481+
case NETCOMMANDTYPE_FILE:
482+
processFile((NetFileCommandMsg *)msg);
483+
return FALSE;
499484

500-
if (msg->getNetCommandType() == NETCOMMANDTYPE_FILEPROGRESS) {
501-
processFileProgress((NetFileProgressCommandMsg *)msg);
502-
return FALSE;
503-
}
485+
case NETCOMMANDTYPE_FILEANNOUNCE:
486+
processFileAnnounce((NetFileAnnounceCommandMsg *)msg);
487+
return FALSE;
504488

505-
if (msg->getNetCommandType() == NETCOMMANDTYPE_FRAMERESENDREQUEST) {
506-
processFrameResendRequest((NetFrameResendRequestCommandMsg *)msg);
507-
return TRUE;
508-
}
489+
case NETCOMMANDTYPE_FILEPROGRESS:
490+
processFileProgress((NetFileProgressCommandMsg *)msg);
491+
return FALSE;
492+
493+
case NETCOMMANDTYPE_FRAMERESENDREQUEST:
494+
processFrameResendRequest((NetFrameResendRequestCommandMsg *)msg);
495+
return TRUE;
509496

510-
return FALSE;
497+
default:
498+
return FALSE;
499+
}
511500
}
512501

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

Generals/Code/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

Generals/Code/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)