Clone of UAS2 @ https://github.com/drudgedance/uas2

Message.h 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * This file is part of UAS2.
  3. *
  4. * UAS2 is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * UAS2 is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with UASv1; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. /**
  18. * @file Message.h
  19. */
  20. #ifndef __MESSAGE_H
  21. #define __MESSAGE_H
  22. #include <deque>
  23. #include <vector>
  24. #include <cassert>
  25. #include "Shared.h"
  26. #define BUFFER_BLOCK_SIZE 1024
  27. class cMessage
  28. {
  29. class cBlock
  30. {
  31. friend class cMessage;
  32. mutable BYTE *m_pStart, *m_pUsed;
  33. public:
  34. cBlock ();
  35. cBlock ( const cBlock &from );
  36. ~cBlock ();
  37. BOOL pasteData ( BYTE *&pData, int &nBytes );
  38. int getAlign ( int nBoundary ) const;
  39. BOOL pasteAlign ( int &nBytes );
  40. size_t getUsed () const
  41. {
  42. return ( m_pUsed - m_pStart );
  43. }
  44. BOOL getData ( size_t &nOffset, size_t &nLength, BYTE * &pBuffer ) const;
  45. private:
  46. // Disabled functions
  47. cBlock &operator= ( const cBlock & );
  48. };
  49. typedef std::deque< cBlock > cBlockList;
  50. cBlockList m_blocks;
  51. // Place a block of data into the message
  52. public:
  53. cMessage ();
  54. cMessage (BYTE* pbPacket, int nSize);
  55. ~cMessage( )
  56. {
  57. while ( !m_blocks.empty( ) )
  58. m_blocks.pop_front( );
  59. }
  60. // Basic functions for inserting data
  61. void pasteData ( BYTE *pData, int nBytes );
  62. void pasteAlign ( int nBoundary );
  63. void pasteString ( const char *str );
  64. // Functions for retrieving data
  65. size_t getSize () const;
  66. void getData ( size_t nOffset, size_t nLength, BYTE *pBuffer ) const;
  67. template< class DataT >
  68. inline void paste ( DataT dt )
  69. {
  70. pasteData ( reinterpret_cast< BYTE * > ( &dt ), sizeof ( DataT ) );
  71. }
  72. // String pastes
  73. template<>
  74. inline void paste ( char *str )
  75. {
  76. pasteString ( static_cast< const char * >( str ) );
  77. }
  78. template<>
  79. inline void paste ( const char *str )
  80. {
  81. pasteString ( static_cast< const char * >( str ) );
  82. }
  83. // Collection pastes
  84. template< class ItemT >
  85. void paste ( std::vector< ItemT > &vec )
  86. {
  87. for ( std::vector< ItemT >::iterator i = vec.begin(); i != vec.end(); ++ i )
  88. i->encode ( *this );
  89. // Always realign after doing a vector
  90. pasteAlign ( sizeof ( unsigned long ) );
  91. }
  92. void CannedData( BYTE *pbData, DWORD dwSize )
  93. {
  94. pasteData( pbData, dwSize );
  95. }
  96. inline BYTE& operator[]( int iElem ) const
  97. {
  98. // #ifdef _DEBUG
  99. // if ( getSize( ) > 0 )
  100. // assert( iElem < getSize( ) );
  101. // #endif
  102. return (m_blocks.begin( ) + (iElem / BUFFER_BLOCK_SIZE))->m_pStart[iElem % BUFFER_BLOCK_SIZE];
  103. }
  104. private:
  105. class cBlockDispenser
  106. {
  107. HANDLE m_hHeap;
  108. public:
  109. cBlockDispenser ();
  110. ~cBlockDispenser ();
  111. BYTE *get ();
  112. void release (BYTE *pBlock);
  113. };
  114. static cBlockDispenser g_dispenser;
  115. friend cBlock;
  116. };
  117. template< class DataT >
  118. cMessage &operator<< ( cMessage &buf, DataT t )
  119. {
  120. buf.paste ( t );
  121. return buf;
  122. }
  123. #endif // #ifndef __MESSAGE_H