|
|
| tcp_socket ()=default |
| | 默认构造函数
|
|
| tcp_socket (tcp_socket &&)=default |
| | 移动构造函数
|
|
tcp_socket & | operator= (tcp_socket &&)=default |
| | 移动赋值运算符
|
| | tcp_socket (native_handle_type fd) |
| | 从原生句柄构造
|
|
| ~tcp_socket () override=default |
| | 析构函数
|
| void | open (family f=family::INET4) |
| | 打开TCP socket
|
| bool | connect (const ip_address &endpoint, milliseconds timeout, bool was_blocking=true) |
| | 连接到远程服务器
|
| virtual ssize_t | send (memory_view< const char > data, int flags=0) |
| | 发送数据
|
| ssize_t | send (memory_view< const char > data, milliseconds timeout, int flags=0) |
| | 带超时的发送数据
|
| void | send_all (memory_view< const char > data) |
| | 发送所有数据
|
| virtual ssize_t | receive (memory_view< char > buffer, int flags=0) |
| | 接收数据
|
| vector< char > | receive_all (size_t expected_size) |
| | 接收指定大小的所有数据
|
| virtual bool | is_ssl () const noexcept |
| | 检查是否为SSL/TLS socket
|
| void | async_connect (io_context &ctx, const ip_address &endpoint, function< void(error_code)> handler) |
| | 异步连接到远程端点
|
| void | async_connect (io_context &ctx, const ip_address &endpoint, cancellation_slot &slot, function< void(error_code)> handler) |
| | 异步连接(带取消槽)
|
| void | async_read (io_context &ctx, memory_view< char > buffer, function< void(error_code, size_t)> handler) override |
| | 异步读取数据
|
| void | async_read (io_context &ctx, memory_view< char > buffer, cancellation_slot &slot, function< void(error_code, size_t)> handler) override |
| | 异步读取(带取消槽)
|
| void | async_write (io_context &ctx, memory_view< const char > buffer, function< void(error_code, size_t)> handler) override |
| | 异步写入数据
|
| void | async_write (io_context &ctx, memory_view< const char > buffer, cancellation_slot &slot, function< void(error_code, size_t)> handler) override |
| | 异步写入(带取消槽)
|
| void | async_receive (io_context &ctx, memory_view< char > buffer, function< void(error_code, size_t)> handler) |
| | 异步接收(转发到 async_read)
|
| void | async_receive (io_context &ctx, memory_view< char > buffer, cancellation_slot &slot, function< void(error_code, size_t)> handler) |
| | 异步接收(带取消槽,转发到 async_read)
|
| void | async_send (io_context &ctx, memory_view< const char > buffer, function< void(error_code, size_t)> handler) |
| | 异步发送(转发到 async_write)
|
| void | async_send (io_context &ctx, memory_view< const char > buffer, cancellation_slot &slot, function< void(error_code, size_t)> handler) |
| | 异步发送(带取消槽,转发到 async_write)
|
| template<typename Token, enable_if_t<!is_same_v< decay_t< Token >, function< void(error_code, size_t)> >, int > = 0> |
| decltype(auto) | async_receive (io_context &ctx, memory_view< char > buffer, Token &&token) |
| | 异步接收—模板令牌版本(转发到 async_read)
|
| template<typename Token, enable_if_t<!is_same_v< decay_t< Token >, function< void(error_code, size_t)> >, int > = 0> |
| decltype(auto) | async_send (io_context &ctx, memory_view< const char > buffer, Token &&token) |
| | 异步发送—模板令牌版本(转发到 async_write)
|
| template<typename Token, enable_if_t<!is_same_v< decay_t< Token >, function< void(error_code)> >, int > = 0> |
| void | async_connect (io_context &ctx, const ip_address &endpoint, Token &&token) |
| | 异步连接—任意可调用对象版本
|
| auto | async_connect (io_context &ctx, const ip_address &endpoint, use_future_t) |
| | 异步连接—use_future
|
| void | async_connect (io_context &ctx, const ip_address &endpoint, detached_t) |
| | 异步连接—detached(即发即忘)
|
| auto | async_connect (io_context &ctx, const ip_address &endpoint, use_awaitable_t) |
| | 异步连接—use_awaitable
|
|
| ip_socket ()=default |
| | 默认构造函数
|
| | ip_socket (const native_handle_type fd) noexcept |
| | 从原生句柄构造
|
|
| ~ip_socket () override=default |
| | 析构函数
|
|
family | address_family () const noexcept |
| | 获取地址族
|
| bool | is_ipv4 () const noexcept |
| | 检查是否为IPv4 socket
|
| bool | is_ipv6 () const noexcept |
| | 检查是否为IPv6 socket
|
| virtual void | connect (const ip_address &endpoint) |
| | 连接到远程端点(TCP客户端)
|
| bool | close () noexcept override |
| | 关闭socket
|
|
| socket_base () |
| | 默认构造函数
|
| | socket_base (const native_handle_type fd) noexcept |
| | 从原生句柄构造
|
| | socket_base (socket_base &&other) noexcept |
| | 移动构造函数
|
| socket_base & | operator= (socket_base &&other) noexcept |
| | 移动赋值运算符
|
|
virtual | ~socket_base () |
| | 析构函数
|
| native_handle_type | native_handle () const noexcept |
| | 获取原生句柄
|
| bool | is_open () const noexcept |
| | 检查socket是否已打开
|
| | operator bool () const noexcept |
| | 布尔转换运算符
|
| void | open (family f, type t=type::STREAM, protocol p=protocol::AUTO) |
| | 打开socket
|
| bool | try_open (family f, type t=type::STREAM, protocol p=protocol::AUTO) noexcept |
| | 尝试打开socket
|
| bool | is_nonblocking () const noexcept |
| | 查询是否为非阻塞模式
|
| bool | set_nonblocking (bool enable) noexcept |
| | 设置非阻塞模式
|
| bool | shutdown_send () noexcept |
| | 关闭发送方向
|
| bool | shutdown_receive () noexcept |
| | 关闭接收方向
|
| bool | shutdown_both () noexcept |
| | 关闭双向通信
|
| bool | set_option (int level, int optname, const void *value, ::socklen_t len) noexcept |
| | 设置socket选项
|
| bool | get_option (int level, int optname, void *optval, ::socklen_t *optlen) const noexcept |
| | 获取socket选项
|
| bool | get_reuse_address () const noexcept |
| | 查询地址重用
|
| bool | set_reuse_address (bool enable=true) noexcept |
| | 设置地址重用
|
| bool | get_reuse_port () const noexcept |
| | 查询端口重用
|
| bool | set_reuse_port (bool enable=true) noexcept |
| | 设置端口重用
|
| bool | get_keep_alive () const noexcept |
| | 查询TCP KeepAlive
|
| bool | set_keep_alive (bool enable=true) noexcept |
| | 设置TCP KeepAlive
|
| bool | get_tcp_nodelay () const noexcept |
| | 查询Nagle算法
|
| bool | set_tcp_nodelay (bool enable=true) noexcept |
| | 设置禁用Nagle算法
|
| int | get_receive_buffer_size () const noexcept |
| | 查询接收缓冲区大小
|
| bool | set_receive_buffer_size (int size) noexcept |
| | 设置接收缓冲区大小
|
| int | get_send_buffer_size () const noexcept |
| | 查询发送缓冲区大小
|
| bool | set_send_buffer_size (int size) noexcept |
| | 设置发送缓冲区大小
|
| optional< milliseconds > | get_send_timeout () const noexcept |
| | 获取发送超时时间
|
| bool | set_send_timeout (milliseconds timeout) noexcept |
| | 设置发送超时时间
|
| optional< milliseconds > | get_receive_timeout () const noexcept |
| | 获取接收超时时间
|
| bool | set_receive_timeout (milliseconds timeout) noexcept |
| | 设置接收超时时间
|
| optional< ip_address > | local_endpoint () const |
| | 获取本地端点地址
|
| optional< ip_address > | remote_endpoint () const |
| | 获取远程端点地址
|
| void | bind (const ip_address &endpoint) |
| | 绑定socket到本地地址
|
| void | listen (int backlog) |
| | 开始监听连接(TCP)
|
| native_handle_type | release () noexcept |
| | 释放socket所有权
|
|
virtual | ~async_stream ()=default |
| | 析构函数
|
| template<typename Token, enable_if_t<!is_same_v< decay_t< Token >, function< void(error_code, size_t)> >, int > = 0> |
| void | async_read (io_context &ctx, memory_view< char > buffer, Token &&token) |
| | 异步读取—任意可调用对象
|
| auto | async_read (io_context &ctx, memory_view< char > buffer, use_future_t) |
| | 异步读取—use_future
|
| void | async_read (io_context &ctx, memory_view< char > buffer, detached_t) |
| | 异步读取—detached(即发即忘)
|
| auto | async_read (io_context &ctx, memory_view< char > buffer, use_awaitable_t) |
| | 异步读取—use_awaitable
|
| template<typename Token, enable_if_t<!is_same_v< decay_t< Token >, function< void(error_code, size_t)> >, int > = 0> |
| void | async_write (io_context &ctx, memory_view< const char > buffer, Token &&token) |
| | 异步写入—任意可调用对象
|
| auto | async_write (io_context &ctx, memory_view< const char > buffer, use_future_t) |
| | 异步写入—use_future
|
| void | async_write (io_context &ctx, memory_view< const char > buffer, detached_t) |
| | 异步写入—detached(即发即忘)
|
| auto | async_write (io_context &ctx, memory_view< const char > buffer, use_awaitable_t) |
| | 异步写入—use_awaitable
|
| void | async_read (io_context &ctx, mutable_buffers &bufs, function< void(error_code, size_t)> handler) |
| | 异步读取—scatter-gather
|
| void | async_write (io_context &ctx, const_buffers &bufs, function< void(error_code, size_t)> handler) |
| | 异步写入—scatter-gather
|
| void | async_read (io_context &ctx, dynamic_buffer &buf, size_t n, function< void(error_code, size_t)> handler) |
| | 异步读取到 dynamic_buffer
|
TCP Socket类
实现TCP协议的流式socket, TCP是面向连接的协议,提供可靠的数据传输、流量控制和拥塞控制。
主要功能:
- TCP socket创建和打开
- 连接建立(支持超时)
- 数据发送(普通、带超时、全部发送)
- 数据接收(普通、全部接收)
- SSL/TLS支持接口
使用示例:
string request = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n";
char buffer[4096];
if (received > 0) {
}
}
server.open(AF_INET);
server.bind(local);
server.listen(128);
auto client_sock = server.accept();
constexpr view_type view() const noexcept
获取字符串视图
static optional< ip_address > parse(const string &host, ports port=ports{}) noexcept
从字符串解析IP地址
static ip_address any(ports port=ports::UNDEF, family f=family::INET4) noexcept
获取通配地址
void open(family f=family::INET4)
打开TCP socket
tcp_socket()=default
默认构造函数
void send_all(memory_view< const char > data)
发送所有数据
virtual ssize_t receive(memory_view< char > buffer, int flags=0)
接收数据
bool connect(const ip_address &endpoint, milliseconds timeout, bool was_blocking=true)
连接到远程服务器
void println(Args &&... args)
打印多个值并换行
duration< int64_t, milli > milliseconds
毫秒持续时间
basic_string_view< char > string_view
字符字符串视图
在文件 tcp_socket.hpp 第 69 行定义.