RMVL  2.3.0
Robotic Manipulation and Vision Library
载入中...
搜索中...
未找到
网络、应用层设施

作者
赵曦
日期
2025/09/25
版本
1.0

上一篇教程:传输层设施 —— Socket
下一篇教程:工业自动化通信协议 —— OPC UA


相关类

1. HTTP 数据结构

相关类: rm::Requestrm::Response

两个类均提供了

  • generate 成员函数,用于从请求/响应结构生成 HTTP 格式的字符串,在调用此方法后可直接通过 Socket 发送数据
  • parse 静态成员函数,用于从 HTTP 格式的字符串解析出请求/响应结构,在接收到 Socket 原始数据后可调用此方法进行解析

rm::Response 还提供了多种简化生成响应的成员函数,如 sendjsonredirect 等。

2. HTTP 请求工具

相关函数:

设计理念参考 Python 的 requests 库,提供了极其简洁易用的接口,这里给出一个简单的 GET 请求示例:

#include <rmvl/io/webapp.hpp>
using namespace rm;
int main() {
// 发送 GET 请求
auto response = requests::get("http://example.com");
if (response) {
printf("Response: %s\n", response->body().c_str());
}
}
Response get(std::string_view url, const std::vector< std::string > &querys={}, const std::unordered_map< std::string, std::string > &heads={})
发出同步 GET 请求
定义 netapp.hpp:210
定义 datastruct.hpp:20

异步请求的使用方法类似,只需将 requests 替换为 async::requests,并在协程中使用 co_await 关键字等待结果,这里不再赘述。

3. Web 后端框架

RMVL 提供了 HTTP 的后端应用框架 rm::async::Webapp 。这是 数据 IO 与通信模块 的集大成者,设计理念参考 JavaScript 的 Express 框架,这里给出一个简单的 Web 服务器示例:

// demo.cpp
#include <iostream>
using namespace rm;
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <port>" << std::endl;
return 1;
}
uint16_t port = static_cast<uint16_t>(std::atoi(argv[1]));
async::IOContext io_context{};
async::Webapp app(io_context);
app.use(cors);
app.get("/", [](const Request &req, Response &res) {
res.send("<html><body><h1>Hello, World!</h1></body></html>");
});
app.get("/test", [](const Request &req, Response &res) {
res.redirect("/");
});
app.get("/str", [](const Request &req, Response &res) {
res.send("only string");
});
app.get("/api", [](const Request &req, Response &res) {
res.json("{ \"key\": \"value\", \"message\": \"This is a test API.\" }");
});
app.get("/api/name/:name", [](const Request &req, Response &res) {
auto name = req.params.at("name");
res.json("{ \"greeting\": \"Hello, " + name + "!\" }");
});
app.listen(port, [&]() {
std::cout << "Server is running on port " << port << std::endl;
});
co_spawn(io_context, &async::Webapp::spin, &app);
io_context.run();
}
异步 I/O 执行上下文,负责管理 IO 事件循环和协程任务的调度
定义 async.hpp:177
Web 应用程序框架
定义 netapp.hpp:325
Task spin()
启动事件循环
void cors(Response &res)
跨域资源共享 CORS 中间件,为响应添加 CORS 头部信息
void co_spawn(IOContext &ctx, Callable &&fn, Args &&...args)
在指定的执行上下文中生成并调度一个协程任务
定义 async.hpp:265
异步 IO 的应用层网络通信框架
HTTP 请求
定义 netapp.hpp:73
std::unordered_map< std::string, std::string > params
路径参数
定义 netapp.hpp:92
HTTP 响应
定义 netapp.hpp:105
void json(std::string_view str)
发送 JSON 格式的响应数据
void redirect(std::string_view url)
发送重定向响应
定义 netapp.hpp:147
void send(std::string_view str)
发送响应数据

可以简单使用以下命令行进行编译

g++ demo.cpp -std=c++20 -I /usr/local/include/RMVL -l rmvl_io -l rmvl_core -o demo
# 如果使用的是 Windows 系统,可能需要额外链接 ws2_32 库

编译后,在终端输入

./demo 8080

即可创建 Web 后端服务,并且监听 8080 端口,可以使用 curl 工具或浏览器访问 http://localhost:8080/ 来测试服务器。

curl -s "http://localhost:8080"

会得到如下输出:

<html><body><h1>Hello, World!</h1></body></html>

同时,可以配合 jq 工具测试 JSON API:

curl -s "http://localhost:8080/api" | jq

会得到如下输出:

{
  "key": "value"
  "message": "This is a test API."
}