Sign in to follow this  

virtual class members and packages

Recommended Posts

Hi, I would be very happy if someone could help me. I am working on a client/server architecture for a game engine. I planned to use a abstract class BaseMessage and derive all message the client/server could send/receive from it. BaseMessage looks like this:
class BaseMessage{
  virtual ~BaseMessage() {}
  virtual Execute() = 0; /* would be overriden by the code to be executed 
                           when the message is processed by the server */
  virtual Size() const = 0; // gives the size of a derived class

The server has a std::queue<BaseMessage*> and just calls message_queue.front()->Execute() on every message before poping it. This works fine as long as client and server are on the same machine and I don't use networking. But what will happen if I memcpy a class with virtual members? Enet (which I will use) does this to create packages. What is the deal with the v-table that is contained in virtual classes. It seems a class with virtual members is 4 bytes bigger than a class without. Is this a pointer to the v-table (which would be bad) or is this an index for the v-table that would be the same on another computer? Can I just create an Enet package of my Messages derived from BaseMessage or should I think of another solution?

Share this post

Link to post
Share on other sites
You need to serialize the message to a byte array when sending, and de-serialize from byte array when receiving.

Thus, with your proposed design, you probably want BaseMessage to have a virtual size_t Serialize(void *, size_t) = 0 method, and a static BaseMessage * Deserialize( void const * &, size_t & ) method (which would be a factory for message-from-data-stream).

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this