• Advertisement
Sign in to follow this  

virtual class members and packages

This topic is 4714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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{
 public:
  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
Advertisement
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
Thank you. I've taken a look at the serializer described in the Enginuity series and I think thats the way I'll go.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement