From 2ad14f04aa7332741c150b5039ff7bba891ed89e Mon Sep 17 00:00:00 2001 From: Yasushi SHOJI Date: Fri, 5 Jul 2024 09:57:11 +0900 Subject: [PATCH] rdp: Refactor csp_rdp_queue_flush using csp_queue_empty With the newly introduced csp_queue_empty(), we can refactor csp_rdp_queue_flush() to improve clarity and functionality. csp_rdp_queue_flush() now consists of a simple if-else statement that calls csp_queue_empty() or __csp_rdp_queue_flush(). This change explicitly indicates that emptying both tx_queue and rx_queue without a connection is intended behavior (whether this is a good idea is a separate issue). __csp_rdp_queue_flush() traverses the items in the queue as follows: 1. Dequeue a packet. 2. Compare the packet against the given connection. 3. Free or re-enqueue the packet based on the given connection. Note that this operation is not thread-safe. However, the current CSP RDP codebase is not thread-safe, and this issue will be addressed in a future commit. Signed-off-by: Yasushi SHOJI --- src/csp_rdp_queue.c | 71 +++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 41 deletions(-) diff --git a/src/csp_rdp_queue.c b/src/csp_rdp_queue.c index 3438e0998..f553f421f 100644 --- a/src/csp_rdp_queue.c +++ b/src/csp_rdp_queue.c @@ -21,55 +21,44 @@ void csp_rdp_queue_init(void) { } -void csp_rdp_queue_flush(csp_conn_t * conn) { +static int __csp_rdp_queue_flush(csp_queue_handle_t queue, csp_conn_t * conn) { - csp_packet_t * packet; + int ret; + int size; - /* Flush packets of the conn from TX queue */ - int size = csp_queue_size(tx_queue); - while(size--) { - if (csp_queue_dequeue(tx_queue, &packet, 0) == CSP_QUEUE_ERROR) { - /* csp_queue_dequeue will fail if the queue is empty, so it's better to break here */ - break; - } + size = csp_queue_size(queue); + while (size--) { + csp_packet_t * packet; - if (packet == NULL) { - continue; - } + ret = csp_queue_dequeue(tx_queue, &packet, 0); + if (ret != CSP_QUEUE_OK) { + break; + } - if ((conn == NULL) || (conn == packet->conn)) { + if (conn == packet->conn) { csp_buffer_free(packet); } else { - if (csp_queue_enqueue(tx_queue, &packet, 0) != CSP_QUEUE_OK) { - csp_print("csp_rdp_queue_flush : RX queue fail to enqueue, data lost !\n"); - /* Free the packet if re-enqueue fails */ - csp_buffer_free(packet); - } - } - } + /* put it back */ + ret = csp_queue_enqueue(queue, packet, 0); + if (ret != CSP_QUEUE_OK) { + /* something is really broken */ + break; + } + } + } + + return ret; +} - /* Flush packets of the conn from RX queue */ - size = csp_queue_size(rx_queue); - while(size--) { - if (csp_queue_dequeue(rx_queue, &packet, 0) == CSP_QUEUE_ERROR) { - /* csp_queue_dequeue will fail if the queue is empty, so it's better to break here */ - break; - } - - if (packet == NULL) { - continue; - } +void csp_rdp_queue_flush(csp_conn_t * conn) { - if ((conn == NULL) || (conn == packet->conn)) { - csp_buffer_free(packet); - } else { - if (csp_queue_enqueue(rx_queue, &packet, 0) != CSP_QUEUE_OK) { - csp_print("csp_rdp_queue_flush : RX queue fail to enqueue, data lost !\n"); - /* Free the packet if re-enqueue fails */ - csp_buffer_free(packet); - } - } - } + if (conn == NULL) { + csp_queue_empty(tx_queue); + csp_queue_empty(rx_queue); + } else { + (void)__csp_rdp_queue_flush(conn, tx_queue); + (void)__csp_rdp_queue_flush(conn, rx_queue); + } } int csp_rdp_queue_tx_size(void) {