| RMVL 2.3.0 Robotic Manipulation and Vision Library

载入中...
搜索中...
未找到
进程间通信设施 —— IPC

作者
  赵曦

日期
  2025/09/25

版本
  1.0

上一篇教程：基于异步 I/O 的协程设施
下一篇教程：串口通信模块

# 1. 命名管道

相关类：

- 同步： rm::PipeServer, rm::PipeClient
- 异步： rm::async::PipeServer, rm::async::PipeClient

管道是一种半双工的通信方式，允许一个进程与另一个进程进行通信。命名管道（Named Pipe）是一种特殊类型的管道，它具有名称，可以在不同的进程之间进行通信。

RMVL 提供了跨平台的命名管道实现，并提供了协程支持，Windows 下基于 Windows 命名管道，Linux 下基于 Unix FIFO 实现。

## 1.1 同步阻塞

服务端

C++
#include <rmvl/io/ipc.hpp>
using namespace rm;
int main() {
PipeServer server("mypipe");
bool success = server.write("message");
if (!success) {
printf("Write failed\n");
return -1;
}
std::string msg = server.read();
if (msg.empty()) {
printf("Read failed\n");
return -1;
}
printf("Received: %s\n", msg.c_str());
}
rm::PipeServer
命名管道服务端
定义 ipc.hpp:24
ipc.hpp
同步/异步的进程间通信框架
rm
定义 datastruct.hpp:20
Python
import rm
server = rm.PipeServer("mypipe")
success = server.write("message")
if not success:
print("Write failed")
return -1
msg = server.read()
if not msg:
print("Read failed")
return -1
print(f"Received: {msg}")

客户端

C++
#include <rmvl/io/ipc.hpp>
using namespace rm;
int main() {
PipeClient client("mypipe");
std::string msg = client.read();
if (msg.empty()) {
printf("Read failed\n");
return -1;
}
printf("Received: %s\n", msg.c_str());
bool success = client.write("received: " + msg);
if (!success) {
printf("Write failed\n");
return -1;
}
}
rm::PipeClient
命名管道客户端
定义 ipc.hpp:79
Python
import rm
client = rm.PipeClient("mypipe")
msg = client.read()
if not msg:
print("Read failed")
return -1
print(f"Received: {msg}")
success = client.write("received: " + msg)
if not success:
print("Write failed")
return -1

## 1.2 异步非阻塞

服务端

#include <rmvl/io/ipc.hpp>
using namespace rm;
async::Task<> session(async::PipeServer server) {
bool success = co_await server.write("message");
if (!success) {
printf("Write failed\n");
co_return;
}
std::string msg = co_await server.read();
if (msg.empty()) {
printf("Read failed\n");
co_return;
}
printf("Received: %s\n", msg.c_str());
}
int main() {
async::IOContext io_context{};
async::PipeServer server("mypipe");
co_spawn(io_context, session, std::move(server));
// 此处可以使用 co_spawn 注册其他 I/O 任务
io_context.run();
return 0;
}
rm::async::IOContext
异步 I/O 执行上下文，负责管理 IO 事件循环和协程任务的调度
定义 async.hpp:177
rm::async::PipeServer
异步命名管道服务端
定义 ipc.hpp:244
rm::async::PipeServer::read
AsyncReadAwaiter read()
从管道读取数据
定义 ipc.hpp:266
rm::async::PipeServer::write
AsyncWriteAwaiter write(std::string_view data)
向管道写入数据
定义 ipc.hpp:280
rm::async::Task
保有 rm::async::Promise 的异步协程任务
定义 async.hpp:146
rm::async::co_spawn
void co_spawn(IOContext &ctx, Callable &&fn, Args &&...args)
在指定的执行上下文中生成并调度一个协程任务
定义 async.hpp:265

客户端

#include <rmvl/io/ipc.hpp>
using namespace rm;
async::Task<> session(async::PipeClient client) {
std::string msg = co_await client.read();
if (msg.empty()) {
printf("Read failed\n");
co_return;
}
printf("Received: %s\n", msg.c_str());
bool success = co_await client.write("received: " + msg);
if (!success) {
printf("Write failed\n");
co_return;
}
}
int main() {
async::IOContext io_context{};
async::PipeClient client("mypipe");
co_spawn(io_context, session, std::move(client));
// 此处可以使用 co_spawn 注册其他 I/O 任务
io_context.run();
return 0;
}
rm::async::PipeClient
异步命名管道客户端
定义 ipc.hpp:289
rm::async::PipeClient::read
AsyncReadAwaiter read()
从管道读取数据
定义 ipc.hpp:302
rm::async::PipeClient::write
AsyncWriteAwaiter write(std::string_view data)
向管道写入数据
定义 ipc.hpp:316

# 2. 消息队列（仅 Linux 可用）

相关类： rm::MqServer 及 rm::MqClient

服务端

