Iceoryx2:高性能进程间通信框架(中间件)

文章目录

    • [0. 引言](#0. 引言)
    • [1. 主要改进](#1. 主要改进)
    • [2. Iceoryx2 的架构](#2. Iceoryx2 的架构)
    • [3. C++示例代码](#3. C++示例代码)
      • [3.1 发布者示例(`publisher.cpp`)](#3.1 发布者示例(publisher.cpp))
      • [3.2 订阅者示例(`subscriber.cpp`)](#3.2 订阅者示例(subscriber.cpp))
    • [4. 机制比较](#4. 机制比较)
    • [5. 架构比较](#5. 架构比较)
    • [6. Iceoryx vs Iceoryx2](#6. Iceoryx vs Iceoryx2)
    • 参考资料

0. 引言

Iceoryx2 是一个基于 Rust 实现的开源中间件,专为实现低延迟和零拷贝进程间通信而设计。相比其前身Iceoryx,Iceoryx2 在内存安全、并发处理、模块化设计以及多平台支持上进行了优化。

提前阅读:

1. 主要改进

  1. 零拷贝通信:Iceoryx2 保留了零拷贝通信的特性,通过直接在进程间传递数据引用,极大减少了数据复制,从而提升了性能并降低了延迟。

  2. Rust 语言的引入:采用 Rust 语言后,Iceoryx2 提升了内存安全和并发安全性。Rust 的所有权和借用机制有效防止了数据竞争和其他常见的并发错误。

  3. 模块化和扩展性:模块化设计允许单独替换或升级内部组件。

  4. 跨平台支持:除了在 Linux 和 Windows 上的原生支持,Iceoryx2 还计划扩展到 Android、QNX 等多个平台。

  5. 支持多种编程语言:Iceoryx2 不仅提供 C 和 C++ 的 API 绑定,还支持 Python 和其他编程语言。

2. Iceoryx2 的架构

以下是 Iceoryx2 的架构图,展示了主要应用模块、通信网关及支持的开发语言:
Supported Platforms Iceoryx2 Vision Android Linux x86_64 Linux aarch64 Linux 32-bit macOS QNX VxWorks Windows GPU FPGA Iceoryx2 App Iceoryx2 App zenoh App DDS App ... App ROS 2 App ROS 2 App ROS 2 Tooling Autosar App Adaptive Autosar Iceoryx2 - Gateway Iceoryx2 RMW Iceoryx2 C / C++ / Python / ... Supported_Platforms

3. C++示例代码

以下是 Iceoryx2 的发布者和订阅者的示例代码:

3.1 发布者示例(publisher.cpp

cpp 复制代码
#include "iox/duration.hpp"
#include "iox/slice.hpp"
#include "iox2/node.hpp"
#include "iox2/sample_mut.hpp"
#include "iox2/service_name.hpp"
#include "iox2/service_type.hpp"

#include <cstdint>
#include <iostream>
#include <utility>

constexpr iox::units::Duration CYCLE_TIME = iox::units::Duration::fromSeconds(1);

auto main() -> int {
    using namespace iox2;
    auto node = NodeBuilder().create<ServiceType::Ipc>().expect("successful node creation");

    auto service = node.service_builder(ServiceName::create("Service With Dynamic Data").expect("valid service name"))
                       .publish_subscribe<iox::Slice<uint8_t>>()
                       .open_or_create()
                       .expect("successful service creation/opening");

    uint64_t worst_case_memory_size = 1024; // NOLINT
    auto publisher = service.publisher_builder()
                         .max_slice_len(worst_case_memory_size)
                         .create()
                         .expect("successful publisher creation");

    auto counter = 1;

    while (node.wait(CYCLE_TIME).has_value()) {
        counter += 1;

        auto required_memory_size = (8 + counter) % 16; // NOLINT
        auto sample = publisher.loan_slice_uninit(required_memory_size).expect("acquire sample");
        sample.write_from_fn([&](auto byte_idx) { return (byte_idx + counter) % 255; }); // NOLINT

        auto initialized_sample = assume_init(std::move(sample));
        send(std::move(initialized_sample)).expect("send successful");

        std::cout << "Send sample " << counter << "..." << std::endl;
    }

    std::cout << "exit" << std::endl;

    return 0;
}

3.2 订阅者示例(subscriber.cpp

cpp 复制代码
#include "iox/duration.hpp"
#include "iox/slice.hpp"
#include "iox2/node.hpp"
#include "iox2/service_name.hpp"
#include "iox2/service_type.hpp"

#include <cstdint>
#include <iostream>

constexpr iox::units::Duration CYCLE_TIME = iox::units::Duration::fromSeconds(1);

auto main() -> int {
    using namespace iox2;
    auto node = NodeBuilder().create<ServiceType::Ipc>().expect("successful node creation");

    auto service = node.service_builder(ServiceName::create("Service With Dynamic Data").expect("valid service name"))
                       .publish_subscribe<iox::Slice<uint8_t>>()
                       .open_or_create()
                       .expect("successful service creation/opening");

    auto subscriber = service.subscriber_builder().create().expect("successful subscriber creation");

    while (node.wait(CYCLE_TIME).has_value()) {
        auto sample = subscriber.receive().expect("receive succeeds");
        while (sample.has_value()) {
            std::cout << "received " << sample->payload().size() << " bytes: ";
            for (auto byte : sample->payload()) {
                std::cout << std::hex << byte << " ";
            }
            std::cout << std::endl;
            sample = subscriber.receive().expect("receive succeeds");
        }
    }

    std::cout << "exit" << std::endl;

    return 0;
}

4. 机制比较

5. 架构比较

6. Iceoryx vs Iceoryx2

特性 Iceoryx Iceoryx2
编程语言 C++ Rust
是否支持真正的零拷贝数据传输
是否需要中央守护进程
消息传递模式 发布-订阅 发布-订阅
通知机制 轮询 事件
支持的平台 Linux, Windows, macOS, FreeBSD, QNX, FreeRTOS Linux, Windows, macOS, FreeBSD, Android), QNX, FreeRTOS, VxWorks
语言绑定 C/C++ Rust/C/C++/Python/Go/C#/Lua

参考资料

Welcome to iceoryx2's C / C++ documentation!
eclipse-iceoryx/iceoryx
eclipse-iceoryx/iceoryx2

相关推荐
JoannaJuanCV2 天前
error: can‘t find Rust compiler
开发语言·后端·rust
Kiri霧2 天前
在actix-web应用用构建集成测试
后端·rust·集成测试
muyouking112 天前
Tauri Android 开发踩坑实录:从 Gradle 版本冲突到离线构建成功
android·rust
Kiri霧2 天前
Rust开发环境搭建
开发语言·后端·rust
虚行3 天前
Mysql 数据同步中间件 对比
数据库·mysql·中间件
康谋自动驾驶3 天前
ROS 传感器模块的通用架构设计与跨中间件扩展实践
中间件
奥尔特星云大使3 天前
mysql读写分离中间件Atlas安装部署及使用
数据库·mysql·中间件·读写分离·atlas
聊聊MES那点事3 天前
Cogent DataHub vs Kepware,两大工业数据中间件的深度对比分析
开发语言·中间件·opc·opc ua
千鼎数字孪生-可视化3 天前
智能制造中的中间件作用:融通设备、系统和云平台
中间件·制造·智能制造
千汇数据的老司机3 天前
边缘存储+中间件协同策略:实现设备数据高效处理与低延迟响应
中间件