From eb79c6676380e41da540679b678b1bbba72257b3 Mon Sep 17 00:00:00 2001 From: wangfq Date: Tue, 30 Jun 2026 17:38:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20JSON=20TCP=20accept=E5=90=8E=E6=9C=AA?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=8E=A5=E6=94=B6=E7=BC=93=E5=86=B2=E5=8C=BA?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E6=97=A0=E6=B3=95=E6=94=B6=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因: 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发送日志 --- DBNetClient/tcp_json_client.py | 3 +- .../OnlyUpdateApp_Peripheral/APP/net_srv.c | 2 +- .../APP/tcp_json_srv.c | 46 +++++++++---------- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/DBNetClient/tcp_json_client.py b/DBNetClient/tcp_json_client.py index 49b45bd..40f48bc 100644 --- a/DBNetClient/tcp_json_client.py +++ b/DBNetClient/tcp_json_client.py @@ -92,7 +92,8 @@ class TcpJsonClient: try: self._sock.sendall(raw.encode("utf-8")) - self._log(f">>> {cmd} (msg_id={msg_id})") + self._log(f">>> {cmd} (msg_id={msg_id}) [{len(raw)} bytes]") + self._log(f">>> RAW: {raw.strip()}") except OSError as e: with self._lock: self._pending.pop(msg_id, None) diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c index acb01a4..dc6c42d 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c @@ -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; } diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c index fd380bb..420082b 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c @@ -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) {