Bläddra i källkod

change modulecomm, change asm name use std::chrono get nanoseconds. add ivservice for service call.

yuchuli 3 år sedan
förälder
incheckning
226a7ddec2

+ 73 - 0
src/common/ivservice/.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
+

+ 41 - 0
src/common/ivservice/ivservice.cpp

@@ -0,0 +1,41 @@
+#include "ivservice.h"
+
+#include "ivservice_impl.h"
+
+namespace iv {
+namespace service {
+
+
+Server::Server(const char *strservicename, ServiceProc xProc)
+{
+    Server_impl * pserver = new Server_impl(strservicename,xProc);
+    mpimpl = (void *)pserver;
+}
+
+Server::~Server()
+{
+    Server_impl * pserver = (Server_impl *)mpimpl;
+    delete pserver;
+}
+
+
+Client::Client(const char *strservicename)
+{
+    Client_impl * pclient = new Client_impl(strservicename);
+    mpimpl = pclient;
+}
+
+Client::~Client()
+{
+    Client_impl * pclient = (Client_impl * )mpimpl;
+    delete pclient;
+}
+
+Client::ServiceResRes  Client::SendRequest(std::shared_ptr<char> pstr_request,const int nreqsize, std::shared_ptr<char> &pstr_response, int & nressize,const int timeout)
+{
+    Client_impl * pclient = (Client_impl * )mpimpl;
+    return pclient->SendRequest(pstr_request,nreqsize,pstr_response,nressize,timeout);
+}
+
+}
+}

+ 64 - 0
src/common/ivservice/ivservice.h

@@ -0,0 +1,64 @@
+#ifndef IVSERVICE_H
+#define IVSERVICE_H
+
+
+#include <memory>
+
+#include <QtCore/qglobal.h>
+
+#if defined(IVSERVICE_LIBRARY)
+#  define IVSERVICE_EXPORT Q_DECL_EXPORT
+#else
+#  define IVSERVICE_EXPORT Q_DECL_IMPORT
+#endif
+
+
+typedef void (*ServiceProc)(std::shared_ptr<char> pstr_request,const int nreqsize, std::shared_ptr<char> & pstr_response,int & nsize);
+
+
+namespace iv {
+
+namespace service {
+class IVSERVICE_EXPORT Server
+{
+public:
+    Server(const char * strservicename,ServiceProc xProc);
+    ~Server();
+
+private:
+    void * mpimpl;
+};
+
+class IVSERVICE_EXPORT Client
+{
+public:
+    Client(const char * strservicename);
+    ~Client();
+
+    enum  ServiceResRes
+    {
+        NO_RES = 1,
+        HAVE_RES = 2
+    };
+
+public:
+    /**
+     * @brief SendRequest 获得服务结果
+     * @param pstr_request  请求的数据
+     * @param pstr_response  返回的结果
+     * @param timeout  超时时间
+     * @return  如果成功,返回  HAVE_RES,如果超时,返回NO_RES
+     */
+    ServiceResRes SendRequest(std::shared_ptr<char> pstr_request,const int nreqsize,std::shared_ptr<char> & pstr_response,
+                              int & nressize,const int timeout = 100);
+
+private:
+    void * mpimpl;
+};
+
+}
+}
+
+
+
+#endif // IVSERVICE_H

+ 35 - 0
src/common/ivservice/ivservice.pro

@@ -0,0 +1,35 @@
+QT -= gui
+
+TEMPLATE = lib
+DEFINES += IVSERVICE_LIBRARY
+
+CONFIG += c++11
+
+# The following define makes your compiler emit warnings if you use
+# any Qt feature that has 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 it uses 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 += \
+    ivservice.cpp \
+    ivservice_impl.cpp
+
+HEADERS += \
+    ivservice.h \
+    ivservice_impl.h
+
+# Default rules for deployment.
+unix {
+    target.path = /usr/lib
+}
+!isEmpty(target.path): INSTALLS += target
+
+
+INCLUDEPATH += $$PWD/../../../include/
+LIBS += -L$$PWD/../../../bin/  -lmodulecomm

+ 125 - 0
src/common/ivservice/ivservice_impl.cpp

