virtual class members and packages

Started by
1 comment, last by Mav 19 years ago
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?
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).
enum Bool { True, False, FileNotFound };
Thank you. I've taken a look at the serializer described in the Enginuity series and I think thats the way I'll go.

This topic is closed to new replies.

Advertisement