# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

message.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "stdnet.h"
00027 
00028 #include "nel/net/message.h"
00029 
00030 /*#ifdef MESSAGES_PLAIN_TEXT
00031 #pragma message( "CMessage: compiling messages as plain text" )
00032 #else
00033 #pragma message( "CMessage: compiling messages as binary" )
00034 #endif*/
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  * Utility method
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         // PATCH: the id system is not available
00127         nlstop;
00128 
00129         // check if we already do a setType ()
00130         nlassert (!_TypeSet);
00131         // don't accept negative value
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                 // check if they don't already serial some stuffs
00141                 nlassert (length () == 0);
00142 
00143                 uint8 format = FormatLong | (_StringMode << 1);
00144                 nlinfo( "OUT format = %hu", (uint16)format );
00145 
00146                 // Force binary mode for header
00147                 bool msgmode = _StringMode;
00148                 _StringMode = false;
00149 
00150                 // debug features, we number all packet to be sure that they are all sent and received
00151                 // \todo remove this debug feature when ok
00152                 // this value will be fill after in the callback function
00153                 uint32 zeroValue = 123;
00154                 serial (zeroValue);
00155 
00156                 serial (format);
00157 
00158                 // End of binary header
00159                 _StringMode = msgmode;
00160 
00161                 serial (id);
00162                 _HeaderSize = getPos ();
00163         }
00164         else
00165         {
00166                 // we set the id, now, we try to set the name if available in the sida
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         // check if we already do a setType ()
00181         nlassert (!_TypeSet);
00182         // don't accept empty string
00183         nlassert (!name.empty ());
00184 
00185         _Name = name;
00186         TypeHasAnId = false;
00187         TypeHasAName = true;
00188 
00189         if (!isReading ())
00190         {
00191                 // check if they don't already serial some stuffs
00192                 nlassert (length () == 0);
00193 
00194                 // if we can send the id instead of the string, "just do it" (c)nike!
00195                 //NLMISC::CStringIdArray::TStringId id = _SIDA->getId (name);
00196 
00197                 // PATCH: always send in full text
00198                 NLMISC::CStringIdArray::TStringId id = -1;
00199 
00200                 // Force binary mode for header
00201                 bool msgmode = _StringMode;
00202                 _StringMode = false;
00203 
00204                 // debug features, we number all packet to be sure that they are all sent and received
00205                 // \todo remove this debug feature when ok
00206                 // this value will be fill after in the callback function
00207                 uint32 zeroValue = 123;
00208                 serial (zeroValue);
00209 
00210                 if (id == -1)
00211                 {
00212                         uint8 format = FormatLong | (msgmode << 1);
00213                         //nldebug( "OUT format = %hu", (uint16)format );
00214                         serial (format);
00215 
00216                         // End of binary header
00217                         _StringMode = msgmode;
00218 
00219                         serial ((std::string&)name);
00220                 }
00221                 else
00222                 {
00223                         uint8 format = FormatShort | (msgmode << 1);
00224                         //nldebug( "OUT format = %hu", (uint16)format );
00225                         serial (format);
00226 
00227                         // End of binary header
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 // The message was filled with an CMemStream, Now, we'll get the message type on this buffer
00250 void CMessage::readType ()
00251 {
00252         nlassert (isReading ());
00253 
00254         // debug features, we number all packet to be sure that they are all sent and received
00255         // \todo remove this debug feature when ok
00256 
00257         // we remove the message from the message
00258         const uint HeaderSize = 4;
00259         seek (HeaderSize, begin);
00260 //              uint32 zeroValue;
00261 //              serial (zeroValue);
00262 
00263         // Force binary mode for header
00264         _StringMode = false;
00265 
00266         uint8 format;
00267         serial (format);
00268         //nldebug( "IN format = %hu", (uint16)format );
00269         bool LongFormat = (format & 1);
00270 
00271         // Set mode for the following of the buffer
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 // Returns true if the message type was already set
00288 bool CMessage::typeIsSet () const
00289 {
00290         return _TypeSet;
00291 }
00292 
00293 // Clear the message. With this function, you can reuse a message to create another message
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 }