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

UDP Socket类 更多...

#include <udp_socket.hpp>

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

Public 成员函数

 udp_socket ()=default
 默认构造函数
void open (family f=family::INET4)
 打开UDP socket
ssize_t send_to (memory_view< const char > data, const ip_address &endpoint, int flags=0)
 向指定端点发送数据报
ssize_t send (memory_view< const char > data, int flags=0)
 向已连接的端点发送数据
pair< ssize_t, ip_addressreceive_from (memory_view< char > buffer, int flags=0)
 接收数据报并获取发送方地址
ssize_t receive (memory_view< char > buffer, int flags=0)
 从已连接的端点接收数据
void async_receive_from (io_context &ctx, memory_view< char > buffer, function< void(error_code, size_t, ip_address)> handler)
 异步接收数据报
void async_receive_from (io_context &ctx, memory_view< char > buffer, cancellation_slot &slot, function< void(error_code, size_t, ip_address)> handler)
 异步接收数据报(带取消槽)
void async_send_to (io_context &ctx, memory_view< const char > data, const ip_address &endpoint, function< void(error_code, size_t)> handler)
 异步发送数据报
void async_send_to (io_context &ctx, memory_view< const char > data, const ip_address &endpoint, cancellation_slot &slot, function< void(error_code, size_t)> handler)
 异步发送数据报(带取消槽)
template<typename Token, enable_if_t<!is_same_v< decay_t< Token >, function< void(error_code, size_t, ip_address)> >, int > = 0>
void async_receive_from (io_context &ctx, memory_view< char > buffer, Token &&token)
 异步接收数据报—任意可调用对象
auto async_receive_from (io_context &ctx, memory_view< char > buffer, use_future_t)
 异步接收数据报—use_future
void async_receive_from (io_context &ctx, memory_view< char > buffer, detached_t)
 异步接收数据报—detached(即发即忘)
