osg::BinaryMessage Class Reference
[Network]

Little-, Big endian independent message buffer. More...

#include <OSGBinaryMessage.h>

Inheritance diagram for osg::BinaryMessage:

osg::NetworkMessage

List of all members.

Public Types

typedef std::vector< UInt8BufferType

Public Member Functions

Constructors


 BinaryMessage (void)
 BinaryMessage (const BinaryMessage &source)
Destructor


virtual ~BinaryMessage (void)
Get


virtual UInt32 getSize (void)
virtual MemoryHandle getBuffer (void)
Misc


virtual void setSize (UInt32 size)
void clear (void)
void reset (void)
write message


void putUInt32 (const UInt32 value)
void putInt32 (const Int32 value)
void putUInt16 (const UInt16 value)
void putInt16 (const Int16 value)
void putUInt8 (const UInt8 value)
void putInt8 (const Int8 value)
void putString (const std::string &value)
void putReal32 (const Real32 value)
read message


void getUInt32 (UInt32 &value)
void getInt32 (Int32 &value)
void getUInt16 (UInt16 &value)
void getInt16 (Int16 &value)
void getUInt8 (UInt8 &value)
void getInt8 (Int8 &value)
void getString (std::string &value)
void getReal32 (Real32 &value)
UInt32 getUInt32 (void)
Int32 getInt32 (void)
UInt16 getUInt16 (void)
Int16 getInt16 (void)
UInt8 getUInt8 (void)
Int8 getInt8 (void)
std::string getString (void)
Real32 getReal32 (void)
Assignment


BinaryMessageoperator= (const BinaryMessage &source)
Get


HeadergetHeader (void)

Protected Attributes

Member


BufferType _buffer
UInt32 _pos

Private Types

typedef NetworkMessage Inherited


Detailed Description

Little-, Big endian independent message buffer.

Example:

 
 // send
 BinSockMessage msg;
 msg.clear();              // if not already empty
 msg.putUInt32(220);
 msg.putInt32 (221);
 msg.putUInt16(222);
 msg.putInt16 (223);
 msg.putUInt8 (224);
 msg.putInt8  (225);
 msg.putReal32(226.0);
 msg.putString("227");
 socket.send(msg);
 // receive
 string str;
 socket.recv(msg);
 str = msg.getString();
 msg.getString(str);        // avoid one copy
 

Definition at line 53 of file OSGBinaryMessage.h.


Member Typedef Documentation

typedef std::vector<UInt8> osg::BinaryMessage::BufferType

Definition at line 58 of file OSGBinaryMessage.h.

Definition at line 150 of file OSGBinaryMessage.h.


Constructor & Destructor Documentation

BinaryMessage::BinaryMessage ( void   ) 

Constructor

Definition at line 86 of file OSGBinaryMessage.cpp.

References clear().

00086                                 :
00087     NetworkMessage(),
00088     _buffer(),
00089     _pos(sizeof(Header))
00090 {
00091     clear();
00092 }

BinaryMessage::BinaryMessage ( const BinaryMessage source  ) 

Copy constructor

Definition at line 96 of file OSGBinaryMessage.cpp.

00096                                                        :
00097     NetworkMessage(source),
00098     _buffer(source._buffer),
00099     _pos(source._pos)
00100 {
00101 }

BinaryMessage::~BinaryMessage ( void   )  [virtual]

Destructor

Definition at line 108 of file OSGBinaryMessage.cpp.

00109 {
00110 }


Member Function Documentation

UInt32 BinaryMessage::getSize ( void   )  [virtual]

Get message size in bytes

Implements osg::NetworkMessage.

Definition at line 166 of file OSGBinaryMessage.cpp.

References _buffer.

00167 {
00168     return _buffer.size();
00169 }

MemoryHandle BinaryMessage::getBuffer ( void   )  [virtual]

Get buffer address

Implements osg::NetworkMessage.

Definition at line 173 of file OSGBinaryMessage.cpp.

References _buffer.

00174 {
00175     if(_buffer.size())
00176         return static_cast<MemoryHandle>(&_buffer[0]);
00177     else
00178         return 0;
00179 }

void BinaryMessage::setSize ( UInt32  size  )  [virtual]

Set message size. This is called by the socket to get enough space to read a message.

Implements osg::NetworkMessage.

Definition at line 141 of file OSGBinaryMessage.cpp.

References _buffer, and reset().

00142 {
00143     _buffer.resize(size);
00144     reset();
00145 }

void BinaryMessage::clear ( void   ) 

void BinaryMessage::reset ( void   ) 

Reset readpointer to the beginn of the buffer

Definition at line 156 of file OSGBinaryMessage.cpp.

