Question

I need to serialize different objects (to messages) and send via tcp connection to server (both clients and server are c++) and I cannot use protobuf (only boost library is available). Is there already algorithm or example, or tutorial how one wood do this ( I need to consider that messages are going to change, new version with additional fields).

class Message {
public:
   char type;
   char version;
};

class Register : public Message {
public:
   // version 1 of Register message
   string first_name;
   string last_name;
};

class Register : public Message {
public:
   // version 2 of Register message
   string first_name;
   string last_name;
   long salary;
};

class ChangeName : public Message {
public:
   // version 1 of ChangeName message 
   string first_name;
};


// this is going to be transmitted ower tcp (length + type + version + data) like array of bytes
class TransmissionMessage {
public:
   uint32_t length;
   char type;
   char version;
   char data[length];
};

At the moment I have class for transmission which has fixed header which contains message type, message version and length and body /body is char array which contains serialized fields from message classes, same implementation on client and server/), but I ma not sure this is the best way to do, is there better way to do.

Was it helpful?

Solution

I just want to make something clear first. You can't implement features (Or additional fields) to client with just sending them the packet. The only option is making the client's program able to self-modifying and sending the code that interprets the data from the server, or very similar option and easy is making the client update it's software.

But I guess you don't want to tangle your program much as described above. To make older client compatibility with newer versions you need to make the server work like client's version. You'll need to duplicate your way of communication (The code itself that arranges the packets before sending) each time you implement new feature or fields and when client is using older version, communicate with it in the same version on the server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top