tags:

views:

77

answers:

3

Hey,

I've been writing a networking server for a while now in C++ and have come to the stage to start looking for a way to properly and easily handle all packets.

I am so far that I can figure out what kind of packet it is, but now I need to figure out how to get the needed data to the handler functions.

I had the following in mind:

  • Have a map of function pointers with the opcode as key and the function pointer as value
  • Have all these functions have 2 arguments, packet and ObjectAccessor
  • ObjectAccessor class contains various functions to fetch various items such as users and alike
  • Perhaps pass the user's guid too so we can fetch it from the objectaccessor

I'd like to know the various implementations others have come up with, so please comment on this idea and reply with your own implementations.

Thanks, Xeross

A: 

In the past I had created an XML message and in the Message provided a command field. Since I only had few commands I just used a bunch of if statements to see what the command was and then called a function based on the command, passing in the payload part of the XML.

Romain Hippeau
A: 

You might what to take a look at some existing solutions. YAMI is a simple messaging system that you might want to look at. (either as a replacement to what you currently have or to get an idea of how handler functions are registered/called)

I have not looked at YAMI4 yet so I can't comment on it, but YAMI 3.x quite easy to get up and running quickly and the documentation should give you an idea of how to access your handler functions.

Chris Mc
A: 

I have implemented a handler class in conjunction with an array of function pointers, works beautifully.

Xeross