NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
neforce::socket_base类 参考

Socket基础类 更多...

#include <socket_base.hpp>

类 neforce::socket_base 继承关系图:
[图例]

Public 类型

enum class  type : int32_t {
  STREAM = SOCK_STREAM , DGRAM = SOCK_DGRAM , RAW = SOCK_RAW , RDM = SOCK_RDM ,
  SEQPACKET = SOCK_SEQPACKET , CLOEXEC = SOCK_CLOEXEC , NONBLOCK = SOCK_NONBLOCK , DCCP = SOCK_DCCP
}
 socket 类型枚举 更多...
enum class  protocol : int32_t {
  AUTO = 0 , ICMP = IPPROTO_ICMP , TCP = IPPROTO_TCP , UDP = IPPROTO_UDP ,
  SCTP = IPPROTO_SCTP , RAW = IPPROTO_RAW , ESP = IPPROTO_ESP , AH = IPPROTO_AH ,
  DCCP = IPPROTO_DCCP , IP = ETH_P_IP , ARP = ETH_P_ARP , IPV6 = ETH_P_IPV6 ,
  LAT = ETH_P_LAT , VLAN = ETH_P_8021Q , LLDP = ETH_P_LLDP , MPLS = ETH_P_MPLS_UC
}
 socket 协议枚举 更多...
using native_handle_type
 平台原生句柄类型
using family = ip_address::family
 网络地址族类型

Public 成员函数

 socket_base ()
 默认构造函数
 socket_base (const native_handle_type fd) noexcept
 从原生句柄构造
 socket_base (socket_base &&other) noexcept
 移动构造函数
