I need to create TCP chat with C++ clients and Python server(already started), I have messages in c++ class like
class Message{
public:
uint64 utc_time;
uint64 token;
string content;
};
I am sending this from client to server, on server I have priority queue by utc_time and need to broadcast to others. My question is how to serialize this, which format to use so to avoid any cross language dependencies on size type size ? (maybe is going to be more meta data in future so need kind a little generic) ? Can anyone give me advice which format to use for serialization(or to flush only like bytes) ?
class Persistent:
public:
Persistent(int sz):objSize(sz){}
void write(std::ostream& out)const{out.write((char*)this, objSize);}
void read(std::istream& in){in.read((char*)this, objSize);}
private:
int objSize;
};
I thought of other possibility to have deserializator in c++ on server and call from python if that is possible. Any elegant solution to this problem ?
If you really want to go cross language and cross platform without having to worry about where the message ends, have a look at the combination of Google Protobuf and ZeroMQ.
When using regular sockets you would first read the size of the message (you would prepend this) and then you would know from where to where the byte-array is a complete message.
Example protobuf + zmq usage:
Use the protobuf compiler to generate C++ code (or ruby/python/etc).
To use it in your code:
To send it using zmq:
To receive it using zmq: