Skip to content

Commit aa9ce28

Browse files
committed
zephyr/modusocket: Get rid of cur_pkt object member.
Instead, just peek a packet at the head of the queue and work with it.
1 parent a3008e4 commit aa9ce28

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

zephyr/modusocket.c

+18-24
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ typedef struct _socket_obj_t {
5252
struct k_fifo recv_q;
5353
struct k_fifo accept_q;
5454
};
55-
struct net_pkt *cur_pkt;
5655

5756
#define STATE_NEW 0
5857
#define STATE_CONNECTING 1
@@ -208,7 +207,6 @@ socket_obj_t *socket_new(void) {
208207
socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t);
209208
socket->base.type = (mp_obj_t)&socket_type;
210209
k_fifo_init(&socket->recv_q);
211-
socket->cur_pkt = NULL;
212210
socket->state = STATE_NEW;
213211
return socket;
214212
}
@@ -393,26 +391,20 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int *
393391

394392
do {
395393

396-
if (socket->cur_pkt == NULL) {
397-
if (socket->state == STATE_PEER_CLOSED) {
398-
return 0;
399-
}
400-
401-
DEBUG_printf("TCP recv: no cur_pkt, getting\n");
402-
_k_fifo_wait_non_empty(&socket->recv_q, K_FOREVER);
403-
struct net_pkt *pkt = _k_fifo_peek_head(&socket->recv_q);
404-
if (pkt == NULL) {
405-
DEBUG_printf("TCP recv: NULL return from fifo\n");
406-
continue;
407-
}
408-
// Drop head packet from queue
409-
k_fifo_get(&socket->recv_q, K_NO_WAIT);
394+
if (socket->state == STATE_PEER_CLOSED) {
395+
return 0;
396+
}
410397

411-
DEBUG_printf("TCP recv: new cur_pkt: %p\n", pkt);
412-
socket->cur_pkt = pkt;
398+
_k_fifo_wait_non_empty(&socket->recv_q, K_FOREVER);
399+
struct net_pkt *pkt = _k_fifo_peek_head(&socket->recv_q);
400+
if (pkt == NULL) {
401+
DEBUG_printf("TCP recv: NULL return from fifo\n");
402+
continue;
413403
}
414404

415-
struct net_buf *frag = socket->cur_pkt->frags;
405+
DEBUG_printf("TCP recv: cur_pkt: %p\n", pkt);
406+
407+
struct net_buf *frag = pkt->frags;
416408
if (frag == NULL) {
417409
printf("net_pkt has empty fragments on start!\n");
418410
assert(0);
@@ -430,15 +422,17 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int *
430422
if (recv_len != frag_len) {
431423
net_buf_pull(frag, recv_len);
432424
} else {
433-
frag = net_pkt_frag_del(socket->cur_pkt, NULL, frag);
425+
frag = net_pkt_frag_del(pkt, NULL, frag);
434426
if (frag == NULL) {
435-
DEBUG_printf("Finished processing pkt %p\n", socket->cur_pkt);
427+
DEBUG_printf("Finished processing pkt %p\n", pkt);
428+
// Drop head packet from queue
429+
k_fifo_get(&socket->recv_q, K_NO_WAIT);
430+
436431
// If "sent" flag was set, it's last packet and we reached EOF
437-
if (net_pkt_sent(socket->cur_pkt)) {
432+
if (net_pkt_sent(pkt)) {
438433
socket->state = STATE_PEER_CLOSED;
439434
}
440-
net_pkt_unref(socket->cur_pkt);
441-
socket->cur_pkt = NULL;
435+
net_pkt_unref(pkt);
442436
}
443437
}
444438
// Keep repeating while we're getting empty fragments

0 commit comments

Comments
 (0)