References _pos.

Referenced by setSize().

00157 {
00158     _pos=sizeof(Header);
00159 }

void BinaryMessage::putUInt32 ( const UInt32  value  ) 

Definition at line 184 of file OSGBinaryMessage.cpp.

References _buffer, and osg::osghtonl().

Referenced by osg::PointSockPipeline::initialize(), osg::PointMCastConnection::initialize(), osg::GroupSockPipeline::initialize(), osg::GroupMCastConnection::initialize(), and putString().

00185 {
00186     Int32 net=osghtonl(value);
00187     _buffer.insert(_buffer.end(),(UInt8*)(&net),((UInt8*)(&net))+sizeof(net));
00188 }

void BinaryMessage::putInt32 ( const Int32  value  ) 

Definition at line 190 of file OSGBinaryMessage.cpp.

References _buffer, and osg::osghtonl().

Referenced by putReal32().

00191 {
00192     Int32 net=osghtonl(value);
00193     _buffer.insert(_buffer.end(),(UInt8*)(&net),((UInt8*)(&net))+sizeof(net));
00194 }

void BinaryMessage::putUInt16 ( const UInt16  value  ) 

Definition at line 196 of file OSGBinaryMessage.cpp.

References _buffer, and osg::osghtons().

00197 {
00198     Int16 net=osghtons(value);
00199     _buffer.insert(_buffer.end(),(UInt8*)(&net),((UInt8*)(&net))+sizeof(net));
00200 }

void BinaryMessage::putInt16 ( const Int16  value  ) 

Definition at line 202 of file OSGBinaryMessage.cpp.

References _buffer, and osg::osghtons().

00203 {
00204     Int16 net=osghtons(value);
00205     _buffer.insert(_buffer.end(),(UInt8*)(&net),((UInt8*)(&net))+sizeof(net));
00206 }

void BinaryMessage::putUInt8 ( const UInt8  value  ) 

Definition at line 208 of file OSGBinaryMessage.cpp.

References _buffer.

00209 {
00210     _buffer.push_back(value);
00211 }

void BinaryMessage::putInt8 ( const Int8  value  ) 

Definition at line 213 of file OSGBinaryMessage.cpp.

References _buffer.

00214 {
00215     UInt8 v=static_cast<UInt8>(value);
00216     _buffer.push_back(v);
00217 }

void BinaryMessage::putString ( const std::string &  value  ) 

Definition at line 219 of file OSGBinaryMessage.cpp.

References _buffer, and putUInt32().

Referenced by osg::ClusterServer::acceptClient(), osg::ClusterWindow::init(), osg::PointSockPipeline::initialize(), osg::GroupSockPipeline::initialize(), and osg::GroupMCastConnection::initialize().

00220 {
00221     putUInt32(value.size());
00222     if(value.size())
00223     {
00224         const UInt8 *s=(const UInt8*)(value.c_str());
00225         const UInt8 *e=s+value.size();
00226         _buffer.insert(_buffer.end(),s,e);
00227     }
00228 }

void BinaryMessage::putReal32 ( const Real32  value  ) 

Definition at line 230 of file OSGBinaryMessage.cpp.

References putInt32().

00231 {
00232     putInt32(*((const Int32*)(&value)));
00233 }

void BinaryMessage::getUInt32 ( UInt32 value  ) 

Definition at line 238 of file OSGBinaryMessage.cpp.

References _buffer, _pos, and osg::osgntohl().

Referenced by osg::PointSockPipeline::initialize(), osg::PointMCastConnection::initialize(), osg::GroupSockPipeline::initialize(), and osg::GroupMCastConnection::initialize().

00239 {
00240     Int32 net;
00241     memcpy(&net,&_buffer[_pos],sizeof(net));
00242     value=osgntohl(net);
00243     _pos+=sizeof(net);
00244 }

void BinaryMessage::getInt32 ( Int32 value  ) 

Definition at line 246 of file OSGBinaryMessage.cpp.

References _buffer, _pos, and osg::osgntohl().

00247 {
00248     Int32 net;
00249     memcpy(&net,&_buffer[_pos],sizeof(net));
00250     value=osgntohl(net);
00251     _pos+=sizeof(net);
00252 }

void BinaryMessage::getUInt16 ( UInt16 value  ) 

Definition at line 254 of file OSGBinaryMessage.cpp.

References _buffer, _pos, and osg::osgntohs().

00255 {
00256     Int16 net=*((Int16 *)( &_buffer[_pos]));
00257     value=osgntohs(net);
00258     _pos+=sizeof(net);
00259 }

void BinaryMessage::getInt16 ( Int16 value  ) 