@@ -0,0 +1,125 @@
+#include "ivservice_impl.h"
+
+#include "modulecomm.h"
+#include <QDateTime>
+#include <iostream>
+#include <chrono>
+
+namespace iv {
+namespace service {
+
+
+Server_impl::Server_impl(const char *strservicename, ServiceProc xProc)
+{
+    char strreqmsgname[256];
+    char strresmsgname[256];
+    snprintf(strreqmsgname,256,"%s_req",strservicename);
+    snprintf(strresmsgname,256,"%s_res",strservicename);
+    ModuleFun funreq=std::bind(&Server_impl::UpdateReq,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
+    mpares = iv::modulecomm::RegisterSend(strresmsgname,1000,3);
+    mpareq = iv::modulecomm::RegisterRecvPlus(strreqmsgname,funreq);
+
+    mpProc = xProc;
+}
+
+Server_impl::~Server_impl()
+{
+    iv::modulecomm::Unregister(mpareq);
+    iv::modulecomm::Unregister(mpares);
+}
+
+void Server_impl::UpdateReq(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
+{
+    (void)index;
+    (void )dt;
+    (void)strmemname;
+    if(nSize<=sizeof(qint64))return;
+    std::shared_ptr<char> pstr_req = std::shared_ptr<char>(new char[nSize-sizeof(qint64)]);
+    std::shared_ptr<char> pstr_res;
+    memcpy(pstr_req.get(),strdata+sizeof(qint64),nSize-sizeof(qint64));
+    int ndatasize = 0;
+    (*mpProc)(pstr_req,nSize-sizeof(qint64),pstr_res,ndatasize);
+    std::shared_ptr<char> pstr_send = std::shared_ptr<char>(new char[ndatasize+sizeof(qint64)]);
+    memcpy(pstr_send.get(),strdata,sizeof(qint64));
+    memcpy(pstr_send.get()+sizeof(qint64),pstr_res.get(),ndatasize);
+    iv::modulecomm::ModuleSendMsg(mpares,pstr_send.get(),ndatasize+sizeof(qint64));
+}
+
+Client_impl::Client_impl(const char *strservicename)
+{
+    char strreqmsgname[256];
+    char strresmsgname[256];
+    snprintf(strreqmsgname,256,"%s_req",strservicename);
+    snprintf(strresmsgname,256,"%s_res",strservicename);
+    ModuleFun funres=std::bind(&Client_impl::UpdateRes,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
+    mpares = iv::modulecomm::RegisterRecvPlus(strresmsgname,funres);
+    mpareq = iv::modulecomm::RegisterSend(strreqmsgname,1000,3);
+}
+
+Client::ServiceResRes Client_impl::SendRequest(std::shared_ptr<char> pstr_request,const int nreqsize, std::shared_ptr<char> &pstr_response,int & nressize, const int timeout)
+{
+    mbNewRes = false;
+    mreqid = std::chrono::system_clock::now().time_since_epoch().count();
+    std::shared_ptr<char> pstr_send = std::shared_ptr<char>(new char[sizeof(qint64)+nreqsize]);
+    memcpy(pstr_send.get(),&mreqid,sizeof(qint64));
+    memcpy(pstr_send.get()+sizeof(qint64),pstr_request.get(),nreqsize);
+    iv::modulecomm::ModuleSendMsg(mpareq,pstr_send.get(),nreqsize+sizeof(qint64));
+//    iv::modulecomm::ModuleSendMsg(mpareq,pstr_request.get(),nreqsize);
+    mWaitMutex.lock();
+    mwc.wait(&mWaitMutex,timeout);
+    mWaitMutex.unlock();
+    if(mbNewRes)
+    {
+        mMutexRes.lock();
+        pstr_response = mpstr_res;
+        nressize = mressize;
+        mbNewRes = false;
+        mMutexRes.unlock();
+        return Client::HAVE_RES;
+    }
+    else
+    {
+        return Client::NO_RES;
+    }
+    return Client::NO_RES;
+}
+
+Client_impl::~Client_impl()
+{
+    iv::modulecomm::Unregister(mpareq);
+    iv::modulecomm::Unregister(mpares);
+}
+
+void Client_impl::UpdateRes(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
+{
+    (void)index;
+    (void )dt;
+    (void)strmemname;
+    int nqint64size = sizeof(qint64);
+    if(nSize<=nqint64size)return;
+    qint64 * preqid = (qint64 *)strdata;
+    if(*preqid != mreqid)
+    {
+        std::cout<<"not this reqid value."<<std::endl;
+        return;
+    }
+
+//    return;
+
+    std::shared_ptr<char> pstr_res = std::shared_ptr<char>(new char[nSize-nqint64size]);
+    memcpy(pstr_res.get(),strdata+nqint64size,nSize-nqint64size);
+
+    mMutexRes.lock();
+    mpstr_res = pstr_res;
+    mressize = nSize-nqint64size;
+    mbNewRes = true;
+    mMutexRes.unlock();
+    mwc.wakeAll();
+}
+
+}
+}
+
+
+
+

+ 73 - 0
src/common/ivservice/ivservice_impl.h

@@ -0,0 +1,73 @@
+#ifndef IVSERVICE_IMPL_H
+#define IVSERVICE_IMPL_H
+
+#include "ivservice.h"
+
+#include <QMutex>
+#include <QWaitCondition>
+#include <QDateTime>
+
+namespace iv {
+
+namespace service {
+class  Server_impl
+{
+public:
+    Server_impl(const char * strservicename,ServiceProc xProc);
+    ~Server_impl();
+
+private:
+    void * mpareq;
+    void * mpares;
+    ServiceProc mpProc;
+
+private:
+    void UpdateReq(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname);
+
+};
+
+class  Client_impl
+{
+public:
+    Client_impl(const char * strservicename);
+    ~Client_impl();
+
+
+public:
+    /**
+     * @brief SendRequest 获得服务结果
+     * @param pstr_request  请求的数据
+     * @param pstr_response  返回的结果
+     * @param timeout  超时时间
+     * @return  如果成功,返回  HAVE_RES,如果超时,返回NO_RES
+     */
+     Client::ServiceResRes SendRequest(std::shared_ptr<char> pstr_request,const int nreqsize,std::shared_ptr<char> & pstr_response,
+                              int & nressize,const int timeout = 100);
+
+
+private:
+
+     void UpdateRes(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname);
+
+
+private:
+     std::shared_ptr<char> mpstr_res;
+     int mressize = 0;
+     bool mbNewRes = false;
+     QMutex mMutexRes;
+
+     QMutex mWaitMutex;
+     QWaitCondition mwc;
+
+private:
+     void * mpareq;
+     void * mpares;
+     qint64 mreqid;
+
+
+
+};
+
+}
+}
+#endif // IVSERVICE_IMPL_H

+ 111 - 0
src/common/ivservice/main.cpp

@@ -0,0 +1,111 @@
+#include <QCoreApplication>
+
+#include <thread>
+#include <chrono>
+#include <iostream>
+
+#include <QDateTime>
+
+#include "ivservice.h"
+
+void ProcReq(std::shared_ptr<char> pstr_req,const int nreqsize,std::shared_ptr<char> & pstr_res,int & nressize)
+{
+   pstr_res = std::shared_ptr<char>(new char[100]);
+   nressize = 100;
+}
+
+
+void threadclientai(int ai)
+{
+    iv::service::Client xclient("hellob");
+
+//    std::this_thread::sleep_for(std::chrono::milliseconds(3));
+//    std::cout<<"Send Time : "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
+    std::shared_ptr<char> pstr_req = std::shared_ptr<char>(new char[1000000]);
+    std::shared_ptr<char> pstr_res;
+    int nressize = 0;
+    int nsendsize = 100;
+    while(nsendsize < 1000000)
+    {
+        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
+
+        nsendsize = nsendsize * 1;
+        if(nsendsize >= 1000000)break;
+        std::cout<<ai<<" Send Time : "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
+        if(iv::service::Client::HAVE_RES ==  xclient.SendRequest(pstr_req,nsendsize,pstr_res,nressize))
+        {
+            std::cout<<ai<<" Res Time: "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
+            std::cout<<ai<<"   nres: "<<nressize<<std::endl;
+        }
+        else
+        {
+            std::cout<<ai<<" Fail Get Result."<<std::endl;
+        }
+    }
+}
+void threadclient()
+{
+    iv::service::Client xclient("hellob");
+
+//    std::this_thread::sleep_for(std::chrono::milliseconds(3));
+    std::cout<<"Send Time : "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
+    std::shared_ptr<char> pstr_req = std::shared_ptr<char>(new char[1000000]);
+    std::shared_ptr<char> pstr_res;
+    int nressize = 0;
+    if(iv::service::Client::HAVE_RES ==  xclient.SendRequest(pstr_req,100,pstr_res,nressize))
+    {
+        std::cout<<"Res Time: "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
+        std::cout<<"   nres: "<<nressize<<std::endl;
+    }
+    else
+    {
+        std::cout<<" Fail Get Result."<<std::endl;
+    }
+    return ;
+    int nsendsize = 100;
+    while(nsendsize < 1000000)
+    {
+    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
+
+    nsendsize = nsendsize * 1;
+    if(nsendsize >= 1000000)break;
+    std::cout<<"Send Time : "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
+    if(iv::service::Client::HAVE_RES ==  xclient.SendRequest(pstr_req,nsendsize,pstr_res,nressize))
+    {
+        std::cout<<"Res Time: "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
+        std::cout<<"   nres: "<<nressize<<std::endl;
+    }
+    else
+    {
+        std::cout<<" Fail Get Result."<<std::endl;
+    }
+    }
+}
+
+
+int main(int argc, char *argv[])
+{
+    QCoreApplication a(argc, argv);
+
+
+
+    std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> tp =std::chrono::system_clock::now();
+
+    qint64 xtime1 = std::chrono::system_clock::now().time_since_epoch().count();
+    qint64 xtime2 = std::chrono::system_clock::now().time_since_epoch().count();
+    qint64 xtd = xtime2 - xtime1;
+    iv::service::Server xserver("hellob",ProcReq);
+    std::this_thread::sleep_for(std::chrono::milliseconds(300));
+    std::thread * pthread_a = new std::thread(threadclientai,1);
+//    std::this_thread::sleep_for(std::chrono::milliseconds(300));
+    std::thread * pthread_b = new std::thread(threadclientai,2);
+//    std::thread * pthread = new std::thread(threadclient);
+//    int i;
+//    for(i=0;i<100;i++)
+//    {
+//        std::this_thread::sleep_for(std::chrono::milliseconds(3000));
+//        pthread = new std::thread(threadclient);
+
+//    }
+    return a.exec();
+}

+ 45 - 0
src/common/ivservice/testivservice.pro

@@ -0,0 +1,45 @@
+QT -= gui
+
+QT += dbus
+QT       += xml
+
+
+
+CONFIG += c++11 console
+CONFIG -= app_bundle
+
+# The following define makes your compiler emit warnings if you use
+# any Qt feature that has 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 it uses 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 += \
+        ivservice.cpp \
+        ivservice_impl.cpp \
+        main.cpp 
+        
+# Default rules for deployment.
+qnx: target.path = /tmp/$${TARGET}/bin
+else: unix:!android: target.path = /opt/$${TARGET}/bin
+!isEmpty(target.path): INSTALLS += target
+
+HEADERS += \
+    ivservice.h \
+    ivservice_impl.h
+
+
+INCLUDEPATH += $$PWD/../../../include/
+LIBS += -L$$PWD/../../../bin/  -lmodulecomm
+
+LIBS +=  -lfastcdr -lfastrtps -ltinyxml2
+
+
+
+
+

+ 80 - 16
src/common/modulecomm/shm/procsm.cpp

@@ -132,6 +132,8 @@ procsm::procsm(const char * strsmname,const unsigned int nBufSize,const unsigned
 
     char strasmname[256];
 
+    bool bSMExit = false;
+
     if(nMode == ModeWrite)
     {
         bool bres;
@@ -159,6 +161,7 @@ procsm::procsm(const char * strsmname,const unsigned int nBufSize,const unsigned
             bres = mpASMPtr->attach();
             if(bres == true)
             {
+                bSMExit = true;
                 std::cout<<"\033[1m\033[32m"<<mstrsmname<<" exist. attach successfully."<<"\033[0m"<<std::endl;
             }
             else
@@ -173,10 +176,23 @@ procsm::procsm(const char * strsmname,const unsigned int nBufSize,const unsigned
             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);
+        if(bSMExit == false)
+        {
+            qint64 uptime = std::chrono::system_clock::now().time_since_epoch().count();
+            snprintf(strasmname,256,"%s_%lld",strsmname,uptime);
+            mpASMPtr->lock();
+            pasm->mnshmsize = sizeof(procsm_info)+nMaxPacCount*sizeof(procsm_head) + nBufSize;
+            pasm->mnUpdateTime = uptime;
+            strncpy(pasm->mstrshmname,strasmname,256);
+            mpASMPtr->unlock();
+        }
+        else
+        {
+            mpASMPtr->lock();
+            pasm->mnUpdateTime = std::chrono::system_clock::now().time_since_epoch().count();
+            strncpy(strasmname,pasm->mstrshmname,256);
+            mpASMPtr->unlock();
+        }
         mASM_State = *pasm;
     }
     else
@@ -203,7 +219,7 @@ procsm::procsm(const char * strsmname,const unsigned int nBufSize,const unsigned
         mmsg<<1;
 #endif
 
-        bool bAttach = mpASM->attach();
+        bool bAttach= true;
 //        AttachThread AT(mpASM,bAttach);
 //        AT.start();
 //        QTime xTime;
@@ -225,16 +241,40 @@ procsm::procsm(const char * strsmname,const unsigned int nBufSize,const unsigned
 //        }
 
  //       if(!mpASM->attach())
-        if(!bAttach)
+
+        bool bares = mpASM->create(sizeof(procsm_info)+nMaxPacCount*sizeof(procsm_head) + nBufSize);
+        if(bares == false)   //Exist.
         {
+            bAttach = mpASM->attach();
+
+            if(bAttach)
+            {
+                char * p = (char *)mpASM->data();
+                if(p == NULL)
+                {
+                    qDebug("Create SharedMemory Fail.");
+                    return;
+                }
+                mpinfo = (procsm_info *)p;
+                mphead = (procsm_head *)(p+sizeof(procsm_info));
+
 
-            mpASM->create(sizeof(procsm_info)+nMaxPacCount*sizeof(procsm_head) + nBufSize);
+            }
+            else
+            {
+                std::cout<<" Exist,But Fail Attach."<<std::endl;
+            }
+
+        }
+        else
+        {
             char * p = (char *)mpASM->data();
             if(p == NULL)
             {
                 qDebug("Create SharedMemory Fail.");
                 return;
             }
+            mpASM->lock();
             mpinfo = (procsm_info *)p;
             mphead = (procsm_head *)(p+sizeof(procsm_info));
             mpinfo->mCap = nMaxPacCount;
@@ -242,11 +282,10 @@ procsm::procsm(const char * strsmname,const unsigned int nBufSize,const unsigned
             mpinfo->mFirst = 0;
             mpinfo->mNext = 0;
             mpinfo->mLock = 0;
+            mpASM->unlock();
         }
 
 
-
-
         if(mpASM->isAttached())
         {
 
@@ -254,8 +293,10 @@ procsm::procsm(const char * strsmname,const unsigned int nBufSize,const unsigned
             char * p = (char *)mpASM->data();
             mpinfo = (procsm_info *)p;
             mphead = (procsm_head *)(p+sizeof(procsm_info));
+            mpASM->lock();
             mnMaxPacCount = mpinfo->mCap;
             mnBufSize = mpinfo->mnBufSize;
+            mpASM->unlock();
     //        qDebug("attach successful");
             mstrtem = new char[mnBufSize];
 
@@ -321,9 +362,9 @@ void procsm::recreateasm(int nbufsize)
  //   mnBufSize = nbufsize+100;
     char strasmname[256];
     ASM_PTR * pasm = (ASM_PTR *)mpASMPtr->data();
-    snprintf(strasmname,256,"%s_%lld",mstrsmname,QDateTime::currentMSecsSinceEpoch());
+    snprintf(strasmname,256,"%s_%lld",mstrsmname,std::chrono::system_clock::now().time_since_epoch().count());//QDateTime::currentMSecsSinceEpoch());
     pasm->mnshmsize = sizeof(procsm_info)+mnMaxPacCount*sizeof(procsm_head) + mnBufSize;
-    pasm->mnUpdateTime = QDateTime::currentMSecsSinceEpoch();
+    pasm->mnUpdateTime = std::chrono::system_clock::now().time_since_epoch().count();
     strncpy(pasm->mstrshmname,strasmname,256);
     mASM_State = *pasm;
 
@@ -429,7 +470,7 @@ bool procsm::AttachMem()
     if(mpASMPtr->isAttached())
     {
         ASM_PTR * pasmptr = (ASM_PTR *)(mpASMPtr->data());
-        mASM_State = * pasmptr;
+        mASM_State = *pasmptr;
 
         if(mpASM != 0)
         {
@@ -531,21 +572,22 @@ int procsm::MoveMem(const unsigned int nSize)
 }
 
 
-void procsm::checkasm()
+int  procsm::checkasm()
 {
 
     mpASMPtr->lock();
     ASM_PTR * pASM_PTR = (ASM_PTR * )mpASMPtr->data();
-    if(pASM_PTR->mnUpdateTime == mASM_State.mnUpdateTime)
+    if((pASM_PTR->mnUpdateTime == mASM_State.mnUpdateTime) && (mbAttach == true) )
     {
 
         mpASMPtr->unlock();
-        return;
+        return 0;
     }
     qDebug("reattch mem.");
     mbAttach = false;
     AttachMem();
     mpASMPtr->unlock();
+    return 1;
 }
 
 int procsm::writemsg(const char *str, const unsigned int nSize)
@@ -653,6 +695,7 @@ WRITEMSG:
 #ifdef USEDBUS
     QDBusConnection::sessionBus().send(mmsg);
 #endif
+//    std::cout<<"write msg."<<std::endl;
     return 0;
 }
 
@@ -674,7 +717,11 @@ unsigned int procsm::getcurrentnext()
 //if return > 0 readdata
 int procsm::readmsg(unsigned int index, char *str, unsigned int nMaxSize,unsigned int * nRead,QDateTime * pdt)
 {
-    checkasm();
+    int ncheck = checkasm();
+//    if(ncheck == 1)
+//    {
+//        return -100;
+//    }
     if(mbAttach == false)
     {
         std::cout<<mstrsmname<<"ShareMemory Attach fail."<<std::endl;
@@ -683,6 +730,22 @@ int procsm::readmsg(unsigned int index, char *str, unsigned int nMaxSize,unsigne
     int nRtn = 0;
     mpASM->lock();
 
+//    if check is 1, maybe sharemem change. Read New Message.
+    if(ncheck == 1)
+    {
+
+        if(mpinfo->mFirst < mpinfo->mNext)
+        {
+            std::cout<<" ShareMemroy Change. Read the newest."<<std::endl;
+            mpASM->unlock();
+            return -100;
+        }
+        else
+        {
+            mpASM->unlock();
+            return -101;
+        }
+    }
     if((index< mpinfo->mFirst)||(index > mpinfo->mNext))
     {
         nRtn = -2;
@@ -729,6 +792,7 @@ int procsm::readmsg(unsigned int index, char *str, unsigned int nMaxSize,unsigne
         }
     }
     mpASM->unlock();
+
     return nRtn;
 }
 

+ 1 - 1
src/common/modulecomm/shm/procsm.h

@@ -120,7 +120,7 @@ private:
 #endif
 
 private:
-    void checkasm();
+    int checkasm();
     void recreateasm(int nbufsize);
 
 private:

+ 5 - 1
src/common/modulecomm/shm/procsm_if.cpp

@@ -141,9 +141,13 @@ void procsm_if_readthread::run()
             }
             else
             {
-                if(nRtn == -2)
+                if((nRtn == -2) ||(nRtn == -100)||(nRtn == -101))
                 {
                    index = mpPSM->getcurrentnext();
+                   if(nRtn == -100)  //Because New ShareMemory, read this.
+                   {
+                       if(index > 0)index = index -1;
+                   }
                 }
                 else
                 {