TitanPacketMsg.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "common/won.h"
  2. #include "msg/TMessage.h"
  3. #include "msg/BadMsgException.h"
  4. #include "TitanPacketMsg.h"
  5. #include "TitanInterfaceC.h"
  6. using WONMsg::MiniMessage;
  7. // ** TitanPacketMsg **
  8. // ** Constructors / Destructor
  9. // Default ctor
  10. TitanPacketMsg::TitanPacketMsg(unsigned char theType, bool encrypted) :
  11. mBlob(NULL),
  12. mBlobLen(0),
  13. mIncludeGameName(theType != TITANMSGTYPE_GAME)
  14. {
  15. SetServiceType(encrypted ? MyEncryptServiceType : MyServiceType);
  16. SetMessageType(theType);
  17. }
  18. // TMessage ctor
  19. TitanPacketMsg::TitanPacketMsg(const MiniMessage& theMsgR) :
  20. MiniMessage(theMsgR),
  21. mIncludeGameName(true),
  22. mBlob(NULL),
  23. mBlobLen(0)
  24. {
  25. Unpack();
  26. }
  27. // Copy ctor
  28. TitanPacketMsg::TitanPacketMsg(const TitanPacketMsg& theMsgR) :
  29. MiniMessage(theMsgR),
  30. mIncludeGameName(theMsgR.mIncludeGameName),
  31. mGameName(theMsgR.mGameName),
  32. mBlob(theMsgR.mBlob),
  33. mBlobLen(theMsgR.mBlobLen)
  34. {
  35. SetBlob(theMsgR.GetBlob(), theMsgR.GetBlobLen());
  36. }
  37. // Destructor
  38. TitanPacketMsg::~TitanPacketMsg(void)
  39. {}
  40. // ** Public Methods
  41. // Assignment operator
  42. TitanPacketMsg&
  43. TitanPacketMsg::operator=(const TitanPacketMsg& theMsgR)
  44. {
  45. if (this != &theMsgR)
  46. {
  47. MiniMessage::operator=(theMsgR);
  48. mIncludeGameName = theMsgR.mIncludeGameName;
  49. mGameName = theMsgR.mGameName;
  50. mBlob = theMsgR.mBlob;
  51. mBlobLen = theMsgR.mBlobLen;
  52. }
  53. return *this;
  54. }
  55. // TitanPacketMsg::Pack
  56. // Virtual method from MiniMessage. Packs data into message buffer and
  57. // sets the new message length.
  58. void*
  59. TitanPacketMsg::Pack(void)
  60. {
  61. MiniMessage::Pack();
  62. // append game name if required
  63. if (mIncludeGameName)
  64. Append_PW_STRING(mGameName);
  65. // append data
  66. AppendBytes(mBlobLen, mBlob);
  67. return GetDataPtr();
  68. }
  69. // TitanPacketMsg::Unpack
  70. // Virtual method from MiniMessage. Extracts data from message buffer.
  71. void
  72. TitanPacketMsg::Unpack(void)
  73. {
  74. MiniMessage::Unpack();
  75. if ((GetServiceType() != MyEncryptServiceType) &&
  76. (GetServiceType() != MyServiceType))
  77. {
  78. throw WONMsg::BadMsgException(*this, __LINE__, __FILE__,
  79. "Not a TitanPacketMsg message.");
  80. }
  81. mIncludeGameName = (GetMessageType() != TITANMSGTYPE_GAME);
  82. if (mIncludeGameName)
  83. ReadWString(mGameName);
  84. mBlobLen = BytesLeftToRead();
  85. mBlob = ReadBytes(BytesLeftToRead());
  86. }