I did something similar, and is easy, but you need to use the ReaderWriter API, that supports serialization to a stream. Here is my code:
void IOSocket::writeObject( const osg::Object* obj)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_rwMutex);
if(!_rw.valid())
{
_rw = osgDB::Registry::instance()->getReaderWriterForExtension("osgt");
if(!_rw.valid())
throw SocketException("Not valid ReaderWriter for osg::Object");
}
std::stringstream sstr;
_rw->writeObject(*obj,sstr);
(*this) << sstr.str();
}
osg::Object* IOSocket::readObject()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_rwMutex);
if(!_rw.valid())
{
_rw = osgDB::Registry::instance()->getReaderWriterForExtension("osgt");
if(!_rw.valid())
throw SocketException("Not valid ReaderWriter for osg::Object");
}
std::string str;
(*this) >> str;
std::cout << str << std::endl;
std::stringstream sstr(str);
return _rw->readObject(sstr).takeObject();
}
Selects the new ascii OSG serializer, but if you set "osgb" then the data is serialized in binary format and probably will give you better performance.
Rafa.