XNSim/Release/include/XNCore/XNDDSInterface.h

76 lines
2.4 KiB
C
Raw Normal View History

2025-04-28 12:25:20 +08:00
#pragma once
2025-05-22 08:53:29 +08:00
#include "XNFramework.h"
#include "XNDDSManager.h"
#include "XNByteArray.h"
#include "XNTypeTraits.h"
#include <stdexcept>
2025-05-19 09:49:54 +08:00
// 定义UDP包的最大大小
constexpr size_t MAX_UDP_PACKET_SIZE = 40000;
class XNDDSInterface
2025-04-28 12:25:20 +08:00
{
public:
2025-05-19 09:49:54 +08:00
XNDDSInterface() = default;
virtual ~XNDDSInterface() = default;
2025-04-28 12:25:20 +08:00
public:
/**
* @brief
* @param framework:
*/
virtual void Initialize(XNFrameworkPtr framework, uint32_t modelID, uint32_t DDS_type) = 0;
/**
* @brief UDP包
* @return
*/
XNByteArray getUDPPackage();
/**
* @brief UDP包设置数据
* @param package: UDP包
*/
void setDataByUDPPackage(const XNByteArray &package);
/**
2025-05-22 08:53:29 +08:00
* @brief
* @param varNames:
* @return:
*/
std::unordered_map<std::string, std::string>
getStringData(const std::vector<std::string> &varNames);
2025-05-22 08:53:29 +08:00
/**
* @brief
* @param data:
*/
void setDataByString(const std::unordered_map<std::string, std::string> &data);
2025-05-19 09:49:54 +08:00
protected:
2025-05-22 08:53:29 +08:00
virtual void clearOutData() {}
virtual void sendOutData() {}
2025-04-28 12:25:20 +08:00
protected:
2025-05-19 09:49:54 +08:00
std::unordered_map<std::string, std::function<std::string()>> getDataFunction;
2025-05-22 08:53:29 +08:00
std::unordered_map<std::string, std::function<void(std::string)>> setDataFunction;
std::vector<std::function<XNByteArray(void)>> getByteArrayFunction;
std::vector<std::function<void(XNByteArray)>> setByteArrayFunction;
std::mutex dataMutex;
std::mutex outDataMutex;
uint8_t header[8]{0}; // 固定大小的头部
size_t headerSize = 8;
2025-05-22 08:53:29 +08:00
FAST_DDS_MACRO::DataWriter *dataWriter;
2025-04-28 12:25:20 +08:00
};
#define MAP_DATA_FUNC(NAME) \
getDataFunction[#NAME] = [this]() { return StringSerializer(data.NAME()); }; \
setDataFunction[#NAME] = [this](const std::string &value) { \
StringDeserializer(out_data.NAME(), value); \
2025-05-22 08:53:29 +08:00
}; \
getByteArrayFunction.push_back([this]() { return ByteArraySerializer(data.NAME()); }); \
setByteArrayFunction.push_back([this](const XNByteArray &byteArray) { \
ByteArrayDeserializer(out_data.NAME(), byteArray); \
});