Skip to content

Commit

Permalink
rdp: Refactor csp_rdp_queue_flush using csp_queue_empty
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
yashi committed Jul 7, 2024
1 parent c5dfbf5 commit 2ad14f0
Showing 1 changed file with 30 additions and 41 deletions.
71 changes: 30 additions & 41 deletions src/csp_rdp_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 2ad14f0

Please sign in to comment.