socket_baseoperator= (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
virtual bool close () noexcept
 关闭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< millisecondsget_send_timeout () const noexcept
 获取发送超时时间
bool set_send_timeout (milliseconds timeout) noexcept
 设置发送超时时间
optional< millisecondsget_receive_timeout () const noexcept
 获取接收超时时间
bool set_receive_timeout (milliseconds timeout) noexcept
 设置接收超时时间
optional< ip_addresslocal_endpoint () const
 获取本地端点地址
optional< ip_addressremote_endpoint () const
 获取远程端点地址
void bind (const ip_address &endpoint)
 绑定socket到本地地址
void listen (int backlog)
 开始监听连接(TCP)
native_handle_type release () noexcept
 释放socket所有权

静态 Public 属性

static constexpr native_handle_type invalid_handle
 无效句柄常量

Protected 属性

native_handle_type fd_ = invalid_handle
 Socket句柄
bool nonblocking_ {false}
 非阻塞模式状态跟踪

详细描述

Socket基础类

提供跨平台的socket基础操作,是所有socket类的基类。 支持移动语义,不支持拷贝。

主要功能:

  • Socket创建和关闭
  • 阻塞/非阻塞模式设置
  • Socket选项配置(地址重用、端口重用、TCP_NODELAY、KeepAlive等)
  • 收发缓冲区大小设置
  • 超时设置
  • 本地/远程端点查询
  • 绑定和监听
  • 移动语义支持

在文件 socket_base.hpp81 行定义.

成员枚举类型说明

◆ protocol

socket 协议枚举

封装了底层协议标识常量,用于指定套接字使用的网络协议类型。

枚举值
AUTO 

自动选择协议

ICMP 

Internet 控制报文协议

TCP 

传输控制协议

UDP 

用户数据报协议

SCTP 

流控制传输协议

RAW 

原始 IP 数据包

ESP 

IPsec 封装安全载荷

AH 

IPsec 认证头

DCCP 

数据报拥塞控制协议

IP 

IPv4 以太网帧类型

ARP 

地址解析协议帧类型

IPV6 

IPv6 以太网帧类型

LAT 

局域传输协议

VLAN 

IEEE 802.1Q VLAN 标签帧

LLDP 

链路层发现协议

MPLS 

多协议标签交换单播

在文件 socket_base.hpp123 行定义.

◆ type

enum class neforce::socket_base::type : int32_t
strong

socket 类型枚举

枚举值
STREAM 

流式套接字(TCP)

DGRAM 

数据报套接字(UDP)

RAW 

原始套接字

RDM 

可靠数据报套接字

SEQPACKET 

有序分组套接字(SCTP)

CLOEXEC 

执行时关闭标志

NONBLOCK 

非阻塞标志

DCCP 

数据报拥塞控制协议(DCCP)

在文件 socket_base.hpp105 行定义.

构造及析构函数说明

◆ socket_base() [1/2]

neforce::socket_base::socket_base ( const native_handle_type fd)
inlineexplicitnoexcept

从原生句柄构造

参数
fd原生socket句柄

在文件 socket_base.hpp159 行定义.

引用了 fd_.

◆ socket_base() [2/2]

neforce::socket_base::socket_base ( socket_base && other)
inlinenoexcept

移动构造函数

参数
other源对象

在文件 socket_base.hpp169 行定义.

引用了 fd_, invalid_handle, nonblocking_ , 以及 socket_base().

成员函数说明

◆ bind()

void neforce::socket_base::bind ( const ip_address & endpoint)

绑定socket到本地地址

参数
endpoint要绑定的IP地址和端口
异常
socket_exception绑定失败时抛出
value_exceptionsocket未打开或端点无效时抛出

引用了 bind().

被这些函数引用 bind().

◆ close()

virtual bool neforce::socket_base::close ( )
virtualnoexcept

关闭socket

返回
关闭成功返回true

neforce::ip_socket , 以及 neforce::ssl_socket 重载.

被这些函数引用 neforce::ip_socket::close() , 以及 ~socket_base().

◆ get_keep_alive()

bool neforce::socket_base::get_keep_alive ( ) const
nodiscardnoexcept

查询TCP KeepAlive

返回
已启用返回true,失败返回false

引用了 get_keep_alive().

被这些函数引用 get_keep_alive().

◆ get_option()

bool neforce::socket_base::get_option ( int level,
int optname,
void * optval,
::socklen_t * optlen ) const
noexcept

获取socket选项

参数
level协议层
optname选项名
optval输出缓冲区
optlen输入输出长度
返回
获取成功返回true

引用了 get_option().

被这些函数引用 get_option().

◆ get_receive_buffer_size()

int neforce::socket_base::get_receive_buffer_size ( ) const
nodiscardnoexcept

查询接收缓冲区大小

返回
缓冲区大小(字节),失败返回-1

引用了 get_receive_buffer_size().

被这些函数引用 get_receive_buffer_size().

◆ get_receive_timeout()

optional< milliseconds > neforce::socket_base::get_receive_timeout ( ) const
nodiscardnoexcept

获取接收超时时间

返回
超时时间,失败返回none

引用了 get_receive_timeout().

被这些函数引用 get_receive_timeout().

◆ get_reuse_address()

bool neforce::socket_base::get_reuse_address ( ) const
nodiscardnoexcept

查询地址重用

返回
已启用返回true,失败返回false

引用了 get_reuse_address().

被这些函数引用 get_reuse_address().

◆ get_reuse_port()

bool neforce::socket_base::get_reuse_port ( ) const
nodiscardnoexcept

查询端口重用

返回
已启用返回true,失败或平台不支持返回false

引用了 get_reuse_port().

被这些函数引用 get_reuse_port().

◆ get_send_buffer_size()

int neforce::socket_base::get_send_buffer_size ( ) const
nodiscardnoexcept

查询发送缓冲区大小

返回
缓冲区大小(字节),失败返回-1

引用了 get_send_buffer_size().

被这些函数引用 get_send_buffer_size().

◆ get_send_timeout()

optional< milliseconds > neforce::socket_base::get_send_timeout ( ) const
nodiscardnoexcept

获取发送超时时间

返回
超时时间,失败返回none

引用了 get_send_timeout().

被这些函数引用 get_send_timeout().

◆ get_tcp_nodelay()

bool neforce::socket_base::get_tcp_nodelay ( ) const
nodiscardnoexcept

查询Nagle算法

返回
已禁用Nagle返回true,失败返回false

引用了 get_tcp_nodelay().

被这些函数引用 get_tcp_nodelay().

◆ is_nonblocking()

bool neforce::socket_base::is_nonblocking ( ) const
nodiscardnoexcept

查询是否为非阻塞模式

返回
非阻塞返回true

引用了 is_nonblocking().

被这些函数引用 is_nonblocking().

◆ is_open()

bool neforce::socket_base::is_open ( ) const
inlinenodiscardnoexcept

检查socket是否已打开

返回
已打开返回true

在文件 socket_base.hpp195 行定义.

引用了 fd_ , 以及 invalid_handle.

被这些函数引用 neforce::smtp_socket::is_connected() , 以及 operator bool().

◆ listen()

void neforce::socket_base::listen ( int backlog)

开始监听连接(TCP)

参数
backlog连接队列大小
异常
socket_exception监听失败时抛出
value_exceptionsocket未打开时抛出

引用了 listen().

被这些函数引用 listen().

◆ local_endpoint()

optional< ip_address > neforce::socket_base::local_endpoint ( ) const
nodiscard

获取本地端点地址

返回
本地IP地址和端口,失败返回none

引用了 local_endpoint().

被这些函数引用 local_endpoint().

◆ native_handle()

native_handle_type neforce::socket_base::native_handle ( ) const
inlinenodiscardnoexcept

获取原生句柄

返回
原生socket句柄

在文件 socket_base.hpp189 行定义.

引用了 fd_.

◆ open()

void neforce::socket_base::open ( family f,
type t = type::STREAM,
protocol p = protocol::AUTO )

打开socket

参数
f地址族
tsocket类型
p协议
异常
socket_exception创建失败时抛出
value_exception地址族无效时抛出

引用了 AUTO , 以及 STREAM.

◆ operator bool()

neforce::socket_base::operator bool ( ) const
inlineexplicitnoexcept

布尔转换运算符

返回
已打开返回true

在文件 socket_base.hpp201 行定义.

引用了 is_open().

◆ operator=()

socket_base & neforce::socket_base::operator= ( socket_base && other)
noexcept

移动赋值运算符

参数
other源对象
返回
自身引用

引用了 socket_base().

◆ release()

native_handle_type neforce::socket_base::release ( )
inlinenodiscardnoexcept

释放socket所有权

返回
原生socket句柄

释放后当前对象不再管理该socket,调用者负责关闭。

在文件 socket_base.hpp417 行定义.

引用了 neforce::exchange(), fd_, invalid_handle , 以及 release().

被这些函数引用 release().

◆ remote_endpoint()

optional< ip_address > neforce::socket_base::remote_endpoint ( ) const
nodiscard

获取远程端点地址

返回
远程IP地址和端口,失败返回none

引用了 remote_endpoint().

被这些函数引用 remote_endpoint().

◆ set_keep_alive()

bool neforce::socket_base::set_keep_alive ( bool enable = true)
noexcept

设置TCP KeepAlive

参数
enable是否启用
返回
设置成功返回true

引用了 set_keep_alive().

被这些函数引用 set_keep_alive().

◆ set_nonblocking()

bool neforce::socket_base::set_nonblocking ( bool enable)
noexcept

设置非阻塞模式

参数
enable是否启用
返回
设置成功返回true

引用了 set_nonblocking().

被这些函数引用 set_nonblocking().

◆ set_option()

bool neforce::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 neforce::socket_base::set_receive_buffer_size ( int size)
noexcept

设置接收缓冲区大小

参数
size缓冲区大小(字节)
返回
设置成功返回true

引用了 set_receive_buffer_size() , 以及 neforce::size().

被这些函数引用 set_receive_buffer_size().

◆ set_receive_timeout()

bool neforce::socket_base::set_receive_timeout ( milliseconds timeout)
noexcept

设置接收超时时间

参数
timeout超时时间
返回
设置成功返回true

引用了 set_receive_timeout().

被这些函数引用 set_receive_timeout().

◆ set_reuse_address()

bool neforce::socket_base::set_reuse_address ( bool enable = true)
noexcept

设置地址重用

参数
enable是否启用
返回
设置成功返回true

引用了 set_reuse_address().

被这些函数引用 set_reuse_address().

◆ set_reuse_port()

bool neforce::socket_base::set_reuse_port ( bool enable = true)
noexcept

设置端口重用

参数
enable是否启用
返回
设置成功返回true(Windows不支持)

引用了 set_reuse_port().

被这些函数引用 set_reuse_port().

◆ set_send_buffer_size()

bool neforce::socket_base::set_send_buffer_size ( int size)
noexcept

设置发送缓冲区大小

参数
size缓冲区大小(字节)
返回
设置成功返回true

引用了 set_send_buffer_size() , 以及 neforce::size().

被这些函数引用 set_send_buffer_size().

◆ set_send_timeout()

bool neforce::socket_base::set_send_timeout ( milliseconds timeout)
noexcept

设置发送超时时间

参数
timeout超时时间
返回
设置成功返回true

引用了 set_send_timeout().

被这些函数引用 set_send_timeout().

◆ set_tcp_nodelay()

bool neforce::socket_base::set_tcp_nodelay ( bool enable = true)
noexcept

设置禁用Nagle算法

参数
enable是否启用
返回
设置成功返回true

引用了 set_tcp_nodelay().

被这些函数引用 set_tcp_nodelay().

◆ shutdown_both()

bool neforce::socket_base::shutdown_both ( )
noexcept

关闭双向通信

返回
成功返回true

引用了 shutdown_both().

被这些函数引用 shutdown_both().

◆ shutdown_receive()

bool neforce::socket_base::shutdown_receive ( )
noexcept

关闭接收方向

返回
成功返回true

引用了 shutdown_receive().

被这些函数引用 shutdown_receive().

◆ shutdown_send()

bool neforce::socket_base::shutdown_send ( )
noexcept

关闭发送方向

返回
成功返回true

引用了 shutdown_send().

被这些函数引用 shutdown_send().

◆ try_open()

bool neforce::socket_base::try_open ( family f,
type t = type::STREAM,
protocol p = protocol::AUTO )
noexcept

尝试打开socket

参数
f地址族
tsocket类型
p协议
返回
打开成功返回true

引用了 try_open().

被这些函数引用 try_open().

类成员变量说明

◆ invalid_handle

native_handle_type neforce::socket_base::invalid_handle
staticconstexpr
初始值:
=
static constexpr T max() noexcept
获取类型的最大值

无效句柄常量

无效句柄

在文件 socket_base.hpp93 行定义.

被这些函数引用 is_open(), release() , 以及 socket_base().


该类的文档由以下文件生成: