Hi James,
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();
}
_rw has this definition: osg::ref_ptr<osgDB::ReaderWriter> _rw;
and this line:
osgDB::Registry::instance()->getReaderWriterForExtension("osgt");
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.
Cheers,
Rafa.
2012/6/7 James Lue <[email protected]>
> Hi,
>
> I want to send a geometry or a node to other computer via socket. Some
> people suggest serialization and de-serialization, but I'm not sure how
> exactly to do so. Can someone giva me a hint?
>
> Right now I think I can write the node by osgDB::writeNodeFile and read
> the file as a byte array then send it out. But I wonder if it's possible
> that I can convert the node into byte array without writing to disk and
> then read it again.
>
> Thank you!
>
> Cheers,
> James
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=48114#48114
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
--
Rafael Gaitán Linares
CTO at Mirage Technologies S.L - http://www.mirage-tech.com
gvSIG3D Developer - http://gvsig3d.blogspot.com
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org