/TGame/TCommon/Message/MsgBuilderSimple.cpp

http://awoe.googlecode.com/ · C++ · 63 lines · 47 code · 10 blank · 6 comment · 4 complexity · 1ba8596061fd65aa7730429b89838676 MD5 · raw file

  1. #include "stdafx.h"
  2. #include "MsgBuilderSimple.h"
  3. #include "MsgCategory.h"
  4. #include "MsgFactoryNetwork.h"
  5. //#include "MsgLogin.h"
  6. //#include "MsgPlayer.h"
  7. const char c_msg_header_flag = 47;
  8. const short s_msg_header_len = 100;
  9. MsgBuilderWithFactory::MsgBuilderWithFactory(IMsgFactory* pFact /* = NULL */)
  10. :m_pMsgFact(pFact)
  11. {
  12. if (m_pMsgFact==NULL)
  13. {
  14. m_pMsgFact = new MsgFactoryNetwork;
  15. }
  16. }
  17. bool
  18. MsgBuilderWithFactory::onEncodeMsg(IMsg* pMsg, MsgOStream& stream)
  19. {
  20. if (pMsg!=NULL)
  21. {
  22. stream << c_msg_header_flag;
  23. stream << pMsg->getMsgCate() << pMsg->getMsgID() << s_msg_header_len;
  24. return pMsg->encode(stream);
  25. }
  26. else
  27. {
  28. return false;
  29. }
  30. }
  31. bool
  32. MsgBuilderWithFactory::onDecodeMsg(IMsg*& pMsg, MsgIStream& stream)
  33. {
  34. char cFlag = 0;
  35. stream >> cFlag;
  36. //if (cType!=c_msg_header_type || cTag!=c_msg_header_tag)
  37. //{
  38. // LOG_ERROR("Failed to decode msg, invalid message type or tag!");
  39. //}
  40. char ucMC = 0;
  41. short nMID = 0, nLen = 0;
  42. stream >> ucMC >> nMID >>nLen;
  43. pMsg = m_pMsgFact->createMsg(ucMC, nMID);
  44. if (pMsg != NULL)
  45. {
  46. return pMsg->decode(stream);
  47. }
  48. else
  49. {
  50. LOG_ERROR_V("Failed to decode msg, message cate or id %d, %d!", ucMC, nMID);
  51. return false;
  52. }
  53. }