modulecommext.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #ifndef MODULECOMMEXT_H
  2. #define MODULECOMMEXT_H
  3. #include <QtCore/qglobal.h>
  4. #include <QDateTime>
  5. #include <functional>
  6. #include "modulecomm.h"
  7. #if defined(MODULECOMMEXT_LIBRARY)
  8. # define MODULECOMMEXTSHARED_EXPORT Q_DECL_EXPORT
  9. #else
  10. # define MODULECOMMEXTSHARED_EXPORT Q_DECL_IMPORT
  11. #endif
  12. #include <google/protobuf/stubs/common.h>
  13. #include <google/protobuf/stubs/port.h>
  14. #include <google/protobuf/stubs/once.h>
  15. #include <google/protobuf/io/coded_stream.h>
  16. #include <google/protobuf/wire_format_lite_inl.h>
  17. #include <google/protobuf/descriptor.h>
  18. #include <google/protobuf/generated_message_reflection.h>
  19. #include <google/protobuf/reflection_ops.h>
  20. #include <google/protobuf/wire_format.h>
  21. #ifndef IV_MODULEEXT_FUN
  22. typedef std::function<void(google::protobuf::Message & xmsg)> ModuleExtFun;
  23. typedef void (* SMExtCallBack)(google::protobuf::Message & xmsg);
  24. #define IV_MODULEEXT_FUN
  25. #endif
  26. namespace iv {
  27. namespace modulecommext {
  28. template<class T>
  29. class modulecommmsg
  30. {
  31. private:
  32. T mMessage;
  33. void * mphandle = NULL;
  34. ModuleExtFun mFun;
  35. SMExtCallBack mpCall;
  36. bool mbCallPlus = false;
  37. int mnType = 0; //0:Not Init 1:Send 2:Recv
  38. void UpdateMsg(const char *strdata,
  39. const unsigned int nSize, const unsigned int index, const QDateTime *dt,
  40. const char *strmemname);
  41. public:
  42. modulecommmsg();
  43. ~modulecommmsg();
  44. void RegisterSend(const char * strcommname);
  45. void RegisterSend(const char * strcommname,const unsigned int nBufSize,const unsigned int nMsgBufCount
  46. ,iv::modulecomm::ModuleComm_TYPE xmctype = iv::modulecomm::ModuleComm_UNDEFINE,const unsigned short nport = 5100);
  47. void RegisterRecv(const char * strcommname,SMExtCallBack pCall,
  48. iv::modulecomm::ModuleComm_TYPE xmctype =iv::modulecomm:: ModuleComm_UNDEFINE,const char * strip = 0,const unsigned short = 5100);
  49. void RegisterRecvPlus(const char * strcommname,ModuleExtFun xFun,
  50. iv::modulecomm::ModuleComm_TYPE xmctype = iv::modulecomm::ModuleComm_UNDEFINE,const char * strip = 0,const unsigned short = 5100);
  51. void ModuleSendMsg(google::protobuf::Message & xmsg);
  52. void Unregister();
  53. void PauseComm();
  54. void ContintuComm();
  55. };
  56. template<class T>
  57. modulecommmsg<T>::modulecommmsg()
  58. {
  59. }
  60. template<class T>
  61. modulecommmsg<T>::~modulecommmsg()
  62. {
  63. if(mphandle != NULL)
  64. {
  65. Unregister();
  66. }
  67. }
  68. template<class T>
  69. void modulecommmsg<T>::UpdateMsg(const char *strdata,
  70. const unsigned int nSize, const unsigned int index, const QDateTime *dt,
  71. const char *strmemname)
  72. {
  73. (void)&index;
  74. (void)dt;
  75. (void)strmemname;
  76. T xmsg;
  77. google::protobuf::Message * pmsg = (google::protobuf::Message *)&xmsg;
  78. bool bParse = pmsg->ParseFromArray(strdata,nSize);
  79. if(bParse == false)
  80. {
  81. qDebug("modulecommext<T>::UpdateMsg Parse Fail.");
  82. return;
  83. }
  84. if(mbCallPlus)
  85. mFun(*pmsg);
  86. else
  87. (*mpCall)(*pmsg);
  88. }
  89. template<class T>
  90. void modulecommmsg<T>::RegisterSend(const char * strcommname)
  91. {
  92. mphandle = iv::modulecomm::RegisterSend(strcommname);
  93. mnType = 1;
  94. }
  95. template<class T>
  96. void modulecommmsg<T>::RegisterSend(const char * strcommname,const unsigned int nBufSize,const unsigned int nMsgBufCount
  97. ,iv::modulecomm::ModuleComm_TYPE xmctype ,const unsigned short nport)
  98. {
  99. mphandle = iv::modulecomm::RegisterSend(strcommname,nBufSize,nMsgBufCount,xmctype,nport);
  100. mnType = 1;
  101. }
  102. template<class T>
  103. void modulecommmsg<T>::RegisterRecv(const char * strcommname,SMExtCallBack pCall,
  104. iv::modulecomm::ModuleComm_TYPE xmctype,const char * strip,const unsigned short nport)
  105. {
  106. mpCall = pCall;
  107. mbCallPlus = false;
  108. ModuleFun funext = std::bind(&modulecommmsg::UpdateMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  109. mphandle = iv::modulecomm::RegisterRecvPlus(strcommname,funext,xmctype,strip,nport);
  110. mnType = 2;
  111. }
  112. template<class T>
  113. void modulecommmsg<T>::RegisterRecvPlus(const char * strcommname,ModuleExtFun xFun,
  114. iv::modulecomm::ModuleComm_TYPE xmctype ,const char * strip ,const unsigned short nport)
  115. {
  116. mFun = xFun;
  117. mbCallPlus = true;
  118. ModuleFun funext = std::bind(&modulecommmsg::UpdateMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  119. mphandle = iv::modulecomm::RegisterRecvPlus(strcommname,funext,xmctype,strip,nport);
  120. mnType = 2;
  121. }
  122. template<class T>
  123. void modulecommmsg<T>::ModuleSendMsg(google::protobuf::Message & xmsg)
  124. {
  125. if((mnType != 1)||(mphandle == NULL))
  126. {
  127. qDebug("This Handle is not send. type : %d ",mnType);
  128. return;
  129. }
  130. int ndatasize = xmsg.ByteSize();
  131. std::shared_ptr<char> str_ptr= std::shared_ptr<char>(new char[ndatasize]);
  132. bool bSer = xmsg.SerializeToArray(str_ptr.get(),ndatasize);
  133. if(bSer == false)
  134. {
  135. qDebug("modulecommext<T>::ModuleSendMsg serialize fail.");
  136. return;
  137. }
  138. iv::modulecomm::ModuleSendMsg(mphandle,str_ptr.get(),ndatasize);
  139. }
  140. template<class T>
  141. void modulecommmsg<T>::Unregister()
  142. {
  143. if((mnType == 0)||(mphandle == NULL))
  144. {
  145. return;
  146. }
  147. iv::modulecomm::Unregister(mphandle);
  148. mphandle = NULL;
  149. }
  150. template<class T>
  151. void modulecommmsg<T>::PauseComm()
  152. {
  153. if((mnType == 0)||(mphandle == NULL))
  154. {
  155. qDebug("modulecommext<T>::PauseComm handle not init.");
  156. return;
  157. }
  158. iv::modulecomm::PauseComm(mphandle);
  159. }
  160. template<class T>
  161. void modulecommmsg<T>::ContintuComm()
  162. {
  163. if((mnType == 0)||(mphandle == NULL))
  164. {
  165. qDebug("modulecommext<T>::PauseComm handle not init.");
  166. return;
  167. }
  168. iv::modulecomm::ContintuComm(mphandle);
  169. }
  170. }
  171. }
  172. #endif // MODULECOMMEXT_H