- Main Page
- Classes
- Files

- File List
- File Members

# E:/uas2/Server/Message.h

Go to the documentation of this file.

    00001 /*
    00002 * This file is part of UAS2.
    00003 *
    00004 * UAS2 is free software; you can redistribute it and/or modify
    00005 * it under the terms of the GNU General Public License as published by
    00006 * the Free Software Foundation; either version 2 of the License, or
    00007 * (at your option) any later version.
    00008 *
    00009 * UAS2 is distributed in the hope that it will be useful,
    00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012 * GNU General Public License for more details.
    00013
    00014 * You should have received a copy of the GNU General Public License
    00015 * along with UASv1; if not, write to the Free Software
    00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00017 */
    00018
    00023 #ifndef __MESSAGE_H
    00024 #define __MESSAGE_H
    00025
    00026 #include <deque>
    00027 #include <vector>
    00028 #include <cassert>
    00029
    00030 #include "Shared.h"
    00031
    00032 #define BUFFER_BLOCK_SIZE 1024
    00033
    00034 class cMessage
    00035 {
    00036         class cBlock
    00037         {
    00038                 friend class cMessage;
    00039
    00040                 mutable BYTE *m_pStart, *m_pUsed;
    00041
    00042         public:
    00043
    00044                 cBlock ();
    00045                 cBlock ( const cBlock &from );
    00046                 ~cBlock ();
    00047
    00048                 BOOL pasteData ( BYTE *&pData, int &nBytes );
    00049                 int getAlign ( int nBoundary ) const;
    00050                 BOOL pasteAlign ( int &nBytes );
    00051
    00052                 size_t getUsed () const
    00053                 {
    00054                         return ( m_pUsed - m_pStart );
    00055                 }
    00056
    00057                 BOOL getData ( size_t &nOffset, size_t &nLength, BYTE * &pBuffer ) const;
    00058
    00059         private:
    00060                 // Disabled functions
    00061                 cBlock &operator= ( const cBlock & );
    00062         };
    00063
    00064         typedef std::deque< cBlock > cBlockList;
    00065         cBlockList m_blocks;
    00066
    00067         // Place a block of data into the message
    00068 public:
    00069         cMessage ();
    00070         cMessage (BYTE* pbPacket, int nSize);
    00071         ~cMessage( )
    00072         {
    00073                 while ( !m_blocks.empty( ) )
    00074                         m_blocks.pop_front( );
    00075         }
    00076
    00077         // Basic functions for inserting data
    00078         void pasteData ( BYTE *pData, int nBytes );
    00079         void pasteAlign ( int nBoundary );
    00080         void pasteString ( const char *str );
    00081
    00082         // Functions for retrieving data
    00083         size_t getSize () const;
    00084         void getData ( size_t nOffset, size_t nLength, BYTE *pBuffer ) const;
    00085
    00086         template< class DataT >
    00087         inline void paste ( DataT dt )
    00088         {
    00089                 pasteData ( reinterpret_cast< BYTE * > ( &dt ), sizeof ( DataT ) );
    00090         }
    00091
    00092         // String pastes
    00093         template<>
    00094         inline void paste ( char *str )
    00095         {
    00096                 pasteString ( static_cast< const char * >( str ) );
    00097         }
    00098
    00099         template<>
    00100         inline void paste ( const char *str )
    00101         {
    00102                 pasteString ( static_cast< const char * >( str ) );
    00103         }
    00104
    00105         // Collection pastes
    00106         template< class ItemT >
    00107         void paste ( std::vector< ItemT > &vec )
    00108         {
    00109                 for ( std::vector< ItemT >::iterator i = vec.begin(); i != vec.end(); ++ i )
    00110                         i->encode ( *this );
    00111
    00112                 // Always realign after doing a vector
    00113                 pasteAlign ( sizeof ( unsigned long ) );
    00114         }
    00115
    00116         void CannedData( BYTE *pbData, DWORD dwSize )
    00117         {
    00118                 pasteData( pbData, dwSize );
    00119         }
    00120
    00121         inline BYTE& operator[]( int iElem ) const
    00122         {
    00123 //      #ifdef _DEBUG
    00124 //              if ( getSize( ) > 0 )
    00125 //                      assert( iElem < getSize( ) );
    00126 //      #endif
    00127                 return (m_blocks.begin( ) + (iElem / BUFFER_BLOCK_SIZE))->m_pStart[iElem % BUFFER_BLOCK_SIZE];
    00128         }
    00129
    00130 private:
    00131         class cBlockDispenser
    00132         {
    00133                 HANDLE m_hHeap;
    00134
    00135         public:
    00136                 cBlockDispenser ();
    00137                 ~cBlockDispenser ();
    00138
    00139                 BYTE *get ();
    00140                 void release (BYTE *pBlock);
    00141         };
    00142
    00143         static cBlockDispenser g_dispenser;
    00144
    00145         friend cBlock;
    00146 };
    00147
    00148 template< class DataT >
    00149 cMessage &operator<< ( cMessage &buf, DataT t )
    00150 {
    00151    buf.paste ( t );
    00152    return buf;
    00153 }
    00154
    00155 #endif  // #ifndef __MESSAGE_H