Browse Source

change modulecomm. solve sometimes init fail. test ok.

yuchuli 2 years ago
parent
commit
f5813ee491

+ 1 - 1
src/common/modulecomm/modulecomm.pro

@@ -18,7 +18,7 @@ win32: DEFINES += SYSTEM_WIN
 
 DEFINES += MODULECOMM_NO_FASTRTPS
 
-#DEFINES += USE_GROUPUDP
+DEFINES += USE_GROUPUDP
 
 if(contains(DEFINES,USE_GROUPUDP)){
 QT += network

+ 33 - 49
src/common/modulecomm/shm/procsm.cpp

@@ -34,24 +34,24 @@
 //#define BOLDCYAN    "\033[1m\033[36m"      /* Bold Cyan */
 //#define BOLDWHITE   "\033[1m\033[37m"      /* Bold White */
 
-class AttachThread : public QThread
-{
-  public:
-    AttachThread(QSharedMemory * pa,bool & bAttach)
-    {
-       mbAttach = bAttach;
-       mpa = pa;
-       mbrun = true;
-    }
-    QSharedMemory * mpa;
-    bool mbAttach = false;
-    bool mbrun = true;
-    void run()
-    {
-        mbAttach = mpa->attach();
-        mbrun = false;
-    }
-};
+//class AttachThread : public QThread
+//{
+//  public:
+//    AttachThread(QSharedMemory * pa,bool & bAttach)
+//    {
+//       mbAttach = bAttach;
+//       mpa = pa;
+//       mbrun = true;
+//    }
+//    QSharedMemory * mpa;
+//    bool mbAttach = false;
+//    bool mbrun = true;
+//    void run()
+//    {
+//        mbAttach = mpa->attach();
+//        mbrun = false;
+//    }
+//};
 
 void procsm::threadAttachMon()
 {
@@ -416,31 +416,17 @@ void procsm::recreateasm(int nbufsize)
     char strout[300];
     snprintf(strout,300,"new asm name is %s,buffer size is %d ",mASM_State.mstrshmname,mnBufSize);
     ivstdcolorout(strout,iv::STDCOLOR_GREEN);
+
+
+
+
 //    qDebug("new asm name is %s,buffer size is %d ",mASM_State.mstrshmname,mnBufSize);
     mpASM = new QSharedMemory(mASM_State.mstrshmname);
 
-    bool bAttach = false;
-    AttachThread AT(mpASM,bAttach);
-    AT.start();
-    QTime xTime;
-    xTime.start();
-    while(xTime.elapsed()<100)
-    {
-        if(AT.mbrun == false)
-        {
-            bAttach = AT.mbAttach;
-            break;
-        }
-    }
-    //       qDebug("time is %d",xTime.elapsed());
-    if(xTime.elapsed()>= 1000)
-    {
-        qDebug("in 1000ms Attach fail.terminate it .");
-        AT.terminate();
-        bAttach = false;
-    }
+    mpASM->attach();
+
+    bool bAttach = mpASM->isAttached();
 
-    //       if(!mpASM->attach())
     if(!bAttach)
     {
 
@@ -451,9 +437,6 @@ void procsm::recreateasm(int nbufsize)
         mphead = (procsm_head *)(p+sizeof(procsm_info));
         mpinfo->mCap = mnMaxPacCount;
         mpinfo->mnBufSize = mnBufSize;
-//        mpinfo->mFirst = nfirst;
-//        mpinfo->mNext = nnext;
-//        mpinfo->mLock = 0;
     }
 
 
@@ -468,8 +451,6 @@ void procsm::recreateasm(int nbufsize)
         mphead = (procsm_head *)(p+sizeof(procsm_info));
         mnMaxPacCount = mpinfo->mCap;
         mnBufSize = mpinfo->mnBufSize;
-        //        qDebug("attach successful");
- //       mstrtem = new char[mnBufSize];
     }
     else
     {
@@ -533,8 +514,8 @@ bool procsm::AttachMem()
         }
         else
         {
-            std::cout<<" mode: "<<mnMode<<" asm name: "<<mASM_State.mstrshmname<<std::endl;
-            ivstdcolorout(" AttachMem: ASM Attach Fail. ",iv::STDCOLOR_BOLDYELLOW);
+//            std::cout<<" mode: "<<mnMode<<" asm name: "<<mASM_State.mstrshmname<<std::endl;
+//            ivstdcolorout(" AttachMem: ASM Attach Fail. ",iv::STDCOLOR_BOLDYELLOW);
             if(mnMode == ModeWrite)
             {
 
@@ -561,9 +542,12 @@ bool procsm::AttachMem()
     }
     else
     {
-        char strout[256];
- //       snprintf(strout,256,"%s  Attach ASMPtr Fail.",mASM_State.mstr);
-        ivstdcolorout(" AttachMem: Attach Fail. ",iv::STDCOLOR_BOLDYELLOW);
+        if(mnMode == ModeWrite)
+        {
+            char strout[256];
+            snprintf(strout,256,"%s  Attach ASMPtr Fail.",mstrsmname);
+            ivstdcolorout(" AttachMem: Attach Fail. ",iv::STDCOLOR_BOLDYELLOW);
+        }
         return false;
     }
 

+ 12 - 5
src/test/testmodule1/main.cpp

@@ -12,14 +12,21 @@ void threadsend(void * pa)
     while(1)
     {
         char str[1000];
+        int64_t xTime = std::chrono::system_clock::now().time_since_epoch().count();
+        memcpy(str,&xTime,8);
         iv::modulecomm::ModuleSendMsg(pa,str,30);
-        std::this_thread::sleep_for(std::chrono::milliseconds(100));
+        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
     }
 }
 
 void ListenPointCloud(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
 {
-    qDebug("%lld recv %s ",QDateTime::currentMSecsSinceEpoch(),strmemname);
+    int64_t xTime = std::chrono::system_clock::now().time_since_epoch().count();
+    int64_t xSendTime;
+    memcpy(&xSendTime,strdata,8);
+    double flatency = static_cast<double >((xTime - xSendTime));
+    flatency = flatency/1000000.0;
+    qDebug("%lld recv %s  latency: %10.6f ",QDateTime::currentMSecsSinceEpoch(),strmemname,flatency);
 }
 
 int main(int argc, char *argv[])
@@ -30,9 +37,9 @@ int main(int argc, char *argv[])
     void * pb1,*pb2,*pb3,*pb4,*pb5;
 
     pa1 = iv::modulecomm::RegisterSend("b1",100,1);
-    pa2 = iv::modulecomm::RegisterSend("b2",1000,1);
-    pa3 = iv::modulecomm::RegisterSend("b3",10000,1);
-    pa4 = iv::modulecomm::RegisterSend("b4",100000,1);
+    pa2 = iv::modulecomm::RegisterSend("b2",100,1);
+    pa3 = iv::modulecomm::RegisterSend("b3",100,1);
+    pa4 = iv::modulecomm::RegisterSend("b4",100,1);
     pa5 = iv::modulecomm::RegisterSend("b6",10000000,1);
 
     pb1 = iv::modulecomm::RegisterRecv("a1",ListenPointCloud);

+ 12 - 5
src/test/testmodule2/main.cpp

@@ -10,14 +10,21 @@ void threadsend(void * pa)
     while(1)
     {
         char str[1000];
+        int64_t xTime = std::chrono::system_clock::now().time_since_epoch().count();
+        memcpy(str,&xTime,8);
         iv::modulecomm::ModuleSendMsg(pa,str,30);
-        std::this_thread::sleep_for(std::chrono::milliseconds(100));
+        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
     }
 }
 
 void ListenPointCloud(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
 {
-      qDebug("%lld recv %s ",QDateTime::currentMSecsSinceEpoch(),strmemname);
+    int64_t xTime = std::chrono::system_clock::now().time_since_epoch().count();
+    int64_t xSendTime;
+    memcpy(&xSendTime,strdata,8);
+    double flatency = static_cast<double >((xTime - xSendTime));
+    flatency = flatency/1000000.0;
+    qDebug("%lld recv %s  latency: %10.6f ",QDateTime::currentMSecsSinceEpoch(),strmemname,flatency);
 }
 
 int main(int argc, char *argv[])
@@ -28,9 +35,9 @@ int main(int argc, char *argv[])
     void * pb1,*pb2,*pb3,*pb4,*pb5;
 
     pa1 = iv::modulecomm::RegisterSend("a1",100,1);
-    pa2 = iv::modulecomm::RegisterSend("a2",1000,1);
-    pa3 = iv::modulecomm::RegisterSend("a3",10000,1);
-    pa4 = iv::modulecomm::RegisterSend("a4",100000,1);
+    pa2 = iv::modulecomm::RegisterSend("a2",100,1);
+    pa3 = iv::modulecomm::RegisterSend("a3",100,1);
+    pa4 = iv::modulecomm::RegisterSend("a4",100,1);
     pa5 = iv::modulecomm::RegisterSend("a6",1000000,1);
 
     pb1 = iv::modulecomm::RegisterRecv("b1",ListenPointCloud);