[osg-users] Convert a node into byte array, serialize a node?

107 views
Skip to first unread message

James Lue

unread,
Jun 7, 2012, 4:05:33 PM6/7/12
to osg-...@lists.openscenegraph.org
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
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Rafa Gaitan

unread,
Jun 7, 2012, 5:13:00 PM6/7/12
to osg-...@lists.openscenegraph.org
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 <jl...@ucsd.edu>



--
Rafael Gaitán Linares
CTO at Mirage Technologies S.L - http://www.mirage-tech.com
gvSIG3D Developer - http://gvsig3d.blogspot.com

Rafa Gaitan

unread,
Jun 7, 2012, 5:16:58 PM6/7/12
to osg-...@lists.openscenegraph.org
Ops! I forgot to mention that in my IOSocket class I have the << operator overloaded to support send and receive data from strings, but there you can use "send" and "recv" of the sockets API.

Rafa.


2012/6/7 Rafa Gaitan <rafa....@gmail.com>

James Lue

unread,
Jun 7, 2012, 8:06:47 PM6/7/12
to osg-...@lists.openscenegraph.org
> --
> Rafael Gaitán Linares
> CTO at Mirage Technologies S.L - http://www.mirage-tech.com (http://www.mirage-tech.com)
> gvSIG3D Developer - http://gvsig3d.blogspot.com (http://gvsig3d.blogspot.com)
>
> ------------------
> Post generated by Mail2Forum


Thank you Rafael! This is what I want!

------------------
Read this topic online here:

http://forum.openscenegraph.org/viewtopic.php?p=48122#48122

Reply all
Reply to author
Forward
0 new messages