mqttmsgpool.cpp 845 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <string.h>
  2. #include "mqttmsg.h"
  3. /////////////////////////////////////////////////////////////////////////////
  4. //
  5. CMqttMessagePool::CMqttMessagePool(void)
  6. {
  7. for(int i = 0; i < _MSG_POOL_SIZE; i++)
  8. {
  9. CMqttMessage *pMsg = CMqttMessage::CreateMessage();
  10. pMsg->SetPoolMsg(true);
  11. pMsg->SetAvailable(true);
  12. m_pool[i] = pMsg;
  13. }
  14. }
  15. CMqttMessagePool::~CMqttMessagePool(void)
  16. {
  17. for(auto i = m_pool.begin(); i != m_pool.end(); ++i)
  18. {
  19. CMqttMessage *pMsg = *i;
  20. pMsg->Release(true);
  21. }
  22. }
  23. CMqttMessage* CMqttMessagePool::GetMsgFromPool(void)
  24. {
  25. for(auto i = m_pool.begin(); i != m_pool.end(); ++i)
  26. {
  27. CMqttMessage *pMsg = *i;
  28. if(pMsg->IsAvailable())
  29. {
  30. pMsg->SetAvailable(false);
  31. return pMsg;
  32. }
  33. }
  34. return NULL;
  35. }
  36. void CMqttMessagePool::ReturnMsgToPool(CMqttMessage *pMsg)
  37. {
  38. if(pMsg)
  39. pMsg->SetAvailable(true);
  40. }