#ifndef MODULECOMMEXT_H #define MODULECOMMEXT_H #include #include #include #include "modulecomm.h" #if defined(MODULECOMMEXT_LIBRARY) # define MODULECOMMEXTSHARED_EXPORT Q_DECL_EXPORT #else # define MODULECOMMEXTSHARED_EXPORT Q_DECL_IMPORT #endif #include #include #include #include #include #include #include #include #include #ifndef IV_MODULEEXT_FUN typedef std::function ModuleExtFun; typedef void (* SMExtCallBack)(google::protobuf::Message & xmsg); #define IV_MODULEEXT_FUN #endif namespace iv { namespace modulecommext { template 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 modulecommmsg::modulecommmsg() { } template modulecommmsg::~modulecommmsg() { if(mphandle != NULL) { Unregister(); } } template void modulecommmsg::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::UpdateMsg Parse Fail."); return; } if(mbCallPlus) mFun(*pmsg); else (*mpCall)(*pmsg); } template void modulecommmsg::RegisterSend(const char * strcommname) { mphandle = iv::modulecomm::RegisterSend(strcommname); mnType = 1; } template void modulecommmsg::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 void modulecommmsg::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 void modulecommmsg::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 void modulecommmsg::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 str_ptr= std::shared_ptr(new char[ndatasize]); bool bSer = xmsg.SerializeToArray(str_ptr.get(),ndatasize); if(bSer == false) { qDebug("modulecommext::ModuleSendMsg serialize fail."); return; } iv::modulecomm::ModuleSendMsg(mphandle,str_ptr.get(),ndatasize); } template void modulecommmsg::Unregister() { if((mnType == 0)||(mphandle == NULL)) { return; } iv::modulecomm::Unregister(mphandle); mphandle = NULL; } template void modulecommmsg::PauseComm() { if((mnType == 0)||(mphandle == NULL)) { qDebug("modulecommext::PauseComm handle not init."); return; } iv::modulecomm::PauseComm(mphandle); } template void modulecommmsg::ContintuComm() { if((mnType == 0)||(mphandle == NULL)) { qDebug("modulecommext::PauseComm handle not init."); return; } iv::modulecomm::ContintuComm(mphandle); } } } #endif // MODULECOMMEXT_H