OSG::BinaryMessage Class Reference

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< UInt8 > BufferType

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


Header & getHeader (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 54 of file OSGBinaryMessage.h.


Member Typedef Documentation

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

Definition at line 59 of file OSGBinaryMessage.h.

Definition at line 151 of file OSGBinaryMessage.h.


Constructor & Destructor Documentation

BinaryMessage::BinaryMessage ( void   ) 

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]

Definition at line 108 of file OSGBinaryMessage.cpp.

00109 {
00110 }


Member Function Documentation

UInt32 BinaryMessage::getSize ( void   )  [virtual]

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]

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   ) 

Clear message buffer

Definition at line 149 of file OSGBinaryMessage.cpp.

References _buffer.

Referenced by BinaryMessage(), OSG::ClusterWindow::init(), OSG::PointMCastConnection::initialize(), and OSG::GroupMCastConnection::initialize().

00150 {
00151     _buffer.resize(sizeof(Header));
00152 }

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 OSG::BinaryMessage::putUInt32 ( const UInt32  value  )  [inline]

Definition at line 47 of file OSGBinaryMessage.inl.

References _buffer.

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

00048 {
00049     UInt32 net = osgHostToNet<UInt32>(value);
00050     _buffer.insert(_buffer.end(),
00051                    reinterpret_cast<UInt8*>(&net),
00052                    reinterpret_cast<UInt8*>(&net) + sizeof(net));
00053 }

void OSG::BinaryMessage::putInt32 ( const Int32  value  )  [inline]

Definition at line 55 of file OSGBinaryMessage.inl.

References _buffer.

00056 {
00057     Int32 net = osgHostToNet<Int32>(value);
00058     _buffer.insert(_buffer.end(),
00059                    reinterpret_cast<UInt8*>(&net),
00060                    reinterpret_cast<UInt8*>(&net) + sizeof(net));
00061 }

void OSG::BinaryMessage::putUInt16 ( const UInt16  value  )  [inline]

Definition at line 63 of file OSGBinaryMessage.inl.

References _buffer.

00064 {
00065     UInt16 net = osgHostToNet<UInt16>(value);
00066     _buffer.insert(_buffer.end(),
00067                    reinterpret_cast<UInt8*>(&net),
00068                    reinterpret_cast<UInt8*>(&net) + sizeof(net));
00069 }

void OSG::BinaryMessage::putInt16 ( const Int16  value  )  [inline]

Definition at line 71 of file OSGBinaryMessage.inl.

References _buffer.

00072 {
00073     Int16 net = osgHostToNet<Int16>(value);
00074     _buffer.insert(_buffer.end(),
00075                    reinterpret_cast<UInt8*>(&net),
00076                    reinterpret_cast<UInt8*>(&net) + sizeof(net));
00077 }

void OSG::BinaryMessage::putUInt8 ( const UInt8  value  )  [inline]

Definition at line 79 of file OSGBinaryMessage.inl.

References _buffer.

00080 {
00081     _buffer.push_back(value);
00082 }

void OSG::BinaryMessage::putInt8 ( const Int8  value  )  [inline]

Definition at line 84 of file OSGBinaryMessage.inl.

References _buffer.

00085 {
00086     UInt8 v = static_cast<const UInt8>(value);
00087     _buffer.push_back(v);
00088 }

void OSG::BinaryMessage::putString ( const std::string &  value  )  [inline]

Definition at line 90 of file OSGBinaryMessage.inl.

References _buffer, and putUInt32().

Referenced by OSG::ClusterWindow::init(), OSG::PointSockPipeline::initialize(), and OSG::GroupMCastConnection::initialize().

00091 {
00092     putUInt32(value.size());
00093     if(value.size())
00094     {
00095         const UInt8 *s = reinterpret_cast<const UInt8*>(value.c_str());
00096         const UInt8 *e = s + value.size();
00097         _buffer.insert(_buffer.end(), s, e);
00098     }
00099 }

void OSG::BinaryMessage::putReal32 ( const Real32  value  )  [inline]

Definition at line 101 of file OSGBinaryMessage.inl.

References _buffer.

00102 {
00103     Real32 net = osgHostToNet<Real32>(value);
00104     _buffer.insert(_buffer.end(),
00105                    reinterpret_cast<UInt8*>(&net),
00106                    reinterpret_cast<UInt8*>(&net) + sizeof(net));
00107 }

void OSG::BinaryMessage::getUInt32 ( UInt32 &  value  )  [inline]

Definition at line 112 of file OSGBinaryMessage.inl.

References _buffer, and _pos.

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

00113 {
00114     UInt32 net = *reinterpret_cast<UInt32*>(&_buffer[_pos]);
00115     value = osgNetToHost<UInt32>(net);
00116     _pos += sizeof(net);
00117 }

void OSG::BinaryMessage::getInt32 ( Int32 &  value  )  [inline]

