XNSim/XNCore/XNDDSInterface.h

76 lines
2.4 KiB
C++

#pragma once
#include "XNFramework.h"
#include "XNDDSManager.h"
#include "XNByteArray.h"
#include "XNTypeTraits.h"
#include <stdexcept>
// 定义UDP包的最大大小
constexpr size_t MAX_UDP_PACKET_SIZE = 40000;
class XNDDSInterface
{
public:
XNDDSInterface() = default;
virtual ~XNDDSInterface() = default;
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);
/**
* @brief 批量获取指定变量的数据
* @param varNames: 变量名列表
* @return: 变量名到数据的映射
*/
std::unordered_map<std::string, std::string>
getStringData(const std::vector<std::string> &varNames);
/**
* @brief 批量设置指定变量的数据
* @param data: 变量名到数据的映射
*/
void setDataByString(const std::unordered_map<std::string, std::string> &data);
protected:
virtual void clearOutData() {}
virtual void sendOutData() {}
protected:
std::unordered_map<std::string, std::function<std::string()>> getDataFunction;
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;
FAST_DDS_MACRO::DataWriter *dataWriter;
};
#define MAP_DATA_FUNC(NAME) \
getDataFunction[#NAME] = [this]() { return StringSerializer(data.NAME()); }; \
setDataFunction[#NAME] = [this](const std::string &value) { \
StringDeserializer(out_data.NAME(), value); \
}; \
getByteArrayFunction.push_back([this]() { return ByteArraySerializer(data.NAME()); }); \
setByteArrayFunction.push_back([this](const XNByteArray &byteArray) { \
ByteArrayDeserializer(out_data.NAME(), byteArray); \
});