sunjiacheng 2 years ago
parent
commit
6663643eae
2 changed files with 45 additions and 2 deletions
  1. 44 2
      src/v2x/obuUdpClient/udpreciver.cpp
  2. 1 0
      src/v2x/obuUdpClient/udpreciver.h

+ 44 - 2
src/v2x/obuUdpClient/udpreciver.cpp

@@ -33,8 +33,50 @@ void UdpReciver::readDatagrams()
     {
         m_data.resize(m_udpSocket->pendingDatagramSize());
         m_udpSocket->readDatagram(m_data.data(), m_data.size(), &client_address);
-        QString strclient_address = client_address.toString();
-        deliverInfo(m_data, strclient_address);
+//        QString strclient_address = client_address.toString();
+//        deliverInfo(m_data, strclient_address);
         qDebug() << "receive UDP data: " << m_data;
+        ReceiveDecode(m_data);
+    }
+}
+
+void UdpReciver::ReceiveDecode(QByteArray &data)
+{
+    static int BATH_LENTH = 31;
+    // 防包太大
+    if (data.size() > 2048) {
+        //qDebug() << "size too large";
+        return;
+    }
+    //  ##
+    char first;
+    char second;
+    // 寻找报文开头
+    while (data.size() >= BATH_LENTH) {
+        while (1) {
+            first = data[0];
+            second = data[1];
+            if (first == '#' && second == '#') {
+                break;
+            }
+            // 删除一个字符
+            data.remove(0, 1);
+            if (data.size() < BATH_LENTH)
+                return;
+        }
+        int high = data.at(22);
+        int low = data.at(23);
+        int dataLen = (high << 8) | low;
+        //qDebug() << "dataLen:" << dataLen;
+        // 长度不对
+        if ((dataLen + sizeof(packageDataHead)) > data.size()) {
+            //qDebug() << "message len error";
+            return;
+        }
+        QByteArray temp = data.left(sizeof(packageDataHead) + dataLen + 1);
+        //queue_mutex.lock();
+        readData.enqueue(temp);
+        //queue_mutex.unlock();
+        data.remove(0, sizeof(packageDataHead) + dataLen + 1);//need check,20210915,jiaolili
     }
 }

+ 1 - 0
src/v2x/obuUdpClient/udpreciver.h

@@ -20,6 +20,7 @@ private:
     int m_port;
     QByteArray m_data;
     QThread *m_thread;
+    void ReceiveDecode(QByteArray &data);
 };