00001 /***************************************************************************** 00002 * @Brief Singleton msg receiver. Alloc receive buffer here and manager it 00003 * @Version 0.3.0 00004 * @Author Chris Liu 00005 * @Created 2015/10/30 00006 * @Modified 2015/12/16 00007 *****************************************************************************/ 00008 00009 #ifndef _DJI2MAV_MSGRECEIVER_H_ 00010 #define _DJI2MAV_MSGRECEIVER_H_ 00011 00012 00013 #include <string> 00014 #include <new> 00015 00016 #include "socketComm.h" 00017 #include "log.h" 00018 00019 namespace dji2mav { 00020 00021 class MsgReceiver { 00022 public: 00023 MsgReceiver(uint16_t bufSize) : m_bufSize(bufSize) { 00024 00025 DJI2MAV_DEBUG("Going to construct MsgReceiver..."); 00026 00027 try { 00028 m_recvBuf = new uint8_t[m_bufSize]; 00029 memset(m_recvBuf, 0, m_bufSize * sizeof(uint8_t)); 00030 } catch(std::bad_alloc &m) { 00031 DJI2MAV_FATAL("Fail to alloc memory for MsgReceiver buf! " 00032 "Exception: %s!", m.what()); 00033 exit(EXIT_FAILURE); 00034 } 00035 00036 DJI2MAV_DEBUG("...finish constructing MsgReceiver"); 00037 00038 } 00039 00040 00041 ~MsgReceiver() { 00042 DJI2MAV_DEBUG("Going to destruct MsgReceiver..."); 00043 delete []m_recvBuf; 00044 m_recvBuf = NULL; 00045 DJI2MAV_DEBUG("...finish destructing MsgReceiver."); 00046 } 00047 00048 00049 inline uint16_t getBufSize() { 00050 return m_bufSize; 00051 } 00052 00053 00054 inline uint8_t* getBuf() { 00055 return m_recvBuf; 00056 } 00057 00058 00059 inline int recv(SocketComm* comm_p) { 00060 return comm_p->recv( (void *)m_recvBuf, m_bufSize ); 00061 } 00062 00063 00064 private: 00065 uint16_t m_bufSize; 00066 uint8_t* m_recvBuf; 00067 }; 00068 00069 } //namespace dji2mav 00070 00071 00072 #endif