From a2d7a705a80b243094b8deeda03f4c1306ae0838 Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Mon, 18 Jan 2021 19:45:48 +0800 Subject: [PATCH] Waiting the tcp socket closed by remote client So that the tcp socket are closed by debugger client first and the debugger client won't receive socket are closed by remote error JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo luoyonggang@gmail.com --- jerry-ext/debugger/debugger-tcp.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/jerry-ext/debugger/debugger-tcp.c b/jerry-ext/debugger/debugger-tcp.c index 0aff1b8508..f52eb7ef70 100644 --- a/jerry-ext/debugger/debugger-tcp.c +++ b/jerry-ext/debugger/debugger-tcp.c @@ -144,6 +144,30 @@ jerryx_debugger_tcp_close (jerry_debugger_transport_header_t *header_p) /**< tcp jerryx_debugger_transport_tcp_t *tcp_p = (jerryx_debugger_transport_tcp_t *) header_p; + /* Waiting for the debug client close the tcp connection first. */ + while (true) + { + char buf[8]; + jerryx_socket_ssize_t result = recv (tcp_p->tcp_socket, buf, sizeof (buf), 0); + if (result == 0) + { + /** + * If result == 0, means the socket are closed by remote client, break the loop + */ + break; + } + else if (result < 0 && jerryx_debugger_tcp_get_errno () != JERRYX_EWOULDBLOCK) + { + /* errno other than JERRYX_EWOULDBLOCK means socket have true error, break the loop */ + break; + } + /** + * If result > 0, means that there is data available on the socket, waiting. + * If result < 0 and errno == JERRYX_EWOULDBLOCK, means have no data but + * the socket still available, waiting. + */ + } + JERRYX_DEBUG_MSG ("TCP connection closed.\n"); jerryx_debugger_tcp_close_socket (tcp_p->tcp_socket);