00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "stdnet.h"
00027
00028 #include "nel/net/message.h"
00029
00030
00031
00032
00033
00034
00035
00036 namespace NLNET
00037 {
00038
00039 bool CMessage::_DefaultStringMode = false;
00040
00041
00042 #define FormatLong 1
00043 #define FormatShort 0
00044
00045
00046 CMessage::CMessage (NLMISC::CStringIdArray &sida, const std::string &name, bool inputStream, TStreamFormat streamformat, uint32 defaultCapacity) :
00047 NLMISC::CMemStream (inputStream, false, defaultCapacity),
00048 _TypeSet (false), _SIDA (&sida), _HeaderSize(0xFFFFFFFF)
00049 {
00050 init( name, streamformat );
00051 }
00052
00053 CMessage::CMessage (const std::string &name, bool inputStream, TStreamFormat streamformat, uint32 defaultCapacity) :
00054 NLMISC::CMemStream (inputStream, false, defaultCapacity),
00055 _TypeSet (false), _SIDA (NULL), _HeaderSize(0xFFFFFFFF)
00056 {
00057 init( name, streamformat );
00058 }
00059
00060
00061
00062
00063
00064 void CMessage::init( const std::string &name, TStreamFormat streamformat )
00065 {
00066 if ( streamformat == UseDefault )
00067 {
00068 setStringMode( _DefaultStringMode );
00069 }
00070 else
00071 {
00072 setStringMode( streamformat == String );
00073 }
00074
00075 if (!name.empty())
00076 setType (name);
00077 }
00078
00079
00080 CMessage::CMessage (NLMISC::CMemStream &memstr) :
00081 _HeaderSize(0xFFFFFFFF)
00082 {
00083 fill (memstr.buffer (), memstr.length ());
00084 uint8 LongFormat=2;
00085 serial (LongFormat);
00086
00087 if (LongFormat)
00088 {
00089 std::string name;
00090 serial (name);
00091 setType (name);
00092 }
00093 else
00094 {
00095 NLMISC::CStringIdArray::TStringId id;
00096 serial (id);
00097 setType (id);
00098 }
00099 }
00100
00101
00103 CMessage::CMessage (const CMessage &other)
00104 {
00105 operator= (other);
00106 }
00107
00109 CMessage &CMessage::operator= (const CMessage &other)
00110 {
00111 CMemStream::operator= (other);
00112 _TypeSet = other._TypeSet;
00113 _SIDA = other._SIDA;
00114 TypeHasAnId = other.TypeHasAnId;
00115 TypeHasAName = other.TypeHasAName;
00116 _Name = other._Name;
00117 _Id = other._Id;
00118 _HeaderSize = other._HeaderSize;
00119 return *this;
00120
00121 }
00122
00124 void CMessage::setType (NLMISC::CStringIdArray::TStringId id)
00125 {
00126
00127 nlstop;
00128
00129
00130 nlassert (!_TypeSet);
00131
00132 nlassert (id >= 0 && id < pow(2, sizeof (NLMISC::CStringIdArray::TStringId)*8));
00133
00134 _Id = id;
00135 TypeHasAnId = true;
00136 TypeHasAName = false;
00137
00138 if (!isReading ())
00139 {
00140
00141 nlassert (length () == 0);
00142
00143 uint8 format = FormatLong | (_StringMode << 1);
00144 nlinfo( "OUT format = %hu", (uint16)format );
00145
00146
00147 bool msgmode = _StringMode;
00148 _StringMode = false;
00149
00150
00151
00152
00153 uint32 zeroValue = 123;
00154 serial (zeroValue);
00155
00156 serial (format);
00157
00158
00159 _StringMode = msgmode;
00160
00161 serial (id);
00162 _HeaderSize = getPos ();
00163 }
00164 else
00165 {
00166
00167 if (_SIDA != NULL)
00168 {
00169 _Name = _SIDA->getString (id);
00170 TypeHasAName = true;
00171 }
00172 }
00173
00174 _TypeSet = true;
00175 }
00176
00178 void CMessage::setType (const std::string &name)
00179 {
00180
00181 nlassert (!_TypeSet);
00182
00183 nlassert (!name.empty ());
00184
00185 _Name = name;
00186 TypeHasAnId = false;
00187 TypeHasAName = true;
00188
00189 if (!isReading ())
00190 {
00191
00192 nlassert (length () == 0);
00193
00194
00195
00196
00197
00198 NLMISC::CStringIdArray::TStringId id = -1;
00199
00200
00201 bool msgmode = _StringMode;
00202 _StringMode = false;
00203
00204
00205
00206
00207 uint32 zeroValue = 123;
00208 serial (zeroValue);
00209
00210 if (id == -1)
00211 {
00212 uint8 format = FormatLong | (msgmode << 1);
00213
00214 serial (format);
00215
00216
00217 _StringMode = msgmode;
00218
00219 serial ((std::string&)name);
00220 }
00221 else
00222 {
00223 uint8 format = FormatShort | (msgmode << 1);
00224
00225 serial (format);
00226
00227
00228 _StringMode = msgmode;
00229
00230 serial (id);
00231
00232 _Id = id;
00233 TypeHasAnId = true;
00234 }
00235 _HeaderSize = getPos ();
00236 }
00237
00238 _TypeSet = true;
00239 }
00240
00242 uint32 CMessage::getHeaderSize ()
00243 {
00244 nlassert (!isReading ());
00245 nlassert (_HeaderSize != 0xFFFFFFFF);
00246 return _HeaderSize;
00247 }
00248
00249
00250 void CMessage::readType ()
00251 {
00252 nlassert (isReading ());
00253
00254
00255
00256
00257
00258 const uint HeaderSize = 4;
00259 seek (HeaderSize, begin);
00260
00261
00262
00263
00264 _StringMode = false;
00265
00266 uint8 format;
00267 serial (format);
00268
00269 bool LongFormat = (format & 1);
00270
00271
00272 _StringMode = (format >> 1) & 1;
00273 if (LongFormat)
00274 {
00275 std::string name;
00276 serial (name);
00277 setType (name);
00278 }
00279 else
00280 {
00281 NLMISC::CStringIdArray::TStringId id;
00282 serial (id);
00283 setType (id);
00284 }
00285 }
00286
00287
00288 bool CMessage::typeIsSet () const
00289 {
00290 return _TypeSet;
00291 }
00292
00293
00294 void CMessage::clear ()
00295 {
00296 CMemStream::clear ();
00297 _TypeSet = false;
00298 }
00299
00301 std::string CMessage::getName () const
00302 {
00303 nlassert (_TypeSet && TypeHasAName);
00304 return _Name;
00305 }
00306
00308 NLMISC::CStringIdArray::TStringId CMessage::getId () const
00309 {
00310 nlassert (_TypeSet && TypeHasAnId);
00311 return _Id;
00312 }
00313
00317 std::string CMessage::toString () const
00318 {
00319 nlassert (_TypeSet);
00320 std::stringstream s;
00321 if (TypeHasAName && TypeHasAnId) s << "('" << _Name << "'," << _Id << ")";
00322 else if (TypeHasAName) s << "('" << _Name << "'," << _SIDA->getId (_Name, true) << ")";
00323 else if (TypeHasAnId) s << "('" << _SIDA->getString (_Id) << "'," << _Id << "')";
00324 return s.str();
00325 }
00326
00327
00328
00329 }