Browse Source

add modulecommext for direct use commulicate proto message.

yuchuli 3 years ago
parent
commit
fb5bd1a7a9

+ 12 - 0
src/include/proto/testmodulecommext.proto

@@ -0,0 +1,12 @@
+syntax = "proto2";
+
+package iv;
+
+message testmodulecommext
+{
+	required uint32 a	= 1;
+	required uint32 b 	= 2;
+	optional int64 time	= 3;
+};
+
+

+ 77 - 4
src1/common/modulecomm/main.cpp

@@ -6,6 +6,13 @@
 
 #include "modulecomm.h"
 
+#include "modulecommext.h"
+
+#include <QSharedMemory>
+
+#include "testmodulecommext.pb.h"
+
+
 void * gpa;
 
 std::chrono::time_point<std::chrono::steady_clock, std::chrono::duration<double,std::nano>> t1,t2;
@@ -49,7 +56,7 @@ void threadsend2()
     int nsendlen = 10000;
     while(1)
     {
-        std::this_thread::sleep_for(std::chrono::milliseconds(1));
+        std::this_thread::sleep_for(std::chrono::milliseconds(100));
         qint64 ntime = QDateTime::currentMSecsSinceEpoch();
         memcpy(strdata,&ntime,8);
         if(nsendlen<2000000)nsendlen = nsendlen*1001/1000;
@@ -62,6 +69,42 @@ void threadsend2()
 #include <future>
 
 
+void testfunc(int a)
+{
+    a = a+1;
+    std::cout<<"a = "<<a<<std::endl;
+
+}
+
+void testfunc2(double a)
+{
+    a = a+1.1;
+    std::cout<<"a = "<<a<<std::endl;
+
+}
+
+
+iv::modulecommext::modulecommmsg<iv::testmodulecommext> * gmsgtestsend,* gmsgtestrecv;
+
+
+void ListenProto(google::protobuf::Message & xmsg)
+{
+    iv::testmodulecommext * pmsg = (iv::testmodulecommext *)&xmsg;
+    qDebug("time : %lld",pmsg->time());
+}
+
+void threadsendproto()
+{
+    while(1)
+    {
+        iv::testmodulecommext xmsg;
+        xmsg.set_a(1);
+        xmsg.set_b(2);
+        xmsg.set_time(QDateTime::currentMSecsSinceEpoch());
+        gmsgtestsend->ModuleSendMsg(xmsg);
+        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
+    }
+}
 
 int main(int argc, char *argv[])
 {
@@ -92,9 +135,39 @@ int main(int argc, char *argv[])
 //    iv::modulecomm::RegisterRecv("test1",testcall,iv::modulecomm::ModuleComm_INTERIOR);
 //    std::thread * xthread = new std::thread(threadsend);
 
-    gpa = iv::modulecomm::RegisterSend("test1",1000,1,iv::modulecomm::ModuleComm_FASTRTPS);
-    iv::modulecomm::RegisterRecv("test1",testcall2,iv::modulecomm::ModuleComm_FASTRTPS);
+
+//    QSharedMemory * pmem = new QSharedMemory("hello");
+//    pmem->create(1000);
+//    qDebug("create");
+//    pmem->lock();
+//    qDebug("lock");
+//    gpa = iv::modulecomm::RegisterSend("test1",1000,1);
+//    gpa = iv::modulecomm::RegisterSend("test1");
 //    iv::modulecomm::RegisterRecv("test1",testcall2);
-    std::thread * xthread = new std::thread(threadsend2);
+//    gpa = iv::modulecomm::RegisterSend("test1",1000,1,iv::modulecomm::ModuleComm_FASTRTPS);
+//    iv::modulecomm::RegisterRecv("test1",testcall2,iv::modulecomm::ModuleComm_FASTRTPS);
+//    iv::modulecomm::RegisterRecv("test1",testcall2);
+//    std::thread * xthread = new std::thread(threadsend2);
+
+
+      gmsgtestsend = new iv::modulecommext::modulecommmsg<iv::testmodulecommext>();
+
+      gmsgtestsend->RegisterSend("testsendproto");
+
+      gmsgtestrecv = new iv::modulecommext::modulecommmsg<iv::testmodulecommext>();
+
+      gmsgtestrecv->RegisterRecv("testsendproto",ListenProto);
+
+      std::thread * xthreadproto = new std::thread(threadsendproto);
+
+      std::string  a1;
+      int b1 = a1;
+
+//    extfunc<int>::pfun x = &testfunc;
+//    extfunc<double>::pfun x2 = &testfunc2;
+//    (*x)(1);
+//    (*x2)(2.0);
+
+
     return a.exec();
 }

+ 73 - 3
src1/common/modulecomm/modulecomm.cpp

@@ -9,6 +9,9 @@
 #include "modulecomm_fastrtps_tcp.h"
 #endif
 
+#include <QtXml>
+#include <QFile>
+
 namespace iv {
 namespace modulecomm {
 
@@ -18,7 +21,59 @@ struct ModeduleInfo
     ModuleComm_TYPE mmctype;
 };
 
+static ModuleComm_TYPE getdefmodulecommtype()
+{
+#ifdef  ANDROID
+    return ModuleComm_INTERIOR;
+#endif
+    ModuleComm_TYPE xrtn = ModuleComm_SHAREMEM;
+    QDomDocument xdoc;
+    QFile file("./modulecomm.xml");
+    if (!file.open(QIODevice::ReadOnly))
+    {
+        return xrtn;
+    }
+    if (!xdoc.setContent(&file)) {
+        file.close();
+        return xrtn;
+    }
+    file.close();
+    QDomElement docElem = xdoc.documentElement();
 
+    QDomNode n = docElem.firstChild();
+    while(!n.isNull())
+    {
+    QDomElement e = n.toElement();
+    if(e.nodeName() == "commtype")
+    {
+        QString strvalue = e.attribute("value","0");
+        if(strvalue == "0")
+        {
+            xrtn = ModuleComm_SHAREMEM;
+        }
+        if(strvalue == "1")
+        {
+            xrtn = ModuleComm_INTERIOR;
+        }
+#ifdef USE_FASTRTPS
+        if(strvalue == "2")
+        {
+            xrtn = ModuleComm_FASTRTPS;
+        }
+#endif
+        break;
+    }
+        n = n.nextSibling();
+    }
+
+    return xrtn;
+}
+
+
+void *  RegisterSend(const char * strcommname)
+{
+    return RegisterSend(strcommname,1000,1);
+}
 
 void *  RegisterSend(const char * strcommname,const unsigned int nBufSize,const unsigned int nMsgBufCount,
                      ModuleComm_TYPE xmctype,const unsigned short nport)
@@ -27,7 +82,12 @@ void *  RegisterSend(const char * strcommname,const unsigned int nBufSize,const
     iv::modulecomm::ModeduleInfo * pmi = new iv::modulecomm::ModeduleInfo;
     pmi->mmctype = xmctype;
     pmi->mphandle = 0;
-    switch (xmctype) {
+    ModuleComm_TYPE mctype = xmctype;
+    if(mctype == ModuleComm_UNDEFINE)
+    {
+        mctype = getdefmodulecommtype();
+    }
+    switch (mctype) {
     case ModuleComm_SHAREMEM:
     {
         iv::modulecomm_shm * p = new iv::modulecomm_shm();
@@ -73,7 +133,12 @@ void  *  RegisterRecv(const char * strcommname,SMCallBack pCall,ModuleComm_TYPE
     iv::modulecomm::ModeduleInfo * pmi = new iv::modulecomm::ModeduleInfo;
     pmi->mmctype = xmctype;
     pmi->mphandle = 0;
-    switch (xmctype) {
+    ModuleComm_TYPE mctype = xmctype;
+    if(mctype == ModuleComm_UNDEFINE)
+    {
+        mctype = getdefmodulecommtype();
+    }
+    switch (mctype) {
     case ModuleComm_SHAREMEM:
     {
         iv::modulecomm_shm * p = new iv::modulecomm_shm();
@@ -119,7 +184,12 @@ void *  RegisterRecvPlus(const char * strcommname,ModuleFun xFun,
     iv::modulecomm::ModeduleInfo * pmi = new iv::modulecomm::ModeduleInfo;
     pmi->mmctype = xmctype;
     pmi->mphandle = 0;
-    switch (xmctype) {
+    ModuleComm_TYPE mctype = xmctype;
+    if(mctype == ModuleComm_UNDEFINE)
+    {
+        mctype = getdefmodulecommtype();
+    }
+    switch (mctype) {
     case ModuleComm_SHAREMEM:
     {
         iv::modulecomm_shm * p = new iv::modulecomm_shm();

+ 7 - 4
src1/common/modulecomm/modulecomm.h

@@ -41,16 +41,19 @@ enum ModuleComm_TYPE
     ModuleComm_SHAREMEM = 0,
     ModuleComm_INTERIOR = 1,
     ModuleComm_FASTRTPS = 2,
-    ModuleComm_FASTRTPS_TCP = 3
+    ModuleComm_FASTRTPS_TCP = 3,
+    ModuleComm_UNDEFINE = 4
 };
 
+void * MODULECOMMSHARED_EXPORT RegisterSend(const char * strcommname);
+
 
 void * MODULECOMMSHARED_EXPORT RegisterSend(const char * strcommname,const unsigned int nBufSize,const unsigned int nMsgBufCount
-                                            ,ModuleComm_TYPE xmctype = ModuleComm_SHAREMEM,const unsigned short nport = 5100);
+                                            ,ModuleComm_TYPE xmctype = ModuleComm_UNDEFINE,const unsigned short nport = 5100);
 void * MODULECOMMSHARED_EXPORT RegisterRecv(const char * strcommname,SMCallBack pCall,
-                                            ModuleComm_TYPE xmctype = ModuleComm_SHAREMEM,const char * strip = 0,const unsigned short = 5100);
+                                            ModuleComm_TYPE xmctype = ModuleComm_UNDEFINE,const char * strip = 0,const unsigned short = 5100);
 void * MODULECOMMSHARED_EXPORT RegisterRecvPlus(const char * strcommname,ModuleFun xFun,
-                                                ModuleComm_TYPE xmctype = ModuleComm_SHAREMEM,const char * strip = 0,const unsigned short = 5100);
+                                                ModuleComm_TYPE xmctype = ModuleComm_UNDEFINE,const char * strip = 0,const unsigned short = 5100);
 
 void MODULECOMMSHARED_EXPORT ModuleSendMsg(void * pHandle,const char * strdata,const unsigned int nDataLen);
 void MODULECOMMSHARED_EXPORT Unregister(void * pHandle);

+ 9 - 0
src1/common/modulecomm/modulecommext.cpp

@@ -0,0 +1,9 @@
+
+#include "modulecommext.h"
+
+namespace iv {
+namespace modulecommext {
+
+
+}
+}

+ 208 - 0
src1/common/modulecomm/modulecommext.h

@@ -0,0 +1,208 @@
+#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

+ 1 - 0
src1/common/modulecomm/shm/procsm.cpp

@@ -272,6 +272,7 @@ bool procsm::AttachMem()
         if(mpASM != 0)
         {
             if(mpASM->isAttached())mpASM->detach();
+            delete mpASM;
         }
         mpASM = new QSharedMemory(mASM_State.mstrshmname);
 

+ 2 - 0
src1/common/modulecomm/testmodulecomm.pro

@@ -1,11 +1,13 @@
 QT -= gui
 
 QT += dbus
+QT       += xml
 
 
 CONFIG += c++11 console
 CONFIG -= app_bundle
 
+#DEFINES += ANDROID
 DEFINES += USE_FASTRTPS
 DEFINES += USEDBUS