From 5e1d88f2e2a87c1faca0e9a34bb27e93c61f0941 Mon Sep 17 00:00:00 2001 From: wyc <1104749580@qq.com> Date: Mon, 29 Jun 2026 11:06:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SSR/convert/DataOutputFormats.cpp | 76 ++++++++++++++++++++----------- SSR/convert/DataOutputFormats.h | 7 +-- 2 files changed, 51 insertions(+), 32 deletions(-) diff --git a/SSR/convert/DataOutputFormats.cpp b/SSR/convert/DataOutputFormats.cpp index 0692457..bcca45b 100644 --- a/SSR/convert/DataOutputFormats.cpp +++ b/SSR/convert/DataOutputFormats.cpp @@ -59,35 +59,57 @@ std::string packet_to_escape_format(std::string_view t) { } return result; } -void Binary_Format_handle_buffer( - std::string& buffer, std::string_view data, - const std::function& callback) { - size_t i = 0; - while (i < data.size()) { - if (data[i] == 0x1A) { - // 如果遇到起始标记 0x1A - if (i + 1 < data.size() && data[i + 1] == 0x1A) { - // 如果是转义的 0x1A 0x1A,继续处理 - buffer.push_back(0x1A); // 添加一个正常的 0x1A 字节 - i += 2; // 跳过这两个字节 - } - else { - // 检测到起始标记 0x1A,认为是新消息的开始 - if (!buffer.empty()) { - // 如果当前消息 buffer 中已有数据,调用回调函数处理当前消息 - callback(buffer); - buffer.clear(); // 清空当前消息的缓存 - } - buffer.push_back(0x1A); - // 跳过当前的 SOP 标记 (0x1A) - i++; - } +// void Binary_Format_handle_buffer(std::string& buffer, std::string_view data, const std::function& callback) { +// size_t i = 0; +// while (i < data.size()) { +// if (data[i] == 0x1A) { +// // 如果遇到起始标记 0x1A +// if (i + 1 < data.size() && data[i + 1] == 0x1A) { +// // 如果是转义的 0x1A 0x1A,继续处理 +// buffer.push_back(0x1A); // 添加一个正常的 0x1A 字节 +// i += 2; // 跳过这两个字节 +// } +// else { +// // 检测到起始标记 0x1A,认为是新消息的开始 +// if (!buffer.empty()) { +// // 如果当前消息 buffer 中已有数据,调用回调函数处理当前消息 +// callback(buffer); +// buffer.clear(); // 清空当前消息的缓存 +// } +// buffer.push_back(0x1A); +// // 跳过当前的 SOP 标记 (0x1A) +// i++; +// } +// } +// else { +// // 如果不是 0x1A,直接将数据添加到 buffer 中 +// buffer.push_back(data[i]); +// i++; +// } +// } +// } +void Binary_Format_handle_buffer(std::string& buffer, std::string_view data_view, const std::function& callback) { + constexpr char sop = 0x1A; + buffer.reserve(buffer.size() + data_view.size()); + std::size_t i = 0; + while (i < data_view.size()) { + auto pos = data_view.find(sop, i); + if (pos == std::string_view::npos) { + buffer.append(data_view.data() + i, data_view.size() - i); + break; } - else { - // 如果不是 0x1A,直接将数据添加到 buffer 中 - buffer.push_back(data[i]); - i++; + buffer.append(data_view.data() + i, pos - i); + if (pos + 1 < data_view.size() && data_view[pos + 1] == sop) { + buffer.push_back(sop); + i = pos + 2; + continue; } + if (!buffer.empty()) { + callback(buffer); + buffer.clear(); + } + buffer.push_back(sop); + i = pos + 1; } } // 9 + 2 diff --git a/SSR/convert/DataOutputFormats.h b/SSR/convert/DataOutputFormats.h index ebe3164..346a37f 100644 --- a/SSR/convert/DataOutputFormats.h +++ b/SSR/convert/DataOutputFormats.h @@ -360,9 +360,7 @@ inline std::uint8_t get_len(std::string_view msg) { // Before Escaping: 1A 32 27 1A E8 57 F0 1A 6C // During Transmission: 1A 32 27 1A 1A E8 57 F0 1A 1A 6C // After Un-Escaping: 1A 32 27 1A E8 57 F0 1A 6C -void Binary_Format_handle_buffer( - std::string& buffer, std::string_view data, - const std::function& callback); +void Binary_Format_handle_buffer(std::string& buffer, std::string_view data, const std::function& callback); /* Command Message (Host ==> Device) The command-message has a simple ASCII based structure. Each message starts with a ‘#’ (0x23) and ends with ‘’ (0x0D, 0x0A). In-between are one to 16 @@ -373,8 +371,7 @@ first byte is mandatory and holds the command while the remaining bytes are parameters. Available commands depend on the selected protocol and are discussed in the respective protocol section. */ -std::string mode_s_msg_packet_to_server_packet(std::uint32_t id, - std::string_view packet); +std::string mode_s_msg_packet_to_server_packet(std::uint32_t id, std::string_view packet); // 1a34 radarcape 状态帧消息 // https://wiki.jetvision.de/wiki/Radarcape:Version_History#Status_Frame_0x34_contents_were_extended_with_GPS_and_UTC_information // 状态帧0x34内容已扩展为 GPS 和 UTC 信息