iceoryx UntypedClient VS Client

Iceoryx 是一个高效的跨进程通信框架,主要用于实时系统和嵌入式系统中。Iceoryx 提供了两种主要的客户端类型:UntypedClientClient。它们之间的主要区别在于类型安全性和使用场景。

UntypedClient vs Client

  1. UntypedClient

    • 特性:不提供类型安全性,用户需要手动处理消息的序列化和反序列化。

    • 应用场景:适用于需要高度灵活性和自定义序列化/反序列化逻辑的场景。

    • 使用复杂度:较高,因为需要手动管理数据的类型和内存。

  2. Client

    • 特性:提供类型安全性,使用模板参数指定请求和响应的类型,Iceoryx 自动处理序列化和反序列化。

    • 应用场景:适用于大多数需要类型安全和简化数据处理的场景。

    • 使用复杂度:较低,因为 Iceoryx 自动处理数据类型和内存管理。

示例代码

UntypedClient 示例

以下是一个使用 UntypedClient 的示例代码:

客户端代码

#include "iceoryx_posh/popo/untyped_client.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include "iceoryx_utils/cxx/helplets.hpp"
#include <iostream>

int main()
{
    // 初始化 Iceoryx 运行时
    iox::runtime::PoshRuntime::initRuntime("untyped-client");

    // 创建一个 UntypedClient
    iox::popo::UntypedClient client({"Example", "Untyped", "Request"});

    // 等待服务可用
    client.waitForServer();    // 发送请求
    std::string request = "Hello, Iceoryx!";
    client.loan(request.size())
        .and_then([&](auto& payload) {
            std::memcpy(payload, request.data(), request.size());
            client.send(payload);
        })
        .or_else([](auto& error) {
            std::cerr << "Failed to loan request: " << error << std::endl;
        });

    // 接收响应
    client.take()
        .and_then([](const void* payload) {
            std::string response(static_cast<const char*>(payload));
            std::cout << "Received response: " << response << std::endl;
        })
        .or_else([](auto& error) {
            std::cerr << "Failed to take response: " << error << std::endl;
        });

    return 0;
}

服务端代码

#include "iceoryx_posh/popo/untyped_server.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include <iostream>int main()
{
    // 初始化 Iceoryx 运行时
    iox::runtime::PoshRuntime::initRuntime("untyped-server");

    // 创建一个 UntypedServer
    iox::popo::UntypedServer server({"Example", "Untyped", "Request"});

    // 处理请求
    server.take()
        .and_then([&](const void* payload) {
            std::string request(static_cast<const char*>(payload));
            std::cout << "Received request: " << request << std::endl;

            std::string response = "Hello from server!";
            server.loan(response.size())
                .and_then([&](auto& payload) {
                    std::memcpy(payload, response.data(), response.size());
                    server.send(payload);
                })
                .or_else([](auto& error) {
                    std::cerr << "Failed to loan response: " << error << std::endl;
                });
        })
        .or_else([](auto& error) {
            std::cerr << "Failed to take request: " << error << std::endl;
        });

    return 0;
}

Client 示例

以下是一个使用 Client 的示例代码:

客户端代码

#include "iceoryx_posh/popo/client.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include "iceoryx_utils/cxx/helplets.hpp"
#include <iostream>

struct Request
{
    char message[128];
};

struct Response
{
    char message[128];
};

int main()
{
    // 初始化 Iceoryx 运行时
    iox::runtime::PoshRuntime::initRuntime("typed-client");

    // 创建一个 Client
    iox::popo::Client<Request, Response> client({"Example", "Typed", "Request"});

    // 等待服务可用
    client.waitForServer();    // 发送请求
    Request request;
    std::strncpy(request.message, "Hello, Iceoryx!", sizeof(request.message));
    client.loan()
        .and_then([&](auto& payload) {
            *payload = request;
            client.send(std::move(payload));
        })
        .or_else([](auto& error) {
            std::cerr << "Failed to loan request: " << error << std::endl;
        });

    // 接收响应
    client.take()
        .and_then([](const Response& response) {
            std::cout << "Received response: " << response.message << std::endl;
        })
        .or_else([](auto& error) {
            std::cerr << "Failed to take response: " << error << std::endl;
        });

    return 0;
}

服务端代码

#include "iceoryx_posh/popo/server.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include <iostream>

struct Request
{
    char message[128];
};

struct Response
{
    char message[128];
};int main()
{
    // 初始化 Iceoryx 运行时
    iox::runtime::PoshRuntime::initRuntime("typed-server");

    // 创建一个 Server
    iox::popo::Server<Request, Response> server({"Example", "Typed", "Request"});

    // 处理请求
    server.take()
        .and_then([&](const Request& request) {
            std::cout << "Received request: " << request.message << std::endl;

            Response response;
            std::strncpy(response.message, "Hello from server!", sizeof(response.message));
            server.loan()
                .and_then([&](auto& payload) {
                    *payload = response;
                    server.send(std::move(payload));
                })
                .or_else([](auto& error) {
                    std::cerr << "Failed to loan response: " << error << std::endl;
                });
        })
        .or_else([](auto& error) {
            std::cerr << "Failed to take request: " << error << std::endl;
        });

    return 0;
}

小结

  • UntypedClient:提供了更高的灵活性,但需要手动处理数据的序列化和反序列化,适用于需要自定义数据处理的场景。

  • Client:提供了类型安全性,使用模板参数指定请求和响应的类型,适用于大多数需要简化数据处理的场景。

根据具体需求选择合适的客户端类型,以便更好地满足应用场景的要求。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值