Browse Source

change vtd_rdb. send use thread.

yuchuli 3 years ago
parent
commit
51b20dbade
2 changed files with 76 additions and 24 deletions
  1. 74 24
      src/driver/vtd_rdb/rdbconn.cpp
  2. 2 0
      src/driver/vtd_rdb/rdbconn.h

+ 74 - 24
src/driver/vtd_rdb/rdbconn.cpp

@@ -17,6 +17,50 @@ RDBConn::~RDBConn()
     mpthreadconn->join();
 }
 
+void RDBConn::threadsend(int nsock)
+{
+    while(mbthreadconn)
+    {
+        if(mbEgoUpdate)
+        {
+            Framework::RDBHandler myHandler;
+
+            // start a new message
+            myHandler.initMsg();
+
+            // begin with an SOF identifier
+            myHandler.addPackage( simTime, simFrame, RDB_PKG_ID_START_OF_FRAME );
+
+            // add extended package for the object state
+            RDB_OBJECT_STATE_t *objState = ( RDB_OBJECT_STATE_t* ) myHandler.addPackage( simTime, simFrame, RDB_PKG_ID_OBJECT_STATE, 1, true );
+
+            if ( !objState )
+            {
+                fprintf( stderr, "sendOwnObjectState: could not create object state\n" );
+                return;
+            }
+
+            // copy contents of internally held object state to output structure
+            memcpy( objState, &sOwnObjectState, sizeof( RDB_OBJECT_STATE_t ) );
+
+            fprintf( stderr, "sendOwnObjectState: sending pos x/y/z = %.3lf/%.3lf/%.3lf,\n", objState->base.pos.x, objState->base.pos.y, objState->base.pos.z );
+
+            // terminate with an EOF identifier
+            myHandler.addPackage( simTime, simFrame, RDB_PKG_ID_END_OF_FRAME );
+
+            int retVal = send( nsock, ( const char* ) ( myHandler.getMsg() ), myHandler.getMsgTotalSize(), 0 );
+
+            if ( !retVal )
+                fprintf( stderr, "sendOwnObjectState: could not send object state\n" );
+            mbEgoUpdate = false;
+        }
+        else
+        {
+            std::this_thread::sleep_for(std::chrono::milliseconds(1));
+        }
+    }
+}
+
 void RDBConn::threadconn(char *strserip, int nport)
 {
 
@@ -60,6 +104,7 @@ void RDBConn::threadconn(char *strserip, int nport)
     char* szBuffer = new char[DEFAULT_BUFFER];  // allocate on heap
 
 
+    std::thread * pthreadsend = NULL;
 
     while(mbthreadconn)
     {
@@ -79,43 +124,48 @@ void RDBConn::threadconn(char *strserip, int nport)
             }
         }
 
+        if(pthreadsend == NULL)
+        {
+            pthreadsend = new std::thread(&RDBConn::threadsend,this,sClient);
+        }
+
         ret = recv( sClient, szBuffer, DEFAULT_BUFFER, 0 );
 
         std::cout<<" ret: "<<ret<<std::endl;
 
-        if(mbEgoUpdate)
-        {
-            Framework::RDBHandler myHandler;
+//        if(mbEgoUpdate)
+//        {
+//            Framework::RDBHandler myHandler;
 
-            // start a new message
-            myHandler.initMsg();
+//            // start a new message
+//            myHandler.initMsg();
 
-            // begin with an SOF identifier
-            myHandler.addPackage( simTime, simFrame, RDB_PKG_ID_START_OF_FRAME );
+//            // begin with an SOF identifier
+//            myHandler.addPackage( simTime, simFrame, RDB_PKG_ID_START_OF_FRAME );
 
-            // add extended package for the object state
-            RDB_OBJECT_STATE_t *objState = ( RDB_OBJECT_STATE_t* ) myHandler.addPackage( simTime, simFrame, RDB_PKG_ID_OBJECT_STATE, 1, true );
+//            // add extended package for the object state
+//            RDB_OBJECT_STATE_t *objState = ( RDB_OBJECT_STATE_t* ) myHandler.addPackage( simTime, simFrame, RDB_PKG_ID_OBJECT_STATE, 1, true );
 
-            if ( !objState )
-            {
-                fprintf( stderr, "sendOwnObjectState: could not create object state\n" );
-                return;
-            }
+//            if ( !objState )
+//            {
+//                fprintf( stderr, "sendOwnObjectState: could not create object state\n" );
+//                return;
+//            }
 
-            // copy contents of internally held object state to output structure
-            memcpy( objState, &sOwnObjectState, sizeof( RDB_OBJECT_STATE_t ) );
+//            // copy contents of internally held object state to output structure
+//            memcpy( objState, &sOwnObjectState, sizeof( RDB_OBJECT_STATE_t ) );
 
-            fprintf( stderr, "sendOwnObjectState: sending pos x/y/z = %.3lf/%.3lf/%.3lf,\n", objState->base.pos.x, objState->base.pos.y, objState->base.pos.z );
+//            fprintf( stderr, "sendOwnObjectState: sending pos x/y/z = %.3lf/%.3lf/%.3lf,\n", objState->base.pos.x, objState->base.pos.y, objState->base.pos.z );
 
-            // terminate with an EOF identifier
-            myHandler.addPackage( simTime, simFrame, RDB_PKG_ID_END_OF_FRAME );
+//            // terminate with an EOF identifier
+//            myHandler.addPackage( simTime, simFrame, RDB_PKG_ID_END_OF_FRAME );
 
-            int retVal = send( sClient, ( const char* ) ( myHandler.getMsg() ), myHandler.getMsgTotalSize(), 0 );
+//            int retVal = send( sClient, ( const char* ) ( myHandler.getMsg() ), myHandler.getMsgTotalSize(), 0 );
 
-            if ( !retVal )
-                fprintf( stderr, "sendOwnObjectState: could not send object state\n" );
-            mbEgoUpdate = false;
-        }
+//            if ( !retVal )
+//                fprintf( stderr, "sendOwnObjectState: could not send object state\n" );
+//            mbEgoUpdate = false;
+//        }
 
         if ( ret == -1 )
         {

+ 2 - 0
src/driver/vtd_rdb/rdbconn.h

@@ -58,6 +58,8 @@ private:
 private:
     void threadconn(char * strserip,int nport);
 
+    void threadsend(int nsock);
+
 private:
     void parseRDBMessage( RDB_MSG_t* msg);
     void parseRDBMessageEntry( const double & simTime, const unsigned int & simFrame, RDB_MSG_ENTRY_HDR_t* entryHdr );