00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045
00046 #include "OSGConfig.h"
00047
00048 #include "OSGBinaryMessage.h"
00049
00050 OSG_USING_NAMESPACE
00051
00081
00082
00083
00086 BinaryMessage::BinaryMessage(void):
00087 NetworkMessage(),
00088 _buffer(),
00089 _pos(sizeof(Header))
00090 {
00091 clear();
00092 }
00093
00096 BinaryMessage::BinaryMessage(const BinaryMessage &source):
00097 NetworkMessage(source),
00098 _buffer(source._buffer),
00099 _pos(source._pos)
00100 {
00101 }
00102
00103
00104
00105
00108 BinaryMessage::~BinaryMessage(void)
00109 {
00110 }
00111
00112
00113
00114
00117 BinaryMessage& BinaryMessage::operator = (const BinaryMessage &source)
00118 {
00119 if(this == &source)
00120 return *this;
00121
00122
00123 *(static_cast<Inherited *>(this)) = source;
00124
00125
00126
00127
00128
00129
00130 _buffer=source._buffer;
00131 _pos =source._pos;
00132 return *this;
00133 }
00134
00135
00136
00137
00141 void BinaryMessage::setSize(UInt32 size)
00142 {
00143 _buffer.resize(size);
00144 reset();
00145 }
00146
00149 void BinaryMessage::clear(void)
00150 {
00151 _buffer.resize(sizeof(Header));
00152 }
00153
00156 void BinaryMessage::reset(void)
00157 {
00158 _pos=sizeof(Header);
00159 }
00160
00161
00162
00163
00166 UInt32 BinaryMessage::getSize(void)
00167 {
00168 return _buffer.size();
00169 }
00170
00173 MemoryHandle BinaryMessage::getBuffer(void)
00174 {
00175 if(_buffer.size())
00176 return static_cast<MemoryHandle>(&_buffer[0]);
00177 else
00178 return 0;
00179 }
00180
00181
00182
00183
00184 void BinaryMessage::putUInt32(const UInt32 value)
00185 {
00186 Int32 net=osghtonl(value);
00187 _buffer.insert(_buffer.end(),(UInt8*)(&net),((UInt8*)(&net))+sizeof(net));
00188 }
00189
00190 void BinaryMessage::putInt32 (const Int32 value)
00191 {
00192 Int32 net=osghtonl(value);
00193 _buffer.insert(_buffer.end(),(UInt8*)(&net),((UInt8*)(&net))+sizeof(net));
00194 }
00195
00196 void BinaryMessage::putUInt16(const UInt16 value)
00197 {
00198 Int16 net=osghtons(value);
00199 _buffer.insert(_buffer.end(),(UInt8*)(&net),((UInt8*)(&net))+sizeof(net));
00200 }
00201
00202 void BinaryMessage::putInt16 (const Int16 value)
00203 {
00204 Int16 net=osghtons(value);
00205 _buffer.insert(_buffer.end(),(UInt8*)(&net),((UInt8*)(&net))+sizeof(net));
00206 }
00207
00208 void BinaryMessage::putUInt8 (const UInt8 value)
00209 {
00210 _buffer.push_back(value);
00211 }
00212
00213 void BinaryMessage::putInt8 (const Int8 value)
00214 {
00215 UInt8 v=static_cast<UInt8>(value);
00216 _buffer.push_back(v);
00217 }
00218
00219 void BinaryMessage::putString(const std::string &value)
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 }
00229
00230 void BinaryMessage::putReal32(const Real32 value)
00231 {
00232 putInt32(*((const Int32*)(&value)));
00233 }
00234
00235
00236
00237
00238 void BinaryMessage::getUInt32(UInt32 &value)
00239 {
00240 Int32 net;
00241 memcpy(&net,&_buffer[_pos],sizeof(net));
00242 value=osgntohl(net);
00243 _pos+=sizeof(net);
00244 }
00245
00246 void BinaryMessage::getInt32 (Int32 &value)
00247 {
00248 Int32 net;
00249 memcpy(&net,&_buffer[_pos],sizeof(net));
00250 value=osgntohl(net);
00251 _pos+=sizeof(net);
00252 }
00253
00254 void BinaryMessage::getUInt16(UInt16 &value)
00255 {
00256 Int16 net=*((Int16 *)( &_buffer[_pos]));
00257 value=osgntohs(net);
00258 _pos+=sizeof(net);
00259 }
00260
00261 void BinaryMessage::getInt16 (Int16 &value)
00262 {
00263 Int16 net=*((Int16 *)( &_buffer[_pos]));
00264 value=osgntohs(net);
00265 _pos+=sizeof(net);
00266 }
00267
00268 void BinaryMessage::getUInt8 (UInt8 &value)
00269 {
00270 value=_buffer[_pos++];
00271 }
00272
00273 void BinaryMessage::getInt8 (Int8 &value)
00274 {
00275 value=_buffer[_pos++];
00276 }
00277
00278 void BinaryMessage::getString(std::string &value)
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 }
00290
00291 void BinaryMessage::getReal32(Real32 &value)
00292 {
00293 getInt32(*((Int32*)(&value)));
00294 }
00295
00296 UInt32 BinaryMessage::getUInt32(void)
00297 {
00298 UInt32 value;
00299 getUInt32(value);
00300 return value;
00301 }
00302
00303 Int32 BinaryMessage::getInt32 (void)
00304 {
00305 Int32 value;
00306 getInt32(value);
00307 return value;
00308 }
00309
00310 UInt16 BinaryMessage::getUInt16(void)
00311 {
00312 UInt16 value;
00313 getUInt16(value);
00314 return value;
00315 }
00316
00317 Int16 BinaryMessage::getInt16 (void)
00318 {
00319 Int16 value;
00320 getInt16(value);
00321 return value;
00322 }
00323
00324 UInt8 BinaryMessage::getUInt8 (void)
00325 {
00326 UInt8 value;
00327 getUInt8(value);
00328 return value;
00329 }
00330
00331 Int8 BinaryMessage::getInt8 (void)
00332 {
00333 Int8 value;
00334 getInt8(value);
00335 return value;
00336 }
00337
00338 std::string BinaryMessage::getString(void)
00339 {
00340 std::string value;
00341 getString(value);
00342 return value;
00343 }
00344
00345 Real32 BinaryMessage::getReal32(void)
00346 {
00347 Real32 value;
00348 getReal32(value);
00349 return value;
00350 }
00351
00352
00353
00354
00355 #ifdef __sgi
00356 #pragma set woff 1174
00357 #endif
00358
00359 #ifdef OSG_LINUX_ICC
00360 #pragma warning( disable : 177 )
00361 #endif
00362
00363 namespace
00364 {
00365 static Char8 cvsid_cpp[] = "@(#)$Id:$";
00366 static Char8 cvsid_hpp[] = OSG_BINSOCKETMESSAGEHEADER_CVSID;
00367 }
00368
00369
00370
00371
00372