I'm having difficulties figuring out correct way of using boost serialization/asio to send objects over network. The message class is as simple as possible. It's not C++ friendly nor suitable for my needs, I just keep it simple temporarily to test asio/ser:
class message {
friend class boost::serialization::access;
public:
message(){}
int type;
int sender;
int assignment;
int nogood;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & type;
ar & sender;
ar & assignment;
ar & nogood;
}
};
On the client side when agent decides to send a message, sends it to server over it's tcp connection:
message m;
// do something to generate message
boost::asio::streambuf bufx;
std::ostream os( &bufx );
boost::archive::binary_oarchive ar( os );
ar & m;
boost::asio::write( socket, bufx);
Server side code:
boost::asio::streambuf bufx;
std::istream is(&bufx);
boost::archive::binary_iarchive ia(is); // <--- Exception: invalid signature
size_t rcx = asio::read(socket,bufx);
message m;
ia >> m;