auto async_receive_from (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_send_to (io_context &ctx, memory_view< const char > data, const ip_address &endpoint, Token &&token)
 异步发送数据报—任意可调用对象
auto async_send_to (io_context &ctx, memory_view< const char > data, const ip_address &endpoint, use_future_t)
 异步发送数据报—use_future
void async_send_to (io_context &ctx, memory_view< const char > data, const ip_address &endpoint, detached_t)
 异步发送数据报—detached(即发即忘)
auto async_send_to (io_context &ctx, memory_view< const char > data, const ip_address &endpoint, use_awaitable_t)
 异步发送数据报—use_awaitable
Public 成员函数 继承自 neforce::ip_socket
 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
Public 成员函数 继承自 neforce::socket_base
 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
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 类型 继承自 neforce::socket_base
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 属性 继承自 neforce::socket_base
static constexpr native_handle_type invalid_handle
 无效句柄常量
Protected 属性 继承自 neforce::ip_socket
family family_ = family::UNDEF
 地址族
Protected 属性 继承自 neforce::socket_base
native_handle_type fd_ = invalid_handle
 Socket句柄
bool nonblocking_ {false}
 非阻塞模式状态跟踪

详细描述

UDP Socket类

实现UDP协议的数据报socket,支持无连接和已连接两种模式。 UDP是面向数据报的协议,提供不可靠、无连接的服务。

主要功能:

  • UDP socket创建和打开
  • 向指定端点发送数据报(send_to)
  • 从任意端点接收数据报(receive_from)
  • 已连接UDP socket的发送(send)和接收(receive)
  • 获取发送方地址信息
注解
数据报大小受MTU限制,通常建议不超过65507字节。

在文件 udp_socket.hpp41 行定义.

成员函数说明

◆ async_receive_from() [1/6]

void neforce::udp_socket::async_receive_from ( io_context & ctx,
memory_view< char > buffer,
cancellation_slot & slot,
function< void(error_code, size_t, ip_address)> handler )

异步接收数据报(带取消槽)

参数
ctx异步 I/O 执行上下文
buffer接收缓冲区
slot取消槽
handler完成回调 void(error_code, size_t bytes, ip_address sender)

◆ async_receive_from() [2/6]

void neforce::udp_socket::async_receive_from ( io_context & ctx,
memory_view< char > buffer,
detached_t  )
inline

异步接收数据报—detached(即发即忘)

参数
ctx异步 I/O 执行上下文
buffer接收缓冲区

在文件 udp_socket.hpp188 行定义.

引用了 async_receive_from().

◆ async_receive_from() [3/6]

void neforce::udp_socket::async_receive_from ( io_context & ctx,
memory_view< char > buffer,
function< void(error_code, size_t, ip_address)> handler )

异步接收数据报

参数
ctx异步 I/O 执行上下文
buffer接收缓冲区
handler完成回调 void(error_code, size_t bytes, ip_address sender)

异步等待接收UDP数据报,返回发送方地址。 handler 在 io_context::run() 线程中执行。

被这些函数引用 async_receive_from(), async_receive_from(), async_receive_from() , 以及 async_receive_from().

◆ async_receive_from() [4/6]

template<typename Token, enable_if_t<!is_same_v< decay_t< Token >, function< void(error_code, size_t, ip_address)> >, int > = 0>
void neforce::udp_socket::async_receive_from ( io_context & ctx,
memory_view< char > buffer,
Token && token )
inline

异步接收数据报—任意可调用对象

模板参数
Token可调用对象类型,需满足 void(error_code, size_t, ip_address) 签名
参数
ctx异步 I/O 执行上下文
buffer接收缓冲区
token完成令牌

在文件 udp_socket.hpp167 行定义.

引用了 async_receive_from() , 以及 neforce::forward().

◆ async_receive_from() [5/6]

auto neforce::udp_socket::async_receive_from ( io_context & ctx,
memory_view< char > buffer,
use_awaitable_t  )
inline

异步接收数据报—use_awaitable

参数
ctx异步 I/O 执行上下文
buffer接收缓冲区
返回
awaitable<pair<size_t, ip_address>> 可协程等待的结果

在文件 udp_socket.hpp200 行定义.

引用了 async_receive_from() , 以及 neforce::use_awaitable.

◆ async_receive_from() [6/6]

auto neforce::udp_socket::async_receive_from ( io_context & ctx,
memory_view< char > buffer,
use_future_t  )
inline

异步接收数据报—use_future

参数
ctx异步 I/O 执行上下文
buffer接收缓冲区
返回
future<pair<size_t, ip_address>> 接收字节数和发送方地址

在文件 udp_socket.hpp177 行定义.

引用了 async_receive_from() , 以及 neforce::use_future.

◆ async_send_to() [1/6]

void neforce::udp_socket::async_send_to ( io_context & ctx,
memory_view< const char > data,
const ip_address & endpoint,
cancellation_slot & slot,
function< void(error_code, size_t)> handler )

异步发送数据报(带取消槽)

参数
ctx异步 I/O 执行上下文
data要发送的数据
endpoint目标端点地址
slot取消槽
handler完成回调 void(error_code, size_t bytes_sent)

引用了 neforce::data().

◆ async_send_to() [2/6]

void neforce::udp_socket::async_send_to ( io_context & ctx,
memory_view< const char > data,
const ip_address & endpoint,
detached_t  )
inline

异步发送数据报—detached(即发即忘)

参数
ctx异步 I/O 执行上下文
data要发送的数据
endpoint目标端点地址

在文件 udp_socket.hpp240 行定义.

引用了 async_send_to() , 以及 neforce::data().

◆ async_send_to() [3/6]

void neforce::udp_socket::async_send_to ( io_context & ctx,
memory_view< const char > data,
const ip_address & endpoint,
function< void(error_code, size_t)> handler )

异步发送数据报

参数
ctx异步 I/O 执行上下文
data要发送的数据
endpoint目标端点地址
handler完成回调 void(error_code, size_t bytes_sent)

异步向指定端点发送UDP数据报。 handler 在 io_context::run() 线程中执行。

引用了 neforce::data().

被这些函数引用 async_send_to(), async_send_to(), async_send_to() , 以及 async_send_to().

◆ async_send_to() [4/6]

template<typename Token, enable_if_t<!is_same_v< decay_t< Token >, function< void(error_code, size_t)> >, int > = 0>
void neforce::udp_socket::async_send_to ( io_context & ctx,
memory_view< const char > data,
const ip_address & endpoint,
Token && token )
inline

异步发送数据报—任意可调用对象

模板参数
Token可调用对象类型,需满足 void(error_code, size_t) 签名
参数
ctx异步 I/O 执行上下文
data要发送的数据
endpoint目标端点地址
token完成令牌

在文件 udp_socket.hpp216 行定义.

引用了 async_send_to(), neforce::data() , 以及 neforce::forward().

◆ async_send_to() [5/6]

auto neforce::udp_socket::async_send_to ( io_context & ctx,
memory_view< const char > data,
const ip_address & endpoint,
use_awaitable_t  )
inline

异步发送数据报—use_awaitable

参数
ctx异步 I/O 执行上下文
data要发送的数据
endpoint目标端点地址
返回
awaitable<size_t> 可协程等待的结果

在文件 udp_socket.hpp253 行定义.

引用了 async_send_to(), neforce::data() , 以及 neforce::use_awaitable.

◆ async_send_to() [6/6]

auto neforce::udp_socket::async_send_to ( io_context & ctx,
memory_view< const char > data,
const ip_address & endpoint,
use_future_t  )
inline

异步发送数据报—use_future

参数
ctx异步 I/O 执行上下文
data要发送的数据
endpoint目标端点地址
返回
future<size_t> 发送字节数

在文件 udp_socket.hpp227 行定义.

引用了 async_send_to(), neforce::data() , 以及 neforce::use_future.

◆ open()

void neforce::udp_socket::open ( family f = family::INET4)

打开UDP socket

参数
f地址族,默认IPv4
异常
socket_exception创建失败时抛出

创建UDP协议的socket,使用SOCK_DGRAM类型和IPPROTO_UDP协议。

◆ receive()

ssize_t neforce::udp_socket::receive ( memory_view< char > buffer,
int flags = 0 )

从已连接的端点接收数据

参数
buffer接收缓冲区
flags接收标志(通常为0)
返回
实际接收的字节数
异常
socket_exception接收失败时抛出
value_exceptionsocket未打开或缓冲区为空时抛出

从connect()关联的远程端点接收数据,需要先调用connect()。 只能接收来自该端点的数据。

◆ receive_from()

pair< ssize_t, ip_address > neforce::udp_socket::receive_from ( memory_view< char > buffer,
int flags = 0 )

接收数据报并获取发送方地址

参数
buffer接收缓冲区
flags接收标志(通常为0)
返回
接收的字节数和发送方地址的pair
异常
socket_exception接收失败时抛出
value_exceptionsocket未打开或缓冲区为空时抛出

接收UDP数据报,同时返回发送方的IP地址和端口。 适用于无连接模式的UDP服务器。

◆ send()

ssize_t neforce::udp_socket::send ( memory_view< const char > data,
int flags = 0 )

向已连接的端点发送数据

参数
data要发送的数据
flags发送标志(通常为0)
返回
实际发送的字节数
异常
socket_exception发送失败时抛出
value_exceptionsocket未打开时抛出

向connect()关联的远程端点发送数据,需要先调用connect()。 性能优于send_to(),因为不需要每次指定目标地址。

引用了 neforce::data().

◆ send_to()

ssize_t neforce::udp_socket::send_to ( memory_view< const char > data,
const ip_address & endpoint,
int flags = 0 )

向指定端点发送数据报

参数
data要发送的数据
endpoint目标端点地址
flags发送标志(通常为0)
返回
实际发送的字节数
异常
socket_exception发送失败时抛出
value_exceptionsocket未打开或端点无效时抛出

向指定的远程端点发送UDP数据报,不需要socket处于连接状态。

引用了 neforce::data().


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