Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[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