修改一些注释

This commit is contained in:
2026-08-25 11:49:31 +08:00
parent 4375873083
commit d6bc3f6253
6 changed files with 496 additions and 347 deletions

View File

@@ -34,17 +34,18 @@ static int check_socket_error(int sockfd);
/*
* 函数功能I/O 多路复用select 模式),等待多个 Socket 的读写事件就绪
* 入口参数nfds - 最大文件描述符数 +1 int > 0
* readfds - 读事件集合指针 net_fd_set *(可为 NULL
* writefds - 写事件集合指针 net_fd_set *(可为 NULL
* exceptfds- 异常事件集合指针 net_fd_set *(可为 NULL
* timeout - 超时时间结构体指针 net_timeval *NULL=无限等待)
* 返回值:就绪的 Socket 数量 int >= 0 || -1
* -1 - 参数无效 int
* 入口参数nfds - 最大文件描述符数 +1 int > 0
* readfds - 读事件集合指针 net_fd_set *(可为 NULL
* writefds - 写事件集合指针 net_fd_set *(可为 NULL
* exceptfds- 异常事件集合指针 net_fd_set *(可为 NULL
* timeout - 超时时间结构体指针 net_timeval *NULL=无限等待)
* 出口参数readfds/writefds/exceptfds - 仅保留就绪的 Socket 位 net_fd_set *
* 返回值:就绪的 Socket 数量(>= 0-1 表示参数无效 int
* 限定条件net_init() + MX_FREERTOS_Init() 已执行;必须在 netTask 上下文中调用
* 函数说明:轮询所有 Socket检查读/写/异常事件是否就绪。阻塞等待直到有事件或超时
* 函数说明:1. 轮询所有 Socket检查读/写/异常事件是否就绪
* 2. 阻塞等待直到有事件就绪或超时
* 3. 通过 osDelay(1) 让出 CPU平衡实时性与 CPU 利用率
*/
int net_select(int nfds, net_fd_set *readfds, net_fd_set *writefds,
net_fd_set *exceptfds, net_timeval *timeout) {
uint32_t start_tick;
@@ -55,8 +56,13 @@ int net_select(int nfds, net_fd_set *readfds, net_fd_set *writefds,
int ready_count = 0;
int i;
/* 计算超时时间 */
if (timeout != NULL) {
/* 检查输入参数合法性 */
if ((0 >= nfds) || (NET_MAX_SOCKETS < nfds)) {
return -1;
}
/* 计算超时时间(毫秒) */
if (NULL != timeout) {
timeout_ms = (uint32_t)(timeout->tv_sec * 1000) +
(uint32_t)(timeout->tv_usec / 1000);
}
@@ -64,37 +70,37 @@ int net_select(int nfds, net_fd_set *readfds, net_fd_set *writefds,
start_tick = HAL_GetTick();
while (1) {
/* 轮询所<EFBFBD>?Socket */
/* 轮询所Socket */
net_poll();
ready_count = 0;
/* 检查哪<EFBFBD>?Socket 有事<EFBFBD>?*/
/* 检查哪Socket 有事*/
for (i = 0; i < nfds && i < NET_MAX_SOCKETS; i++) {
net_sock_t *p_sock = net_get_sock(i);
if (p_sock == NULL) {
if (NULL == p_sock) {
continue;
}
/* 检查可读事<EFBFBD>?*/
if (readfds != NULL && NET_FD_ISSET(i, readfds)) {
/* 检查可读事*/
if ((NULL != readfds) && NET_FD_ISSET(i, readfds)) {
if (check_socket_readable(i)) {
NET_FD_SET(i, &result_read);
ready_count++;
}
}
/* 检查可写事<EFBFBD>?*/
if (writefds != NULL && NET_FD_ISSET(i, writefds)) {
/* 检查可写事*/
if ((NULL != writefds) && NET_FD_ISSET(i, writefds)) {
if (check_socket_writable(i)) {
NET_FD_SET(i, &result_write);
ready_count++;
}
}
/* 检查异常事<EFBFBD>?*/
if (exceptfds != NULL && NET_FD_ISSET(i, exceptfds)) {
/* 检查异常事*/
if ((NULL != exceptfds) && NET_FD_ISSET(i, exceptfds)) {
if (check_socket_error(i)) {
NET_FD_SET(i, &result_except);
ready_count++;
@@ -103,14 +109,14 @@ int net_select(int nfds, net_fd_set *readfds, net_fd_set *writefds,
}
/* 有事件就绪,返回 */
if (ready_count > 0) {
if (0 < ready_count) {
break;
}
/* 超时检<EFBFBD>?*/
if (timeout != NULL) {
if (timeout_ms > 0 &&
(HAL_GetTick() - start_tick) >= timeout_ms) {
/* 超时检*/
if (NULL != timeout) {
if ((0 < timeout_ms) &&
((HAL_GetTick() - start_tick) >= timeout_ms)) {
break; /* 超时返回 0 */
}
}
@@ -119,14 +125,14 @@ int net_select(int nfds, net_fd_set *readfds, net_fd_set *writefds,
osDelay(1); /* 1ms 周期轮询,平衡实时性和 CPU 利用率 */
}
/* 复制结果 */
if (readfds != NULL) {
/* 复制结果集合 */
if (NULL != readfds) {
readfds->fd_bits = result_read.fd_bits;
}
if (writefds != NULL) {
if (NULL != writefds) {
writefds->fd_bits = result_write.fd_bits;
}
if (exceptfds != NULL) {
if (NULL != exceptfds) {
exceptfds->fd_bits = result_except.fd_bits;
}
@@ -135,20 +141,22 @@ int net_select(int nfds, net_fd_set *readfds, net_fd_set *writefds,
/*
* 函数功能I/O 多路复用poll 模式),等待多个 Socket 的读写事件就绪
* 入口参数fds - pollfd 数组指针 net_pollfd *
* nfds - 数组元素数量 int > 0
* timeout - 超时时间(毫秒) int >= 0
* 返回值:就绪的 fd 数量 int >= 0 || -1
* -1 - 参数无效 int
* 入口参数fds - pollfd 数组指针 net_pollfd *
* nfds - 数组元素数量 int > 0
* timeout - 超时时间(毫秒) int >= 0-1=无限等待,但本实现按 >=0 处理)
* 出口参数fds[i].revents - 填充实际发生的事件掩码 short
* 返回值:就绪的 fd 数量(>= 0-1 表示参数无效 int
* 限定条件net_init() + MX_FREERTOS_Init() 已执行;必须在 netTask 上下文中调用
* 函数说明:轮询所有 Socket检查读/写/错误/挂起事件是否就绪。阻塞等待直到有事件或超时
* 函数说明:1. 轮询所有 Socket检查读/写/错误/挂起事件是否就绪
* 2. 阻塞等待直到有事件就绪或超时
* 3. 通过 osDelay(1) 让出 CPU平衡实时性与 CPU 利用率
*/
int net_poll_events(net_pollfd *fds, int nfds, int timeout) {
uint32_t start_tick;
int ready = 0;
int i;
if (fds == NULL || nfds <= 0) {
if ((NULL == fds) || (0 >= nfds)) {
return -1;
}
@@ -163,8 +171,8 @@ int net_poll_events(net_pollfd *fds, int nfds, int timeout) {
for (i = 0; i < nfds; i++) {
fds[i].revents = 0;
/* 检<EFBFBD>?fd 有效<EFBFBD>?*/
if (fds[i].fd < 0 || fds[i].fd >= NET_MAX_SOCKETS) {
/* 检fd 有效*/
if ((0 > fds[i].fd) || (NET_MAX_SOCKETS <= fds[i].fd)) {
fds[i].revents |= NET_POLLNVAL;
ready++;
continue;
@@ -172,13 +180,13 @@ int net_poll_events(net_pollfd *fds, int nfds, int timeout) {
net_sock_t *p_sock = net_get_sock(fds[i].fd);
if (p_sock == NULL) {
if (NULL == p_sock) {
fds[i].revents |= NET_POLLNVAL;
ready++;
continue;
}
/* 检查可<EFBFBD>?*/
/* 检查可*/
if (fds[i].events & NET_POLLIN) {
if (check_socket_readable(fds[i].fd)) {
fds[i].revents |= NET_POLLIN;
@@ -186,7 +194,7 @@ int net_poll_events(net_pollfd *fds, int nfds, int timeout) {
}
}
/* 检查可<EFBFBD>?*/
/* 检查可*/
if (fds[i].events & NET_POLLOUT) {
if (check_socket_writable(fds[i].fd)) {
fds[i].revents |= NET_POLLOUT;
@@ -194,26 +202,26 @@ int net_poll_events(net_pollfd *fds, int nfds, int timeout) {
}
}
/* 检查错<EFBFBD>?*/
/* 检查错*/
if (check_socket_error(fds[i].fd)) {
fds[i].revents |= NET_POLLERR;
ready++;
}
/* 检查挂起(连接断开<EFBFBD>?*/
if (p_sock->state == NET_SOCK_STATE_TCP_ACCEPT ||
p_sock->state == NET_SOCK_STATE_CLOSED) {
/* 检查挂起(连接断开 */
if ((NET_SOCK_STATE_TCP_ACCEPT == p_sock->state) ||
(NET_SOCK_STATE_CLOSED == p_sock->state)) {
fds[i].revents |= NET_POLLHUP;
ready++;
}
}
if (ready > 0) {
if (0 < ready) {
break;
}
/* 超时检<EFBFBD>?*/
if (timeout >= 0) {
/* 超时检*/
if (0 <= timeout) {
if ((HAL_GetTick() - start_tick) >= (uint32_t)timeout) {
break;
}
@@ -227,29 +235,36 @@ int net_poll_events(net_pollfd *fds, int nfds, int timeout) {
}
/*
* 私有函数实现<EFBFBD>? */
* 私有函数实现
*/
/*
* 函数功能:检<EFBFBD>?Socket 是否可读
* 返回值1 可读<E58FAF>? 不可<E4B88D>? */
* 函数功能:检查指定 Socket 当前是否可读
* 入口参数sockfd - 待检查的 Socket 描述符 int 0 - NET_MAX_SOCKETS-1
* 出口参数:无
* 返回值1 - 可读监听态有新连接或已连接态接收缓冲非空0 - 不可读或描述符无效 int
* 限定条件net_init() 已调用
* 函数说明1. 监听态 Socket 视为可读(存在可 accept 的新连接)
* 2. 已建立连接且接收缓冲区长度大于 0 时视为可读
*/
static int check_socket_readable(int sockfd) {
net_sock_t *p_sock;
uint16_t recv_len;
p_sock = net_get_sock(sockfd);
if (p_sock == NULL) {
if (NULL == p_sock) {
return 0;
}
/* 监听 Socket 有新连接时可<EFBFBD>?*/
if (p_sock->state == NET_SOCK_STATE_LISTENING) {
/* 监听 Socket 有新连接时可*/
if (NET_SOCK_STATE_LISTENING == p_sock->state) {
return 1;
}
/* 已建立连接的 Socket 有数据可<EFBFBD>?*/
if (p_sock->state == NET_SOCK_STATE_ESTABLISHED) {
/* 已建立连接的 Socket 有数据可*/
if (NET_SOCK_STATE_ESTABLISHED == p_sock->state) {
recv_len = ch395f_get_recv_len(sockfd);
if (recv_len > 0) {
if (0 < recv_len) {
return 1;
}
}
@@ -258,18 +273,23 @@ static int check_socket_readable(int sockfd) {
}
/*
* 函数功能:检<EFBFBD>?Socket 是否可写
* 返回值1 可写<E58FAF>? 不可<E4B88D>? */
* 函数功能:检查指定 Socket 当前是否可写
* 入口参数sockfd - 待检查的 Socket 描述符 int 0 - NET_MAX_SOCKETS-1
* 出口参数:无
* 返回值1 - 可写已连接且发送缓冲空闲0 - 不可写或描述符无效 int
* 限定条件net_init() 已调用
* 函数说明1. 仅已建立连接且 send_ready 标志置位的 Socket 视为可写
*/
static int check_socket_writable(int sockfd) {
net_sock_t *p_sock;
p_sock = net_get_sock(sockfd);
if (p_sock == NULL) {
if (NULL == p_sock) {
return 0;
}
/* 已建立连接的 Socket 发送缓冲区空闲时可<EFBFBD>?*/
if (p_sock->state == NET_SOCK_STATE_ESTABLISHED) {
/* 已建立连接的 Socket 发送缓冲区空闲时可*/
if (NET_SOCK_STATE_ESTABLISHED == p_sock->state) {
if (p_sock->send_ready) {
return 1;
}
@@ -279,17 +299,24 @@ static int check_socket_writable(int sockfd) {
}
/*
* 函数功能:检<EFBFBD>?Socket 是否有错<EFBFBD>? * 返回值1 有错误0 无错<E697A0>? */
* 函数功能:检查指定 Socket 是否存在错误状态
* 入口参数sockfd - 待检查的 Socket 描述符 int 0 - NET_MAX_SOCKETS-1
* 出口参数:无
* 返回值1 - 有错误(含描述符越界或 Socket 未分配0 - 无错误 int
* 限定条件:无
* 函数说明1. 描述符越界视为错误
* 2. 未分配net_get_sock 返回 NULL的 Socket 视为错误
*/
static int check_socket_error(int sockfd) {
net_sock_t *p_sock;
/* 无效 fd 视为错误 */
if (sockfd < 0 || sockfd >= NET_MAX_SOCKETS) {
if ((0 > sockfd) || (NET_MAX_SOCKETS <= sockfd)) {
return 1;
}
p_sock = net_get_sock(sockfd);
if (p_sock == NULL) {
if (NULL == p_sock) {
return 1; /* 未使用的 Socket 视为错误 */
}