#include <rmvl/io/ipc.hpp>
using namespace rm;
int main() {
MqServer server("/myqueue");
bool success = server.send("message");
if (!success) {
printf("Send failed\n");
return -1;
}
std::string msg = server.receive();
if (msg.empty()) {
printf("Receive failed\n");
return -1;
}
printf("Received: %s\n", msg.c_str());
}
rm::MqServer
消息队列服务端
定义 ipc.hpp:130

客户端

#include <rmvl/io/ipc.hpp>
using namespace rm;
int main() {
MqClient client("/myqueue");
std::string msg = client.receive();
if (msg.empty()) {
printf("Receive failed\n");
return -1;
}
printf("Received: %s\n", msg.c_str());
bool success = client.send("received: " + msg);
if (!success) {
printf("Send failed\n");
return -1;
}
}
rm::MqClient
消息队列客户端
定义 ipc.hpp:181

# 3. Unix Domain Socket

相关类：

- 同步： rm::Socket, rm::Acceptor 及 rm::Connector
- 异步： rm::async::Socket, rm::async::Acceptor 及 rm::async::Connector

一般工程使用中，推荐客户端使用同步阻塞方式进行通信，服务端使用异步非阻塞方式进行通信，以兼顾高并发性能。有关网络 Socket 通信的内容可参考 传输层设施 —— Socket

## 3.1 同步阻塞

服务端

#include <rmvl/io/socket.hpp>
using namespace rm;
int main() {
Acceptor acceptor(Endpoint(ipc::stream(), "rmvl_test.sock"));
auto socket = acceptor.accept();
std::string msg = socket.read();
if (msg.empty()) {
printf("Read failed\n");
return -1;
}
printf("Received: %s\n", msg.c_str());
bool success = socket.write("received: " + msg);
if (!success) {
printf("Write failed\n");
return -1;
}
}
rm::Acceptor
Socket 接受器
定义 socket.hpp:218
rm::Endpoint
端点
定义 socket.hpp:91
socket.hpp
以及基于 socket 的同步/异步 IPC 通信、传输层与会话层通信框架
rm::ipc::stream
static ipc stream()
构造端点，以表示本地流式协议

客户端

#include <rmvl/io/socket.hpp>
using namespace rm;
int main() {
Connector connector(Endpoint(ipc::stream(), "rmvl_test.sock"));
auto socket = connector.connect();
bool success = socket.write("message");
if (!success) {
printf("Write failed\n");
return -1;
}
std::string response = socket.read();
if (response.empty()) {
printf("Read failed\n");
return -1;
}
printf("Received: %s\n", response.c_str());
}
rm::Connector
Socket 连接器
定义 socket.hpp:257

## 3.2 异步非阻塞

服务端

#include <rmvl/io/socket.hpp>
using namespace rm;
async::Task<> session(async::Socket socket) {
std::string msg = co_await socket.read();
if (msg.empty()) {
printf("Read failed\n");
co_return;
}
printf("Received: %s\n", msg.c_str());
bool success = co_await socket.write("received: " + msg);
if (!success) {
printf("Write failed\n");
co_return;
}
}
int main() {
async::IOContext io_context{};
async::Acceptor acceptor(Endpoint(ipc::stream(), "rmvl_test.sock"));
// 持续接受连接
co_spawn(io_context, [&]() -> async::Task<> {
while (true) {
auto socket = co_await acceptor.accept();
co_spawn(io_context, session, std::move(socket));
}
});
// 此处可以使用 co_spawn 注册其他 I/O 任务
io_context.run();
return 0;
}
rm::async::Acceptor
异步 Socket 接受器
定义 socket.hpp:391
rm::async::Socket
Socket 异步会话层
定义 socket.hpp:297
rm::async::Socket::write
SocketWriteAwaiter write(std::string_view data)
异步写入数据到已连接的 Socket 中
定义 socket.hpp:376
rm::async::Socket::read
SocketReadAwaiter read()
异步读取已连接的 Socket 中的数据
定义 socket.hpp:343

客户端

#include <rmvl/io/socket.hpp>
using namespace rm;
async::Task<> session(async::Connector &connector) {
auto socket = co_await connector.connect();
bool success = co_await socket.write("message");
if (!success) {
printf("Write failed\n");
co_return;
}
std::string response = co_await socket.read();
if (response.empty()) {
printf("Read failed\n");
co_return;
}
printf("Received: %s\n", response.c_str());
}
int main() {
async::IOContext io_context{};
async::Connector connector(Endpoint(ipc::stream(), "rmvl_test.sock"));
co_spawn(io_context, session, std::ref(connector));
// 此处可以使用 co_spawn 注册其他 I/O 任务
io_context.run();
return 0;
}
rm::async::Connector
异步 Socket 连接器
定义 socket.hpp:441
rm::async::Connector::connect
ConnectAwaiter connect()
异步连接
定义 socket.hpp:480