fix(tcp): MUX模式网口失联 — 对端关闭时用tcp_abort替代tcp_close避免TIME_WAIT耗尽pcb池

根因: tcp_close()将对端关闭的pcb推入TIME_WAIT(120s), 占用MEMP_TCP_PCB池(仅4个),
多连接同时断开后pcb池耗尽, tcp_new()返回NULL, 新连接无法建立直到120s超时释放。

核心修复:
- tcp_server/client: 对端关闭(p=NULL)时tcp_abort替代tcp_close, pcb立即释放
- ch390_runtime: PKT_ERR恢复强制OR上RCR_RXEN(与WCH官方一致)
- ch390_runtime: TX连续超时3次自动emergency reset
- ch390_runtime: 每5秒health_check读VID验证芯片存活
- main: App_StartLinksIfNeeded失败时不标记g_links_started, 允许重试
- main: MUX逐帧RTT printf改为#if DEBUG门控, 减少主循环延迟
- uart_trans: MUX帧解析改为先搜0x7E再消费header, 非法帧只丢1字节
This commit is contained in:
2026-04-14 03:44:26 +08:00
parent efb88ea367
commit 31a3da48fa
7 changed files with 151 additions and 19 deletions
+2 -4
View File
@@ -52,13 +52,11 @@ static err_t tcp_client_on_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
tcp_recv(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_err(pcb, NULL);
if (tcp_close(pcb) != ERR_OK) {
tcp_abort(pcb);
}
tcp_abort(pcb);
ctx->pcb = NULL;
ctx->status.state = TCP_CLIENT_STATE_DISCONNECTED;
ctx->next_retry_ms = HAL_GetTick() + ctx->config.reconnect_interval_ms;
return ERR_OK;
return ERR_ABRT;
}
for (q = p; q != NULL; q = q->next) {
+5 -5
View File
@@ -52,12 +52,10 @@ static err_t tcp_server_on_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
tcp_recv(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_err(pcb, NULL);
if (tcp_close(pcb) != ERR_OK) {
tcp_abort(pcb);
}
tcp_abort(pcb);
ctx->client_pcb = NULL;
ctx->status.state = ctx->config.enabled ? TCP_SERVER_STATE_LISTENING : TCP_SERVER_STATE_IDLE;
return ERR_OK;
return ERR_ABRT;
}
for (q = p; q != NULL; q = q->next) {
@@ -205,7 +203,9 @@ int tcp_server_stop(uint8_t instance)
if (ctx->listen_pcb != NULL) {
tcp_arg(ctx->listen_pcb, NULL);
tcp_accept(ctx->listen_pcb, NULL);
tcp_close(ctx->listen_pcb);
if (tcp_close(ctx->listen_pcb) != ERR_OK) {
tcp_abort(ctx->listen_pcb);
}
ctx->listen_pcb = NULL;
}
+18 -6
View File
@@ -297,7 +297,8 @@ void uart_trans_tx_cplt_handler(uart_channel_t channel)
bool uart_mux_try_extract_frame(uart_channel_t channel, uart_mux_frame_t *frame)
{
uint8_t header[5];
uint8_t sync_byte;
uint8_t header[4];
uint16_t available;
uint16_t payload_len;
@@ -310,14 +311,25 @@ bool uart_mux_try_extract_frame(uart_channel_t channel, uart_mux_frame_t *frame)
return false;
}
if (uart_trans_read(channel, header, sizeof(header)) != sizeof(header)) {
/* Scan for SYNC byte (0x7E) — discard non-matching bytes one at a time */
if (uart_trans_read(channel, &sync_byte, 1u) != 1u) {
return false;
}
if (header[0] != UART_MUX_SYNC) {
if (sync_byte != UART_MUX_SYNC) {
return false;
}
payload_len = (uint16_t)(((uint16_t)header[1] << 8) | header[2]);
/* Need at least: 2(len) + 1(src) + 1(dst) + payload + 1(tail) = 5 + payload */
available = uart_trans_rx_available(channel);
if (available < 4u) {
return false;
}
if (uart_trans_read(channel, header, sizeof(header)) != sizeof(header)) {
return false;
}
payload_len = (uint16_t)(((uint16_t)header[0] << 8) | header[1]);
if (payload_len > sizeof(frame->payload)) {
return false;
}
@@ -325,8 +337,8 @@ bool uart_mux_try_extract_frame(uart_channel_t channel, uart_mux_frame_t *frame)
return false;
}
frame->src_id = header[3];
frame->dst_mask = header[4];
frame->src_id = header[2];
frame->dst_mask = header[3];
frame->payload_len = payload_len;
if (payload_len > 0u) {
if (uart_trans_read(channel, frame->payload, payload_len) != payload_len) {