Definition at line 119 of file OSGBinaryMessage.inl.

References _buffer, and _pos.

00120 {
00121     Int32 net = *reinterpret_cast<Int32*>(&_buffer[_pos]);
00122     value = osgNetToHost<Int32>(net);
00123     _pos += sizeof(net);
00124 }

void OSG::BinaryMessage::getUInt16 ( UInt16 &  value  )  [inline]

Definition at line 126 of file OSGBinaryMessage.inl.

References _buffer, and _pos.

00127 {
00128     UInt16 net = *reinterpret_cast<UInt16*>(&_buffer[_pos]);
00129     value = osgNetToHost<UInt16>(net);
00130     _pos += sizeof(net);
00131 }

void OSG::BinaryMessage::getInt16 ( Int16 &  value  )  [inline]

Definition at line 133 of file OSGBinaryMessage.inl.

References _buffer, and _pos.

00134 {
00135     Int16 net = *reinterpret_cast<Int16*>(&_buffer[_pos]);
00136     value = osgNetToHost<Int16>(net);
00137     _pos += sizeof(net);
00138 }

void OSG::BinaryMessage::getUInt8 ( UInt8 &  value  )  [inline]

Definition at line 140 of file OSGBinaryMessage.inl.

References _buffer, and _pos.

00141 {
00142     value = _buffer[_pos++];
00143 }

void OSG::BinaryMessage::getInt8 ( Int8 &  value  )  [inline]

Definition at line 145 of file OSGBinaryMessage.inl.

References _buffer, and _pos.

00146 {
00147     value = _buffer[_pos++];
00148 }

void OSG::BinaryMessage::getString ( std::string &  value  )  [inline]

Definition at line 150 of file OSGBinaryMessage.inl.

References _buffer, _pos, getUInt32(), and size.

Referenced by OSG::PointSockPipeline::initialize(), and OSG::PointMCastConnection::initialize().

00151 {
00152     UInt32 size;
00153     getUInt32(size);
00154 
00155     if(size)
00156     {
00157 // error if [] checks index
00158 //        value.assign(reinterpret_cast<char*>(&_buffer[_pos       ]),
00159 //                     reinterpret_cast<char*>(&_buffer[_pos + size]) );
00160 
00161         value.assign(reinterpret_cast<char*>(&(_buffer[_pos])), size);
00162 
00163         _pos += size;
00164     }
00165     else
00166     {
00167         value.erase();
00168     }
00169 }

void OSG::BinaryMessage::getReal32 ( Real32 &  value  )  [inline]

Definition at line 171 of file OSGBinaryMessage.inl.

References _buffer, and _pos.

00172 {
00173     Real32 net = *reinterpret_cast<Real32*>(&_buffer[_pos]);
00174     value = osgNetToHost<Real32>(net);
00175     _pos += sizeof(net);
00176 }

UInt32 OSG::BinaryMessage::getUInt32 ( void   )  [inline]

Definition at line 178 of file OSGBinaryMessage.inl.

Referenced by getString().

00179 {
00180     UInt32 value;
00181     getUInt32(value);
00182     return value;
00183 }

Int32 OSG::BinaryMessage::getInt32 ( void   )  [inline]

Definition at line 185 of file OSGBinaryMessage.inl.

00186 {
00187     Int32 value;
00188     getInt32(value);
00189     return value;
00190 }

UInt16 OSG::BinaryMessage::getUInt16 ( void   )  [inline]

Definition at line 192 of file OSGBinaryMessage.inl.

00193 {
00194     UInt16 value;
00195     getUInt16(value);
00196     return value;
00197 }

Int16 OSG::BinaryMessage::getInt16 ( void   )  [inline]

Definition at line 199 of file OSGBinaryMessage.inl.

00200 {
00201     Int16 value;
00202     getInt16(value);
00203     return value;
00204 }

UInt8 OSG::BinaryMessage::getUInt8 ( void   )  [inline]

Definition at line 206 of file OSGBinaryMessage.inl.

00207 {
00208     UInt8 value;
00209     getUInt8(value);
00210     return value;
00211 }

Int8 OSG::BinaryMessage::getInt8 ( void   )  [inline]

Definition at line 213 of file OSGBinaryMessage.inl.

00214 {
00215     Int8 value;
00216     getInt8(value);
00217     return value;
00218 }

std::string OSG::BinaryMessage::getString ( void   )  [inline]

Definition at line 220 of file OSGBinaryMessage.inl.

00221 {
00222     std::string value;
00223     getString(value);
00224     return value;
00225 }

Real32 OSG::BinaryMessage::getReal32 ( void   )  [inline]

Definition at line 227 of file OSGBinaryMessage.inl.

00228 {
00229     Real32 value;
00230     getReal32(value);
00231     return value;
00232 }

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

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

UInt32 OSG::BinaryMessage::_pos [protected]


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