#include "XNDDSInterface.h" #include "XNObject_p.h" #include using json = nlohmann::json; void XNDDSInterface::getUDPPackage(uint8_t *buffer, size_t bufferSize) { if (bufferSize < MAX_UDP_PACKET_SIZE) return; size_t currentPos = 0; // 复制头部 if (headerSize >= 5) { std::memcpy(buffer, header, 5); currentPos = 5; } // 复制数据 for (auto func : getByteArrayFunction) { if (currentPos + func.size <= MAX_UDP_PACKET_SIZE) { func.func(buffer + currentPos, func.size); currentPos += func.size; } else { break; // 超出最大包大小 } } // 更新包大小 if (currentPos >= 5) { buffer[4] = static_cast(currentPos); } } std::string XNDDSInterface::getData(const std::string &varName) { int index1 = -1; int index2 = -1; std::string trueVarName = varName; size_t startPos = varName.find('['); if (startPos != std::string::npos) { size_t midPos = varName.find("][", startPos); size_t endPos = varName.find_last_of(']'); if (midPos != std::string::npos) { try { index1 = std::stoi(varName.substr(startPos + 1, midPos - startPos - 1)); index2 = std::stoi(varName.substr(midPos + 2, endPos - midPos - 2)); } catch (...) { std::cerr << "无法解析数组索引: " << varName << std::endl; index1 = 0; index2 = 0; } } else if (endPos != std::string::npos) { try { index1 = std::stoi(varName.substr(startPos + 1, endPos - startPos - 1)); } catch (...) { std::cerr << "无法解析数组索引: " << varName << std::endl; index1 = 0; } } trueVarName = varName.substr(0, startPos); } auto it = getDataFunction.find(trueVarName); if (it == getDataFunction.end()) { return std::string(); } std::lock_guard lock(mutex); std::string result = it->second(); if (index1 < 0) { return result; } std::vector list; std::stringstream ss(result); std::string item; while (std::getline(ss, item, ',')) { list.push_back(item); } if (index1 >= static_cast(list.size())) { std::cerr << "数组索引超出范围: " << varName << std::endl; return std::string(); } if (index2 < 0) { return list[index1]; } std::vector list2; std::stringstream ss2(list[index1]); while (std::getline(ss2, item, ' ')) { list2.push_back(item); } if (index2 >= static_cast(list2.size())) { std::cerr << "数组索引超出范围: " << varName << std::endl; return std::string(); } return list2[index2]; }