Browse Source

change common/modulecomm, fix a bug in this program.

yuchuli 3 years ago
parent
commit
bafc60241b

+ 35 - 29
src/common/modulecomm/shm/procsm.cpp

@@ -44,18 +44,19 @@ procsm::procsm(const char * strsmname,const unsigned int nBufSize,const unsigned
         if(bres == false)
         {
             mpASMPtr->create(sizeof(ASM_PTR));
-            ASM_PTR * pasm = (ASM_PTR *)mpASMPtr->data();
-            snprintf(strasmname,256,"%s_%lld",strsmname,QDateTime::currentMSecsSinceEpoch());
-            pasm->mnshmsize = sizeof(procsm_info)+nMaxPacCount*sizeof(procsm_head) + nBufSize;
-            pasm->mnUpdateTime = QDateTime::currentMSecsSinceEpoch();
-            strncpy(pasm->mstrshmname,strasmname,256);
-            mASM_State = *pasm;
+
         }
-        else
+        ASM_PTR * pasm = (ASM_PTR *)mpASMPtr->data();
+        if(pasm == NULL)
         {
-            ASM_PTR * pasm = (ASM_PTR *)mpASMPtr->data();
-            mASM_State = *pasm;
+            qDebug("ASM_PTR is NULL.");
+            return;
         }
+        snprintf(strasmname,256,"%s_%lld",strsmname,QDateTime::currentMSecsSinceEpoch());
+        pasm->mnshmsize = sizeof(procsm_info)+nMaxPacCount*sizeof(procsm_head) + nBufSize;
+        pasm->mnUpdateTime = QDateTime::currentMSecsSinceEpoch();
+        strncpy(pasm->mstrshmname,strasmname,256);
+        mASM_State = *pasm;
     }
     else
     {
@@ -76,26 +77,26 @@ procsm::procsm(const char * strsmname,const unsigned int nBufSize,const unsigned
         mmsg<<1;
 #endif
 
-        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;
-        }
+        bool bAttach = mpASM->attach();
+//        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;
+//        }
 
  //       if(!mpASM->attach())
         if(!bAttach)
@@ -103,6 +104,11 @@ procsm::procsm(const char * strsmname,const unsigned int nBufSize,const unsigned
 
             mpASM->create(sizeof(procsm_info)+nMaxPacCount*sizeof(procsm_head) + nBufSize);
             char * p = (char *)mpASM->data();
+            if(p == NULL)
+            {
+                qDebug("Create SharedMemory Fail.");
+                return;
+            }
             mpinfo = (procsm_info *)p;
             mphead = (procsm_head *)(p+sizeof(procsm_info));
             mpinfo->mCap = nMaxPacCount;

+ 73 - 0
src/test/testmodulecommextsend/.gitignore

@@ -0,0 +1,73 @@
+# This file is used to ignore files which are generated
+# ----------------------------------------------------------------------------
+
+*~
+*.autosave
+*.a
+*.core
+*.moc
+*.o
+*.obj
+*.orig
+*.rej
+*.so
+*.so.*
+*_pch.h.cpp
+*_resource.rc
+*.qm
+.#*
+*.*#
+core
+!core/
+tags
+.DS_Store
+.directory
+*.debug
+Makefile*
+*.prl
+*.app
+moc_*.cpp
+ui_*.h
+qrc_*.cpp
+Thumbs.db
+*.res
+*.rc
+/.qmake.cache
+/.qmake.stash
+
+# qtcreator generated files
+*.pro.user*
+
+# xemacs temporary files
+*.flc
+
+# Vim temporary files
+.*.swp
+
+# Visual Studio generated files
+*.ib_pdb_index
+*.idb
+*.ilk
+*.pdb
+*.sln
+*.suo
+*.vcproj
+*vcproj.*.*.user
+*.ncb
+*.sdf
+*.opensdf
+*.vcxproj
+*vcxproj.*
+
+# MinGW generated files
+*.Debug
+*.Release
+
+# Python byte code
+*.pyc
+
+# Binaries
+# --------
+*.dll
+*.exe
+

+ 20 - 0
src/test/testmodulecommextsend/main.cpp

@@ -0,0 +1,20 @@
+#include <QCoreApplication>
+
+#include "testinteriorrecv.h"
+#include "testinteriorsend.h"
+
+#include "testshmsend.h"
+
+int main(int argc, char *argv[])
+{
+    QCoreApplication a(argc, argv);
+
+    testinteriorrecv xinterrecv;
+    testinteriorsend xintersend;
+    xintersend.start();
+
+    testshmsend xshmsend;
+    xshmsend.start();
+
+    return a.exec();
+}

+ 16 - 0
src/test/testmodulecommextsend/testinteriorrecv.cpp

@@ -0,0 +1,16 @@
+#include "testinteriorrecv.h"
+
+testinteriorrecv::testinteriorrecv()
+{
+
+    mpa = new iv::modulecommext::modulecommmsg<iv::testmodulecommext>();
+    ModuleExtFun funext = std::bind(&testinteriorrecv::ListenMsg,this,std::placeholders::_1);
+    mpa->RegisterRecvPlus("test",funext,iv::modulecomm::ModuleComm_INTERIOR);
+}
+
+void testinteriorrecv::ListenMsg(google::protobuf::Message &xmsg)
+{
+    iv::testmodulecommext  xdata;
+    xdata.CopyFrom(xmsg);
+    qDebug("Inter: %lld",xdata.time());
+}

+ 21 - 0
src/test/testmodulecommextsend/testinteriorrecv.h

@@ -0,0 +1,21 @@
+#ifndef TESTINTERIORRECV_H
+#define TESTINTERIORRECV_H
+
+
+#include "modulecommext.h"
+
+#include "testmodulecommext.pb.h"
+
+#include "testinteriorsend.h"
+
+class testinteriorrecv
+{
+public:
+    testinteriorrecv();
+    iv::modulecommext::modulecommmsg<iv::testmodulecommext> * mpa;
+
+private:
+    void ListenMsg(google::protobuf::Message & xmsg);
+};
+
+#endif // TESTINTERIORRECV_H

+ 20 - 0
src/test/testmodulecommextsend/testinteriorsend.cpp

@@ -0,0 +1,20 @@
+#include "testinteriorsend.h"
+
+testinteriorsend::testinteriorsend()
+{
+    mpa = new iv::modulecommext::modulecommmsg<iv::testmodulecommext>();
+
+    mpa->RegisterSend("test",1000,1,iv::modulecomm::ModuleComm_INTERIOR);
+}
+
+void testinteriorsend::run()
+{
+    while (!QThread::isInterruptionRequested()) {
+        iv::testmodulecommext xmsg;
+        xmsg.set_a(1);
+        xmsg.set_b(2);
+        xmsg.set_time(QDateTime::currentMSecsSinceEpoch());
+        mpa->ModuleSendMsg(xmsg);
+        msleep(1000);
+    }
+}

+ 23 - 0
src/test/testmodulecommextsend/testinteriorsend.h

@@ -0,0 +1,23 @@
+#ifndef TESTINTERIORSEND_H
+#define TESTINTERIORSEND_H
+
+#include <QThread>
+
+#include "modulecommext.h"
+
+#include "testmodulecommext.pb.h"
+
+#include "testinteriorsend.h"
+
+class testinteriorsend : public QThread
+{
+public:
+    testinteriorsend();
+
+    iv::modulecommext::modulecommmsg<iv::testmodulecommext> * mpa;
+
+private:
+    void run();
+};
+
+#endif // TESTINTERIORSEND_H

+ 35 - 0
src/test/testmodulecommextsend/testmodulecommextsend.pro

@@ -0,0 +1,35 @@
+QT -= gui
+
+CONFIG += c++11 console
+CONFIG -= app_bundle
+
+# The following define makes your compiler emit warnings if you use
+# any feature of Qt which as been marked deprecated (the exact warnings
+# depend on your compiler). Please consult the documentation of the
+# deprecated API in order to know how to port your code away from it.
+DEFINES += QT_DEPRECATED_WARNINGS
+
+# You can also make your code fail to compile if you use deprecated APIs.
+# In order to do so, uncomment the following line.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+SOURCES += main.cpp \
+    ../../include/msgtype/testmodulecommext.pb.cc \
+    testinteriorsend.cpp \
+    testinteriorrecv.cpp \
+    testshmsend.cpp
+
+!include(../../../include/common.pri ) {
+    error( "Couldn't find the common.pri file!" )
+}
+
+!include(../../../include/ivprotobuf.pri ) {
+    error( "Couldn't find the ivprotobuf.pri file!" )
+}
+
+HEADERS += \
+    ../../include/msgtype/testmodulecommext.pb.h \
+    testinteriorsend.h \
+    testinteriorrecv.h \
+    testshmsend.h

+ 20 - 0
src/test/testmodulecommextsend/testshmsend.cpp

@@ -0,0 +1,20 @@
+#include "testshmsend.h"
+
+testshmsend::testshmsend()
+{
+    mpa = new iv::modulecommext::modulecommmsg<iv::testmodulecommext>();
+
+    mpa->RegisterSend("test3",1000,1,iv::modulecomm::ModuleComm_SHAREMEM);
+}
+
+void testshmsend::run()
+{
+    while (!QThread::isInterruptionRequested()) {
+        iv::testmodulecommext xmsg;
+        xmsg.set_a(1);
+        xmsg.set_b(2);
+        xmsg.set_time(QDateTime::currentMSecsSinceEpoch());
+        mpa->ModuleSendMsg(xmsg);
+        msleep(1000);
+    }
+}

+ 23 - 0
src/test/testmodulecommextsend/testshmsend.h

@@ -0,0 +1,23 @@
+#ifndef TESTSHMSEND_H
+#define TESTSHMSEND_H
+
+#include <QThread>
+
+#include "modulecommext.h"
+
+#include "testmodulecommext.pb.h"
+
+#include "testinteriorsend.h"
+
+class testshmsend : public QThread
+{
+public:
+    testshmsend();
+
+    iv::modulecommext::modulecommmsg<iv::testmodulecommext> * mpa;
+
+private:
+    void run();
+};
+
+#endif // TESTSHMSEND_H