123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #ifndef MODULECOMMEXT_H
- #define MODULECOMMEXT_H
- #include <QtCore/qglobal.h>
- #include <QDateTime>
- #include <functional>
- #include "modulecomm.h"
- #if defined(MODULECOMMEXT_LIBRARY)
- # define MODULECOMMEXTSHARED_EXPORT Q_DECL_EXPORT
- #else
- # define MODULECOMMEXTSHARED_EXPORT Q_DECL_IMPORT
- #endif
- #include <google/protobuf/stubs/common.h>
- #include <google/protobuf/stubs/port.h>
- #include <google/protobuf/stubs/once.h>
- #include <google/protobuf/io/coded_stream.h>
- #include <google/protobuf/wire_format_lite_inl.h>
- #include <google/protobuf/descriptor.h>
- #include <google/protobuf/generated_message_reflection.h>
- #include <google/protobuf/reflection_ops.h>
- #include <google/protobuf/wire_format.h>
- #ifndef IV_MODULEEXT_FUN
- typedef std::function<void(google::protobuf::Message & xmsg)> ModuleExtFun;
- typedef void (* SMExtCallBack)(google::protobuf::Message & xmsg);
- #define IV_MODULEEXT_FUN
- #endif
- namespace iv {
- namespace modulecommext {
- template<class T>
- class modulecommmsg
- {
- private:
- T mMessage;
- void * mphandle = NULL;
- ModuleExtFun mFun;
- SMExtCallBack mpCall;
- bool mbCallPlus = false;
- int mnType = 0; //0:Not Init 1:Send 2:Recv
- void UpdateMsg(const char *strdata,
- const unsigned int nSize, const unsigned int index, const QDateTime *dt,
- const char *strmemname);
- public:
- modulecommmsg();
- ~modulecommmsg();
- void RegisterSend(const char * strcommname);
- void RegisterSend(const char * strcommname,const unsigned int nBufSize,const unsigned int nMsgBufCount
- ,iv::modulecomm::ModuleComm_TYPE xmctype = iv::modulecomm::ModuleComm_UNDEFINE,const unsigned short nport = 5100);
- void RegisterRecv(const char * strcommname,SMExtCallBack pCall,
- iv::modulecomm::ModuleComm_TYPE xmctype =iv::modulecomm:: ModuleComm_UNDEFINE,const char * strip = 0,const unsigned short = 5100);
- void RegisterRecvPlus(const char * strcommname,ModuleExtFun xFun,
- iv::modulecomm::ModuleComm_TYPE xmctype = iv::modulecomm::ModuleComm_UNDEFINE,const char * strip = 0,const unsigned short = 5100);
- void ModuleSendMsg(google::protobuf::Message & xmsg);
- void Unregister();
- void PauseComm();
- void ContintuComm();
- };
- template<class T>
- modulecommmsg<T>::modulecommmsg()
- {
- }
- template<class T>
- modulecommmsg<T>::~modulecommmsg()
- {
- if(mphandle != NULL)
- {
- Unregister();
- }
- }
- template<class T>
- void modulecommmsg<T>::UpdateMsg(const char *strdata,
- const unsigned int nSize, const unsigned int index, const QDateTime *dt,
- const char *strmemname)
- {
- (void)&index;
- (void)dt;
- (void)strmemname;
- T xmsg;
- google::protobuf::Message * pmsg = (google::protobuf::Message *)&xmsg;
- bool bParse = pmsg->ParseFromArray(strdata,nSize);
- if(bParse == false)
- {
- qDebug("modulecommext<T>::UpdateMsg Parse Fail.");
- return;
- }
- if(mbCallPlus)
- mFun(*pmsg);
- else
- (*mpCall)(*pmsg);
- }
- template<class T>
- void modulecommmsg<T>::RegisterSend(const char * strcommname)
- {
- mphandle = iv::modulecomm::RegisterSend(strcommname);
- mnType = 1;
- }
- template<class T>
- void modulecommmsg<T>::RegisterSend(const char * strcommname,const unsigned int nBufSize,const unsigned int nMsgBufCount
- ,iv::modulecomm::ModuleComm_TYPE xmctype ,const unsigned short nport)
- {
- mphandle = iv::modulecomm::RegisterSend(strcommname,nBufSize,nMsgBufCount,xmctype,nport);
- mnType = 1;
- }
- template<class T>
- void modulecommmsg<T>::RegisterRecv(const char * strcommname,SMExtCallBack pCall,
- iv::modulecomm::ModuleComm_TYPE xmctype,const char * strip,const unsigned short nport)
- {
- mpCall = pCall;
- mbCallPlus = false;
- ModuleFun funext = std::bind(&modulecommmsg::UpdateMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
- mphandle = iv::modulecomm::RegisterRecvPlus(strcommname,funext,xmctype,strip,nport);
- mnType = 2;
- }
- template<class T>
- void modulecommmsg<T>::RegisterRecvPlus(const char * strcommname,ModuleExtFun xFun,
- iv::modulecomm::ModuleComm_TYPE xmctype ,const char * strip ,const unsigned short nport)
- {
- mFun = xFun;
- mbCallPlus = true;
- ModuleFun funext = std::bind(&modulecommmsg::UpdateMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
- mphandle = iv::modulecomm::RegisterRecvPlus(strcommname,funext,xmctype,strip,nport);
- mnType = 2;
- }
- template<class T>
- void modulecommmsg<T>::ModuleSendMsg(google::protobuf::Message & xmsg)
- {
- if((mnType != 1)||(mphandle == NULL))
- {
- qDebug("This Handle is not send. type : %d ",mnType);
- return;
- }
- int ndatasize = xmsg.ByteSize();
- std::shared_ptr<char> str_ptr= std::shared_ptr<char>(new char[ndatasize]);
- bool bSer = xmsg.SerializeToArray(str_ptr.get(),ndatasize);
- if(bSer == false)
- {
- qDebug("modulecommext<T>::ModuleSendMsg serialize fail.");
- return;
- }
- iv::modulecomm::ModuleSendMsg(mphandle,str_ptr.get(),ndatasize);
- }
- template<class T>
- void modulecommmsg<T>::Unregister()
- {
- if((mnType == 0)||(mphandle == NULL))
- {
- return;
- }
- iv::modulecomm::Unregister(mphandle);
- mphandle = NULL;
- }
- template<class T>
- void modulecommmsg<T>::PauseComm()
- {
- if((mnType == 0)||(mphandle == NULL))
- {
- qDebug("modulecommext<T>::PauseComm handle not init.");
- return;
- }
- iv::modulecomm::PauseComm(mphandle);
- }
- template<class T>
- void modulecommmsg<T>::ContintuComm()
- {
- if((mnType == 0)||(mphandle == NULL))
- {
- qDebug("modulecommext<T>::PauseComm handle not init.");
- return;
- }
- iv::modulecomm::ContintuComm(mphandle);
- }
- }
- }
- #endif // MODULECOMMEXT_H
|