Skip to content

Commit

Permalink
Merge pull request #21122 from benpicco/nanocoap_sock/flush
Browse files Browse the repository at this point in the history
sys/net/nanocoap: flush stale responses before sending request
  • Loading branch information
maribu authored Jan 6, 2025
2 parents 725a22e + 5869554 commit ff92ec9
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions sys/net/application_layer/nanocoap/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ static uint32_t _deadline_left_us(uint32_t deadline)
return deadline - now;
}

static void _sock_flush(nanocoap_sock_t *sock)
{
void *payload, *ctx = NULL;
while (_sock_recv_buf(sock, &payload, &ctx, 0) > 0 || ctx) {}
}

ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt,
coap_request_cb_t cb, void *arg)
{
Expand Down Expand Up @@ -223,6 +229,9 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt,
.iol_len = coap_get_total_len(pkt),
};

/* clear out stale responses from previous requests */
_sock_flush(sock);

while (1) {
switch (state) {
case STATE_REQUEST_SEND:
Expand Down

0 comments on commit ff92ec9

Please sign in to comment.