This commit is contained in:
2026-03-14 21:31:44 +08:00
parent 2fa3bbf647
commit 9230e7985d
2 changed files with 46 additions and 13 deletions
+37 -11
View File
@@ -331,7 +331,7 @@ qdx_socket_t qdx_port_tcp_connect(const char *ip, uint16_t port)
if (err != WCHNET_ERR_SUCCESS) {
DBG_PORT("SocketConnect fail %02X\r\n", err);
WCHNET_SocketClose(wchnet_id, 0);
WCHNET_SocketClose(wchnet_id, TCP_CLOSE_RST);
free_sock_ctx(ctx);
return NULL;
}
@@ -346,14 +346,14 @@ qdx_socket_t qdx_port_tcp_connect(const char *ip, uint16_t port)
DBG_PORT("connect_sem TIMEOUT after %d ms -> %d.%d.%d.%d:%d\r\n",
(int)elapsed, dest_ip[0], dest_ip[1], dest_ip[2], dest_ip[3], port);
DBG_PORT(" ctx->connected=%d wchnet_id=%d\r\n", ctx->connected, wchnet_id);
WCHNET_SocketClose(wchnet_id, 0);
WCHNET_SocketClose(wchnet_id, TCP_CLOSE_RST);
free_sock_ctx(ctx);
return NULL;
}
DBG_PORT("connect_sem got after %d ms, connected=%d\r\n", (int)elapsed, ctx->connected);
if (!ctx->connected) {
WCHNET_SocketClose(wchnet_id, 0);
WCHNET_SocketClose(wchnet_id, TCP_CLOSE_RST);
free_sock_ctx(ctx);
return NULL;
}
@@ -373,16 +373,39 @@ int32_t qdx_port_tcp_send(qdx_socket_t sock, const uint8_t *data, uint32_t len)
if (!ctx || !ctx->connected)
return -1;
uint32_t send_len = len;
uint32_t total_sent = 0;
const uint8_t *ptr = data;
uint32_t remaining = len;
uint8_t retries = 0;
xSemaphoreTake(g_wchnet_mutex, portMAX_DELAY);
uint8_t err = WCHNET_SocketSend(ctx->wchnet_sock_id, (uint8_t *)data, &send_len);
xSemaphoreGive(g_wchnet_mutex);
while (remaining > 0) {
uint32_t send_len = remaining;
if (err != WCHNET_ERR_SUCCESS)
return -1;
xSemaphoreTake(g_wchnet_mutex, portMAX_DELAY);
uint8_t err = WCHNET_SocketSend(ctx->wchnet_sock_id, (uint8_t *)ptr, &send_len);
xSemaphoreGive(g_wchnet_mutex);
return (int32_t)send_len;
if (err != WCHNET_ERR_SUCCESS && send_len == 0) {
/* WCHNET send buffer full — yield so wchnet task can flush */
if (++retries > 50) {
DBG_PORT("send fail after retries, err=0x%02X\r\n", err);
return -1;
}
vTaskDelay(pdMS_TO_TICKS(2));
continue;
}
retries = 0;
ptr += send_len;
remaining -= send_len;
total_sent += send_len;
if (remaining > 0) {
/* Partial send — yield to let wchnet task drain the buffer */
vTaskDelay(pdMS_TO_TICKS(2));
}
}
return (int32_t)total_sent;
}
int32_t qdx_port_tcp_recv(qdx_socket_t sock, uint8_t *buf, uint32_t max_len)
@@ -422,7 +445,10 @@ void qdx_port_tcp_close(qdx_socket_t sock)
if (ctx->in_use) {
xSemaphoreTake(g_wchnet_mutex, portMAX_DELAY);
WCHNET_SocketClose(ctx->wchnet_sock_id, 0);
/* Use TCP_CLOSE_RST to immediately release the WCHNET socket slot.
* TCP_CLOSE_NORMAL (0) causes TIME_WAIT which holds the slot for
* an extended period, preventing reconnection with only 2 sockets. */
WCHNET_SocketClose(ctx->wchnet_sock_id, TCP_CLOSE_RST);
xSemaphoreGive(g_wchnet_mutex);
}
ring_init(&ctx->rx_ring);