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

RecvPacket.h 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 RecvPacket.h
  19. */
  20. #ifndef __RECVPACKET_H
  21. #define __RECVPACKET_H
  22. #include <cassert>
  23. #include "Shared.h"
  24. class cRecvPacket
  25. {
  26. public:
  27. cRecvPacket( )
  28. : m_wSize( 0 )
  29. {
  30. m_pcTH = reinterpret_cast< cTransportHeader * >( m_bData );
  31. }
  32. ~cRecvPacket( ) {}
  33. inline DWORD GetSequence ( ) { return m_pcTH->m_dwSequence; }
  34. inline DWORD GetFlags ( ) { return m_pcTH->m_dwFlags; }
  35. inline DWORD GetCRC ( ) { return m_pcTH->m_dwCRC; }
  36. inline WORD GetLogicalID( ) { return m_pcTH->m_wLogicalID; }
  37. inline WORD GetTime ( ) { return m_pcTH->m_wTime; }
  38. inline WORD GetTotalSize( ) { return m_pcTH->m_wTotalSize; }
  39. inline WORD GetTable ( ) { return m_pcTH->m_wTable; }
  40. inline BYTE *GetPayloadFront( ) { return m_bData + sizeof( cTransportHeader ); }
  41. inline BYTE *GetPayload ( WORD wOffset ) { return m_bData + sizeof( cTransportHeader ) + wOffset; }
  42. inline BYTE *GetPayloadBack ( ) { return m_bData + sizeof( cTransportHeader ) + m_pcTH->m_wTotalSize; }
  43. inline void CopyPayload( WORD wOffset, void *pDest, WORD wSize )
  44. {
  45. CopyMemory( pDest, m_bData + sizeof( cTransportHeader ) + wOffset, wSize );
  46. }
  47. inline BYTE& operator[]( int n )
  48. {
  49. #ifdef _DEBUG
  50. WORD wMaxSize = GetTotalSize( ) + sizeof( cTransportHeader );
  51. if ( wMaxSize > 0 )
  52. assert( n < wMaxSize );
  53. #endif
  54. return m_bData[n];
  55. }
  56. inline int GetPadding( BYTE *pbStart );
  57. BYTE m_bData[MAX_PACKET_SIZE];
  58. WORD m_wSize;
  59. SOCKADDR_IN m_saSockAddr;
  60. private:
  61. cTransportHeader *m_pcTH;
  62. };
  63. inline int cRecvPacket::GetPadding( BYTE *pbStart )
  64. {
  65. WORD wSize = *(WORD *)pbStart;
  66. pbStart += (sizeof( WORD ) + wSize);
  67. DWORD dwDiff = pbStart - m_bData;
  68. if( dwDiff < 4 )
  69. return 4 - dwDiff;
  70. else if ( (dwDiff % 4) != 0 )
  71. return 4 - (dwDiff % 4);
  72. else
  73. return 0;
  74. }
  75. #endif // #ifndef __RECVPACKET_H