From e8a27bb203fe2aff70390a5eca002d7438da9b0f Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期三, 22 十月 2025 14:24:34 +0800
Subject: [PATCH] Merge branch 'clh' into liuyang

---
 SourceCode/Bond/DAQBridge/net/FrameAssembler.h |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 58 insertions(+), 0 deletions(-)

diff --git a/SourceCode/Bond/DAQBridge/net/FrameAssembler.h b/SourceCode/Bond/DAQBridge/net/FrameAssembler.h
new file mode 100644
index 0000000..dd68684
--- /dev/null
+++ b/SourceCode/Bond/DAQBridge/net/FrameAssembler.h
@@ -0,0 +1,58 @@
+#pragma once
+#include <vector>
+#include <cstdint>
+#include <algorithm>
+
+class FrameAssembler {
+public:
+    // 追加一块原始字节
+    void push(const std::vector<uint8_t>& chunk) {
+        buf_.insert(buf_.end(), chunk.begin(), chunk.end());
+    }
+    void push(const uint8_t* p, size_t n) {
+        buf_.insert(buf_.end(), p, p + n);
+    }
+
+    // 提取下一帧;返回 true 表示 out 拿到了一帧完整数据
+    bool nextFrame(std::vector<uint8_t>& out) {
+        const uint8_t HEAD[4] = { 0x11,0x88,0x11,0x88 };
+        const uint8_t TAIL = 0x88;
+        for (;;) {
+            // 需要至少 4B 头 + 4B dataId + 2B len + 1B 尾 = 11B
+            if (buf_.size() < 11) return false;
+
+            // 找头同步
+            size_t i = 0;
+            while (i + 4 <= buf_.size() && !std::equal(HEAD, HEAD + 4, buf_.begin() + i)) ++i;
+            if (i + 4 > buf_.size()) { // 没找到头,清空
+                buf_.clear();
+                return false;
+            }
+            if (i > 0) buf_.erase(buf_.begin(), buf_.begin() + i); // 丢弃头前噪声
+
+            if (buf_.size() < 11) return false; // 还不够最小帧
+
+            // 读取正文长度(大端)
+            uint16_t len = (uint16_t(buf_[8]) << 8) | buf_[9];
+            size_t total = 4 + 4 + 2 + size_t(len) + 1; // 整帧长度
+            if (buf_.size() < total) return false; // 半帧,等下次
+
+            // 校验尾
+            if (buf_[total - 1] != TAIL) {
+                // 尾不对:丢弃一个字节,重新找头(避免死锁)
+                buf_.erase(buf_.begin());
+                continue;
+            }
+
+            // 取出完整帧
+            out.assign(buf_.begin(), buf_.begin() + total);
+            buf_.erase(buf_.begin(), buf_.begin() + total);
+            return true;
+        }
+    }
+
+    void clear() { buf_.clear(); }
+
+private:
+    std::vector<uint8_t> buf_;
+};

--
Gitblit v1.9.3