Definition at line 261 of file OSGBinaryMessage.cpp.

References _buffer, _pos, and osg::osgntohs().

00262 {
00263     Int16 net=*((Int16 *)( &_buffer[_pos]));
00264     value=osgntohs(net);
00265     _pos+=sizeof(net);
00266 }

void BinaryMessage::getUInt8 ( UInt8 value  ) 

Definition at line 268 of file OSGBinaryMessage.cpp.

References _buffer, and _pos.

00269 {
00270     value=_buffer[_pos++];
00271 }

void BinaryMessage::getInt8 ( Int8 value  ) 

Definition at line 273 of file OSGBinaryMessage.cpp.

References _buffer, and _pos.

00274 {
00275     value=_buffer[_pos++];
00276 }

void BinaryMessage::getString ( std::string &  value  ) 

Definition at line 278 of file OSGBinaryMessage.cpp.

References _buffer, _pos, and getUInt32().

Referenced by osg::ClusterServer::acceptClient(), osg::ClusterWindow::init(), osg::PointSockPipeline::initialize(), osg::PointMCastConnection::initialize(), and osg::GroupSockPipeline::initialize().

00279 {
00280     UInt32 size;
00281     getUInt32(size);
00282     if(!value.empty())
00283         value.erase();
00284     if(size)
00285     {
00286         value = std::string((const char*)&_buffer[_pos],size);
00287         _pos+=size;
00288     }
00289 }

void BinaryMessage::getReal32 ( Real32 value  ) 

Definition at line 291 of file OSGBinaryMessage.cpp.

References getInt32().

00292 {
00293     getInt32(*((Int32*)(&value)));
00294 }

UInt32 BinaryMessage::getUInt32 ( void   ) 

Definition at line 296 of file OSGBinaryMessage.cpp.

Referenced by getString().

00297 {
00298     UInt32 value;
00299     getUInt32(value);
00300     return value;
00301 }

Int32 BinaryMessage::getInt32 ( void   ) 

Definition at line 303 of file OSGBinaryMessage.cpp.

Referenced by getReal32().

00304 {
00305     Int32 value;
00306     getInt32(value);
00307     return value;
00308 }

UInt16 BinaryMessage::getUInt16 ( void   ) 

Definition at line 310 of file OSGBinaryMessage.cpp.

00311 {
00312     UInt16 value;
00313     getUInt16(value);
00314     return value;
00315 }

Int16 BinaryMessage::getInt16 ( void   ) 

Definition at line 317 of file OSGBinaryMessage.cpp.

00318 {
00319     Int16 value;
00320     getInt16(value);
00321     return value;
00322 }

UInt8 BinaryMessage::getUInt8 ( void   ) 

Definition at line 324 of file OSGBinaryMessage.cpp.

00325 {
00326     UInt8 value;
00327     getUInt8(value);
00328     return value;
00329 }

Int8 BinaryMessage::getInt8 ( void   ) 

Definition at line 331 of file OSGBinaryMessage.cpp.

00332 {
00333     Int8 value;
00334     getInt8(value);
00335     return value;
00336 }

std::string BinaryMessage::getString ( void   ) 

Definition at line 338 of file OSGBinaryMessage.cpp.

00339 {
00340     std::string value;
00341     getString(value);
00342     return value;
00343 }

Real32 BinaryMessage::getReal32 ( void   ) 

Definition at line 345 of file OSGBinaryMessage.cpp.

00346 {
00347     Real32 value;
00348     getReal32(value);
00349     return value;
00350 }

BinaryMessage & BinaryMessage::operator= ( const BinaryMessage source  ) 

assignment

Definition at line 117 of file OSGBinaryMessage.cpp.

References _buffer, and _pos.

00118 {
00119     if(this == &source)
00120         return *this;
00121 
00122     // copy parts inherited from parent
00123     *(static_cast<Inherited *>(this)) = source;
00124 
00125     // free mem alloced by members of 'this'
00126 
00127     // alloc new mem for members
00128 
00129     // copy
00130     _buffer=source._buffer;
00131     _pos   =source._pos;
00132     return *this;
00133 }

NetworkMessage::Header & NetworkMessage::getHeader ( void   )  [inherited]

Get message header. A pointer to the first byte of the message is returned

Definition at line 97 of file OSGNetworkMessage.cpp.

References osg::NetworkMessage::getBuffer().

Referenced by osg::Socket::send(), and osg::DgramSocket::sendTo().

00098 {
00099     return *((Header*)(getBuffer()));
00100 }


Member Data Documentation


The documentation for this class was generated from the following files:

Generated on Mon Mar 17 11:11:01 2008 for OpenSG by  doxygen 1.5.5