send / receive generic object using boost::async_read/write

I have been implementing a client/server program sending back and forth generic object by the slow and dumb method of encapsulating the object type in string AND serialized string of the object into a struct named "SerializedString", and send this struct across. Then in the handle_read callback function, which has a SerializedString in its argument, it checks for the type, deserialize the serialized string and call the relevant handling function for that type.

1
2
3
4
5
6
7
8
9
10
11
struct SerializeString {
	std::string type_name;
	std::string serialized_string;

	friend class boost::serialization::access;
	template<typename Archive>
	void serialize(Archive& ar, const unsigned version) {
		ar & type_name & serialized_string;
	}

};


While this has served my purpose in the past, it is getting too slow and I'm trying to improve this by sending the object directly instead. However, since both client/server has no idea what object it is going to receive, the initial callback function cannot have that object in its argument. I wonder if there are any example on how this can be done, that the handle_read function do not know the type of the object until it receives it.
Plain text is really the best thing to send. What is the bottleneck for you? CPU time in the serialized/deserializer? (in that case, find another library or special-case something yourself). Network throughput? (in that case, consider gzipping the stream).

In general, solutions to
sending the object directly instead. However, since both client/server has no idea what object it is going to receive

involve inventing/using a protocol that encodes the type and the contents sufficient to reconstruct an object of that type (note reconstructing objects of different types would be wildly different, unless you limit the protocol to POD types where you can just send the char array starting at its address)
Topic archived. No new replies allowed.