brpc/docs/cn/endpoint.md
2022-12-14 20:13:26 +08:00

61 lines
2.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# UDS及IPV6支持
butil::EndPoint已经支持UDS(Unix Domain Socket)及IPV6。
## 基本用法
代码用法:
```cpp
EndPoint ep;
str2endpoint("unix:path.sock", &ep); // 初始化一个UDS的EndPoint
str2endpoint("[::1]:8086", &ep); // 初始化一个IPV6的EndPoint
str2endpoint("[::1]", 8086, &ep); // 初始化一个IPV6的EndPoint
// 获取EndPoint的类型
sa_family_t type = get_endpoint_type(ep); // 可能为AF_INET、AF_INET6或AF_UNIX
// 使用EndPoint和原来的方式一样
LOG(DEBUG) << ep; // 打印EndPoint
std::string ep_str = endpoint2str(ep).c_str(); // EndPoint转str
tcp_listen(ep); // 用监听EndPoint表示的tcp端口
tcp_connect(ep, NULL); // 用连接EndPoint表示的tcp端口
sockaddr_storage ss;
socklen_t socklen = 0;
endpoint2sockaddr(ep, &ss, &socklen); // 将EndPoint转为sockaddr结构以便调用系统函数
```
## 在brpc中使用UDS或IPV6
只需要在原来输入IPV4字符串的时候填写UDS路径或IPV6地址即可
```cpp
server.Start("unix:path.sock", options); // 启动server监听UDS地址
server.Start("[::0]:8086", options); // 启动server监听IPV6地址
channel.Init("unix:path.sock", options); // 初始化single server的Channel访问UDS地址
channel.Init("list://[::1]:8086,[::1]:8087", "rr", options); // 初始化带LB的Channel访问IPV6地址
```
通过 example/echo_c++ 展示了如何使用UDS或IPV6
```bash
./echo_server -listen_addr='unix:path.sock' & # 启动Server监听UDS地址
./echo_server -listen_addr='[::0]:8080' & # 启动Server监听IPV6端口
./echo_client -server='unix:path.sock' # 启动Client访问UDS地址
./echo_client -server='[::1]:8080' # 启动Client访问IPV6端口
```
## 限制
由于EndPoint结构被广泛地使用为了保证对存量代码的兼容性包括ABI兼容性目前采用的实现方式是不修改EndPoint的ABI定义使用原来的ip字段作为idport字段来做为扩展标记把真正的信息存在一个外部的数据结构中。
这种实现方式对于现存的仅使用IPV4的代码是完全兼容的但对于使用UDS或IPV6的用户有些代码是不兼容的比如直接访问EndPoint的ip和port成员的代码。
关于UDS和IPV6目前已知的一些限制
- 不兼容rpcz
- 不支持使用PortRange方式启动server
- 不支持在ServerOption中指定internal_port
- IPV6不支持link local地址fe80::开头的地址)