fix: JSON TCP accept后未配置接收缓冲区导致无法收数据

根因: WCHNET TCP Server 模式下, accept后的socket需要调用
WCHNET_ModifyRecvBuf 配置接收缓冲区才能正常接收数据。

修复:
1. tcp_json_srv.c: accept时调用 WCHNET_ModifyRecvBuf 设置 recv buf
2. 去掉脆弱的scan逻辑, 改为收到CONNECT+socket不匹配已知socket时自动识别
3. net_srv.c: 同步更新路由条件
4. DBNetClient: 增加原始JSON发送日志
This commit is contained in:
wangfq
2026-06-30 17:38:45 +08:00
parent 8b4404d5b3
commit eb79c66763
3 changed files with 25 additions and 26 deletions

View File

@@ -487,7 +487,7 @@ void WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat)
return;
}
// Also catch newly accepted TCP connections that might be JSON clients
if (intstat & SINT_STAT_CONNECT && g_json_socket_client == 0xFF && socketid != g_json_socket_listen) {
if ((intstat & SINT_STAT_CONNECT) && socketid != SocketId_TCP && socketid != SocketId_UDP) {
tcp_json_handle_sock_int(socketid, intstat);
return;
}

View File

@@ -614,30 +614,9 @@ void tcp_json_handle_sock_int(uint8_t socketid, uint8_t intstat) {
// === Listen socket events ===
if (socketid == g_json_socket_listen) {
if (intstat & SINT_STAT_CONNECT) {
// A client connected — find the accepted socket
// The accepted connection gets a different socket ID
// We find it by scanning TCP sockets in ESTABLISHED state
uint8_t i;
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
if (i == g_json_socket_listen) continue;
// Check if this socket is in ESTABLISHED state and not our SSC/MQTT socket
SOCK_INF *si = NULL;
// We check by trying to recv — if it works, it's the accepted socket
uint32_t recv_len = WCHNET_SocketRecvLen(i, NULL);
(void)recv_len;
// Simpler approach: just assign the first non-listen TCP socket
// that's not the existing SSC/MQTT client (SocketId_TCP)
if (i != SocketId_TCP && g_json_socket_client == 0xFF) {
g_json_socket_client = i;
g_json_auth_state = JSON_STATE_WAIT_AUTH;
g_json_pwd_retry = 0;
g_json_auth_timer = mstick();
g_json_recv_len = 0;
memset(g_json_recv_buf, 0, sizeof(g_json_recv_buf));
PRINT("JSON: Client connected on socket %d\n", i);
break;
}
}
// A client connected — the accepted socket gets its own CONNECT event
// We handle the accept in the "newly accepted" path below
PRINT("JSON: Listen socket got CONNECT\n");
}
if (intstat & SINT_STAT_DISCONNECT) {
PRINT("JSON: Listen socket disconnect (unexpected)\n");
@@ -645,6 +624,25 @@ void tcp_json_handle_sock_int(uint8_t socketid, uint8_t intstat) {
return;
}
// === Newly accepted client (CONNECT on unknown socket) ===
if ((intstat & SINT_STAT_CONNECT) && socketid != g_json_socket_client
&& socketid != SocketId_TCP && socketid != SocketId_UDP) {
// This is the newly accepted JSON client connection
g_json_socket_client = socketid;
g_json_auth_state = JSON_STATE_WAIT_AUTH;
g_json_pwd_retry = 0;
g_json_auth_timer = mstick();
g_json_recv_len = 0;
memset(g_json_recv_buf, 0, sizeof(g_json_recv_buf));
// CRITICAL: Set up receive buffer for the accepted TCP socket
// Without this, WCHNET won't buffer incoming data on this socket
WCHNET_ModifyRecvBuf(socketid, (uint32_t)g_json_recv_buf, TCP_JSON_RECV_BUF_LEN);
PRINT("JSON: Client accepted on socket %d, recv buf configured\n", socketid);
return;
}
// === Client socket events ===
if (socketid == g_json_socket_client) {
if (intstat & SINT_STAT_RECV) {