# az_iox **Repository Path**: stdarg/az_iox ## Basic Information - **Project Name**: az_iox - **Description**: 基于iceoryx封装的pub/sub/req/res接口封装 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-03-16 - **Last Updated**: 2026-03-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # az_iox - C++ DDS通信框架(基于iceoryx) 基于[eclipse-iceoryx](https://github.com/eclipse-iceoryx/iceoryx)实现的高性能C++ DDS(数据分发服务)通信框架,支持发布订阅(Pub/Sub)、请求响应(Req/Res)和RPC通信模式。 ## 特性 - **类型安全**:利用C++模板实现编译期类型检查 - **自动Topic生成**:通过`typeid`和`std::hash`从类型信息自动生成唯一Topic标识 - **零拷贝通信**:基于iceoryx共享内存机制实现高效进程间通信 - **多种通信模式**: - Pub/Sub:发布订阅模式 - Req/Res:请求响应模式 - RPC:远程过程调用 - **现代化C++**:C++17标准,支持移动语义 ## 依赖 - C++17编译器(GCC 7+ / Clang 5+) - CMake 3.16+ - iceoryx 2.0+ ## 安装iceoryx(Ubuntu/Debian) ```bash sudo apt-get update sudo apt-get install -y libiceoryx-posh-dev libiceoryx-hoofs-dev iceoryx ``` ## 编译项目 ```bash mkdir build && cd build cmake .. make -j$(nproc) ``` ## 使用示例 ### 发布订阅模式(Pub/Sub) ```cpp #include "az_iox/publisher.hpp" #include "az_iox/subscriber.hpp" struct MyData { int id; std::string message; }; // Publisher az_iox::PublisherBase::init_runtime("publisher_app"); az_iox::Publisher pub; pub.publish(MyData{1, "hello"}); // Subscriber az_iox::PublisherBase::init_runtime("subscriber_app"); az_iox::Subscriber sub; sub.subscribe([](const MyData& data) { std::cout << data.message << std::endl; }); sub.start(); ``` ### 请求响应模式(Req/Res) ```cpp #include "az_iox/requester.hpp" #include "az_iox/responser.hpp" // Responser (Server) az_iox::Responser resp; resp.register_handler([](const int& req) -> int { return req * req; // 返回平方 }); resp.run(); // Requester (Client) az_iox::Requester req; int result = req.request(5); // 返回25 ``` ### RPC通信 ```cpp #include "az_iox/rpc_client.hpp" #include "az_iox/rpc_server.hpp" struct RpcRequest { int method_id; }; struct RpcResponse { int result; }; // RPC Server az_iox::RpcServer server; server.register_handler([](const RpcRequest& req) -> RpcResponse { return RpcResponse{req.method_id * 2}; }); server.run(); // RPC Client az_iox::RpcClient client; auto resp = client.call(RpcRequest{5}); // 返回RpcResponse{10} ``` ## Topic生成规则 ```cpp // 自动从类型信息生成Topic az_iox::Publisher::type_name(); // "int" az_iox::Publisher::topic_id(); // hash("int") az_iox::Publisher::type_name(); // "MyData" az_iox::Publisher>::type_name(); // "std::vector" ``` ## 项目结构 ``` az_iox/ ├── include/az_iox/ │ ├── type_utils.hpp # 类型名提取工具 │ ├── publisher.hpp # Publisher类(基于iceoryx::popo::Publisher) │ ├── subscriber.hpp # Subscriber类(基于iceoryx::popo::Subscriber) │ ├── requester.hpp # Requester类(基于iceoryx::popo::Client) │ ├── responser.hpp # Responser类(基于iceoryx::popo::Server) │ ├── rpc_client.hpp # RpcClient类 │ └── rpc_server.hpp # RpcServer类 ├── src/ │ ├── type_utils.cpp # TypeUtils实现 │ ├── publisher.cpp # PublisherBase实现 │ ├── subscriber.cpp # 占位文件 │ ├── requester.cpp # 占位文件 │ ├── responser.cpp # 占位文件 │ ├── rpc_client.cpp # 占位文件 │ └── rpc_server.cpp # 占位文件 ├── examples/ │ ├── example_pub.cpp # 发布者示例 │ ├── example_sub.cpp # 订阅者示例 │ ├── example_req.cpp # 请求者示例 │ ├── example_resp.cpp # 响应者示例 │ ├── example_rpc_client.cpp # RPC客户端示例 │ └── example_rpc_server.cpp # RPC服务端示例 └── CMakeLists.txt ``` ## 运行示例 需要先启动RouDi守护进程: ```bash # 终端1:启动RouDi iox-roudi # 终端2:启动发布者 ./build/example_iox_pub # 终端3:启动订阅者 ./build/example_iox_sub ``` ## 注意事项 1. 使用iceoryx前必须先启动`iox-roudi`守护进程 2. 自定义数据类型需要满足iceoryx的要求(如固定大小、可平凡复制等) 3. 首次运行时会自动初始化运行时,也可以通过`PublisherBase::init_runtime()`手动初始化 4. 所有模板类的实现主要在头文件中,cpp文件为占位文件用于编译库 ## API说明 ### Publisher | 方法 | 说明 | |------|------| | `publish(const T& data)` | 发布数据 | | `publish_loan(std::function)` | 使用loan机制发布 | | `type_name()` | 获取类型名(static) | | `topic_id()` | 获取topic hash(static) | | `get_topic_name()` | 获取topic名称 | | `is_available()` | 检查是否可用 | ### Subscriber | 方法 | 说明 | |------|------| | `subscribe(callback)` | 设置接收回调 | | `start()` | 启动接收线程 | | `stop()` | 停止接收 | | `poll(timeout_ms)` | 单次轮询 | | `has_data()` | 检查是否有数据 | ### Requester | 方法 | 说明 | |------|------| | `request(data, timeout_ms)` | 同步请求 | | `request_async(data)` | 异步请求 | | `wait_for_service(timeout_ms)` | 等待服务可用 | | `is_connected()` | 检查连接状态 | ### Responser | 方法 | 说明 | |------|------| | `register_handler(handler)` | 注册处理函数 | | `start()` | 启动服务线程 | | `run()` | 运行服务(阻塞) | | `stop()` | 停止服务 | | `poll(timeout_ms)` | 单次处理 |