• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[C++] Game server design for MMORPG Emulator

Newbie Spellweaver
Joined
Aug 14, 2015
Messages
79
Reaction score
18
Kinda late on this, but instead of using a switch for handling by opcode, you can also use an array of handler objects. That makes it alot more organized in my opinion.

Hey thanks for the answer. Could you explain what do you mean by handler objects and how would I use them?
 
[emoji848]
Legend
Joined
Dec 3, 2011
Messages
2,232
Reaction score
1,518
Hey thanks for the answer. Could you explain what do you mean by handler objects and how would I use them?

I could imagine something like this to be very fast (C++ code):

Code:
// Array of function pointers having the size of all OP Codes
BOOL (*handlers[MAX_OP_CODE])(void);

// Then assign them at the op codes
handlers[OP_CODE_LOGIN] = &HandleLogin; // expecting: BOOL HandleLogin(void) { blablabla }
[...]

// Later you can just exec the handler in the array at the position of the OP code
BOOL result = handlers[opCode]();
 
Elite Diviner
Joined
Mar 24, 2015
Messages
426
Reaction score
416
Hey thanks for the answer. Could you explain what do you mean by handler objects and how would I use them?

Make an interface for handling packets and subclass it for each opcode. As in a command pattern:
 
Newbie Spellweaver
Joined
Aug 14, 2015
Messages
79
Reaction score
18
Thanks for the answers. There're 3 opcodes for each packet but I think I can use std::unordered_map for that. It looks much cleaner compared to switch loop. Something like:

Code:
std::unordered_map<std::string, UINT32> handler_map;
handler_map.insert(std::make_pair("040D15", OP_CODE_LOGIN));

// Array of function pointers having the size of all OP Codes
BOOL (*handlers[MAX_OP_CODE])(void);


// Then assign them at the op codes
handlers[OP_CODE_LOGIN] = &HandleLogin; // expecting: BOOL HandleLogin(void) { blablabla }
[...]


// Later you can just exec the handler in the array at the position of the OP code
BOOL result = handlers[handler_map["040D15"]]();
 
Last edited:
Back
Top