boost::serialization fundamental question about "block read/write"

Hi, I have a more fundamental question/inquiry about the serialization library. What I want to do is to use the serialization library to send classes over a socket. Even though archives are not supposed to be used at streams I figured that it should be possible to create a oarchive on the sender side, create a iarchive on the receiver side, and use these for multiple, successive write / read operations in block mode. In order to test that assumption I created a test program to see if it would be possible. Here the pseudo code: void sync_streams(stringstream &os, stringstream& os) { // write data from one sting stream to the other // this emulates sending the data on a socket int rd_avail = os.tellp() - os.tellg(); os.read(buffer, rd_avail); is.write(buffer, rd_avail); } stringstream os, is; boost::archive::text_oarchive oa(os); sync_streams(os, is); // sync preamble boost::archive::text_iarchive ia(is); oa << object1; sync_streams(os, is); // sync object block ia >> rdObject1; oa << object2; sync_streams(os, is); // sync object block ia >> rdObject2; While I made this work, I wonder if it is a hack or if this is a valid use case for the serialization library. I looked at the asio projects example/source, there it was done by using a full archive for each object to be transmitted. Sending an archive every time seems a bit expensive too me, since a full preamble is sent everytime and I would loose all the pointer tracking and other great features from the serialization library. Regards, Gerd

In order to test that assumption I created a test program to see if it would be possible. Here the > pseudo code:
..
While I made this work, I wonder if it is a hack or if this is a valid use case for the serialization library.
This looks like a good start to me. I suspect that what this will need to really work the way you want is a custom stream_buf implementation. There should already be one around somewhere for passing data from one process to another - on the fly. Robert Ramey

Hi Gerd, I'm sure you know that already. But, have you taken a look at
boost::asio? There is even an example for using the serialization lib
when sending objects over a socket.
Here a link:
http://asio.sourceforge.net/boost_asio_0_3_7/libs/asio/doc/examples/index.ht...
Just scroll down a bit to the Serialization section.
Christian
On 1/8/07, Gerd Ritter
Hi,
I have a more fundamental question/inquiry about the serialization library.
What I want to do is to use the serialization library to send classes over a socket. Even though archives are not supposed to be used at streams I figured that it should be possible to create a oarchive on the sender side, create a iarchive on the receiver side, and use these for multiple, successive write / read operations in block mode. In order to test that assumption I created a test program to see if it would be possible. Here the pseudo code:
void sync_streams(stringstream &os, stringstream& os) { // write data from one sting stream to the other // this emulates sending the data on a socket
int rd_avail = os.tellp() - os.tellg(); os.read(buffer, rd_avail); is.write(buffer, rd_avail); }
stringstream os, is;
boost::archive::text_oarchive oa(os); sync_streams(os, is); // sync preamble boost::archive::text_iarchive ia(is);
oa << object1; sync_streams(os, is); // sync object block ia >> rdObject1;
oa << object2; sync_streams(os, is); // sync object block ia >> rdObject2;
While I made this work, I wonder if it is a hack or if this is a valid use case for the serialization library. I looked at the asio projects example/source, there it was done by using a full archive for each object to be transmitted. Sending an archive every time seems a bit expensive too me, since a full preamble is sent everytime and I would loose all the pointer tracking and other great features from the serialization library.
Regards,
Gerd _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Christian Henning
-
Gerd Ritter
-
Robert Ramey