2025-05-21 12:17:50 +08:00
|
|
|
#include "XNDDSInterface.h"
|
|
|
|
#include "XNObject_p.h"
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
2025-05-22 08:53:29 +08:00
|
|
|
XNByteArray XNDDSInterface::getUDPPackage()
|
2025-05-21 12:17:50 +08:00
|
|
|
{
|
2025-05-22 08:53:29 +08:00
|
|
|
XNByteArray result;
|
2025-05-21 12:17:50 +08:00
|
|
|
|
|
|
|
size_t currentPos = 0;
|
|
|
|
|
|
|
|
// 复制头部
|
2025-05-22 10:54:46 +08:00
|
|
|
result.append(header, headerSize);
|
|
|
|
currentPos = headerSize;
|
2025-05-21 12:17:50 +08:00
|
|
|
|
|
|
|
// 复制数据
|
2025-05-26 15:27:02 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(dataMutex);
|
|
|
|
for (auto func : getByteArrayFunction) {
|
|
|
|
XNByteArray byteArray = func();
|
|
|
|
size_t byteArraySize = byteArray.size();
|
|
|
|
if (currentPos + byteArraySize <= MAX_UDP_PACKET_SIZE) {
|
|
|
|
result.append(byteArray);
|
|
|
|
currentPos += byteArraySize; // 使用实际返回的字节数组大小
|
|
|
|
} else {
|
|
|
|
break; // 超出最大包大小
|
|
|
|
}
|
2025-05-21 12:17:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 更新包大小
|
2025-05-22 10:54:46 +08:00
|
|
|
if (currentPos >= 8 && currentPos < MAX_UDP_PACKET_SIZE) {
|
|
|
|
result[6] = static_cast<uint8_t>((currentPos >> 8) & 0xFF);
|
|
|
|
result[7] = static_cast<uint8_t>(currentPos & 0xFF);
|
2025-05-21 12:17:50 +08:00
|
|
|
}
|
2025-05-22 08:53:29 +08:00
|
|
|
|
|
|
|
return result;
|
2025-05-21 12:17:50 +08:00
|
|
|
}
|
|
|
|
|
2025-05-22 10:54:46 +08:00
|
|
|
void XNDDSInterface::setDataByUDPPackage(const XNByteArray &package)
|
|
|
|
{
|
|
|
|
if (package.size() < headerSize) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取包大小
|
|
|
|
uint16_t packageSize = (package[6] << 8) | package[7];
|
|
|
|
if (packageSize > MAX_UDP_PACKET_SIZE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-05-26 15:27:02 +08:00
|
|
|
uint32_t currentPos = 8;
|
2025-05-22 10:54:46 +08:00
|
|
|
|
2025-05-26 15:27:02 +08:00
|
|
|
// 设置数据
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(outDataMutex);
|
|
|
|
clearOutData();
|
|
|
|
for (auto &func : setByteArrayFunction) {
|
|
|
|
func(package, currentPos);
|
2025-05-22 10:54:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sendOutData();
|
|
|
|
}
|
|
|
|
|
2025-05-22 08:53:29 +08:00
|
|
|
std::unordered_map<std::string, std::string>
|
|
|
|
XNDDSInterface::getStringData(std::vector<std::string> varNames)
|
2025-05-21 12:17:50 +08:00
|
|
|
{
|
2025-05-22 08:53:29 +08:00
|
|
|
std::unordered_map<std::string, std::string> result;
|
2025-05-21 12:17:50 +08:00
|
|
|
|
2025-05-26 15:27:02 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(dataMutex);
|
|
|
|
for (const auto &varName : varNames) {
|
|
|
|
auto it = getDataFunction.find(varName);
|
|
|
|
if (it == getDataFunction.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result[varName] = it->second();
|
2025-05-22 08:53:29 +08:00
|
|
|
}
|
2025-05-21 12:17:50 +08:00
|
|
|
}
|
|
|
|
|
2025-05-22 08:53:29 +08:00
|
|
|
return result;
|
|
|
|
}
|
2025-05-21 12:17:50 +08:00
|
|
|
|
2025-05-22 08:53:29 +08:00
|
|
|
void XNDDSInterface::setDataByString(std::unordered_map<std::string, std::string> data)
|
|
|
|
{
|
2025-05-26 15:27:02 +08:00
|
|
|
std::lock_guard<std::mutex> lock(outDataMutex);
|
2025-05-22 08:53:29 +08:00
|
|
|
clearOutData();
|
|
|
|
for (const auto &[varName, value] : data) {
|
|
|
|
auto it = setDataFunction.find(varName);
|
|
|
|
if (it == setDataFunction.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
it->second(value);
|
2025-05-21 12:17:50 +08:00
|
|
|
}
|
2025-05-22 08:53:29 +08:00
|
|
|
sendOutData();
|
|
|
|
}
|