Socket基础类
更多...
#include <socket_base.hpp>
|
|
using | native_handle_type |
| | 平台原生句柄类型
|
|
|
| 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 (int family, int type, int protocol) |
| | 打开socket
|
| virtual bool | close () noexcept |
| | 关闭socket
|
| bool | try_open (int family, int type, int protocol) noexcept |
| | 尝试打开socket(不抛出异常)
|
| 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 | set_reuse_address (bool enable=true) noexcept |
| | 设置地址重用(SO_REUSEADDR)
|
| bool | set_reuse_port (bool enable=true) noexcept |
| | 设置端口重用(SO_REUSEPORT)
|
| bool | set_keep_alive (bool enable=true) noexcept |
| | 设置TCP KeepAlive
|
| bool | set_tcp_nodelay (bool enable=true) noexcept |
| | 设置TCP_NODELAY(禁用Nagle算法)
|
| bool | set_receive_buffer_size (int size) noexcept |
| | 设置接收缓冲区大小
|
| bool | set_send_buffer_size (int size) noexcept |
| | 设置发送缓冲区大小
|
| bool | set_send_timeout (milliseconds timeout) 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所有权
|
Socket基础类
提供跨平台的socket基础操作,是所有socket类的基类。 支持移动语义,不支持拷贝。
主要功能:
- Socket创建和关闭
- 阻塞/非阻塞模式设置
- Socket选项配置(地址重用、端口重用、TCP_NODELAY、KeepAlive等)
- 收发缓冲区大小设置
- 超时设置
- 本地/远程端点查询
- 绑定和监听
- 移动语义支持
使用示例:
sock.
open(AF_INET, SOCK_STREAM, 0);
if (local) {
}
static ip_address any(ports port=ports::UNDEF, int family=AF_INET) noexcept
获取通配地址
bool set_tcp_nodelay(bool enable=true) noexcept
设置TCP_NODELAY(禁用Nagle算法)
optional< ip_address > local_endpoint() const
获取本地端点地址
bool set_reuse_address(bool enable=true) noexcept
设置地址重用(SO_REUSEADDR)
void bind(const ip_address &endpoint)
绑定socket到本地地址
bool set_nonblocking(bool enable) noexcept
设置非阻塞模式
void listen(int backlog)
开始监听连接(TCP)
void open(int family, int type, int protocol)
打开socket
void println(Args &&... args)
打印多个值并换行
在文件 socket_base.hpp 第 100 行定义.
◆ socket_base() [1/2]
◆ socket_base() [2/2]
◆ bind()
| void socket_base::bind |
( |
const ip_address & | endpoint | ) |
|
◆ close()
| virtual bool socket_base::close |
( |
| ) |
|
|
virtualnoexcept |
◆ get_option()
| bool socket_base::get_option |
( |
int | level, |
|
|
int | optname, |
|
|
void * | optval, |
|
|
::socklen_t * | optlen ) const |
|
noexcept |
◆ is_open()
| bool socket_base::is_open |
( |
| ) |
const |
|
inlinenodiscardnoexcept |
◆ listen()
| void socket_base::listen |
( |
int | backlog | ) |
|
◆ local_endpoint()
◆ native_handle()
◆ open()
| void socket_base::open |
( |
int | family, |
|
|
int | type, |
|
|
int | protocol ) |
打开socket
- 参数
-
| family | 地址族(AF_INET或AF_INET6) |
| type | socket类型(SOCK_STREAM、SOCK_DGRAM等) |
| protocol | 协议(0为自动选择) |
- 异常
-
◆ operator bool()
| socket_base::operator bool |
( |
| ) |
const |
|
inlineexplicitnoexcept |
◆ operator=()
◆ release()
◆ remote_endpoint()
◆ set_keep_alive()
| bool socket_base::set_keep_alive |
( |
bool | enable = true | ) |
|
|
noexcept |
◆ set_nonblocking()
| bool socket_base::set_nonblocking |
( |
bool | enable | ) |
|
|
noexcept |
◆ set_option()
| bool socket_base::set_option |
( |
int | level, |
|
|
int | optname, |
|
|
const void * | value, |
|
|
::socklen_t | len ) |
|
noexcept |
设置socket选项
- 参数
-
| level | 协议层(SOL_SOCKET、IPPROTO_TCP等) |
| optname | 选项名 |
| value | 选项值指针 |
| len | 值长度 |
- 返回
- 设置成功返回true
引用了 set_option().
被这些函数引用 set_option().
◆ set_receive_buffer_size()
| bool socket_base::set_receive_buffer_size |
( |
int | size | ) |
|
|
noexcept |
◆ set_receive_timeout()
| bool socket_base::set_receive_timeout |
( |
milliseconds | timeout | ) |
|
|
noexcept |
◆ set_reuse_address()
| bool socket_base::set_reuse_address |
( |
bool | enable = true | ) |
|
|
noexcept |
◆ set_reuse_port()
| bool socket_base::set_reuse_port |
( |
bool | enable = true | ) |
|
|
noexcept |
◆ set_send_buffer_size()
| bool socket_base::set_send_buffer_size |
( |
int | size | ) |
|
|
noexcept |
◆ set_send_timeout()
◆ set_tcp_nodelay()
| bool socket_base::set_tcp_nodelay |
( |
bool | enable = true | ) |
|
|
noexcept |
◆ shutdown_both()
| bool socket_base::shutdown_both |
( |
| ) |
|
|
noexcept |
◆ shutdown_receive()
| bool socket_base::shutdown_receive |
( |
| ) |
|
|
noexcept |
◆ shutdown_send()
| bool socket_base::shutdown_send |
( |
| ) |
|
|
noexcept |
◆ try_open()
| bool socket_base::try_open |
( |
int | family, |
|
|
int | type, |
|
|
int | protocol ) |
|
noexcept |
尝试打开socket(不抛出异常)
- 参数
-
| family | 地址族 |
| type | socket类型 |
| protocol | 协议 |
- 返回
- 打开成功返回true
引用了 try_open().
被这些函数引用 try_open().
◆ invalid_handle
该类的文档由以下文件生成: