Skip to content

Commit b830f4c

Browse files
committed
extmod/modlwip: lwip_tcp_send(): Full error handling.
1 parent d49a547 commit b830f4c

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

extmod/modlwip.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,15 +409,27 @@ STATIC mp_uint_t lwip_udp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
409409
return (mp_uint_t) result;
410410
}
411411

412+
// For use in stream virtual methods
413+
#define STREAM_ERROR_CHECK(socket) \
414+
if (socket->state < 0) { \
415+
*_errno = error_lookup_table[-socket->state]; \
416+
return MP_STREAM_ERROR; \
417+
} \
418+
assert(socket->pcb.tcp);
419+
420+
412421
// Helper function for send/sendto to handle TCP packets
413422
STATIC mp_uint_t lwip_tcp_send(lwip_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno) {
423+
// Check for any pending errors
424+
STREAM_ERROR_CHECK(socket);
425+
414426
u16_t available = tcp_sndbuf(socket->pcb.tcp);
415427

416428
if (available == 0) {
417429
// Non-blocking socket
418430
if (socket->timeout == 0) {
419431
*_errno = EAGAIN;
420-
return -1;
432+
return MP_STREAM_ERROR;
421433
}
422434

423435
mp_uint_t start = mp_hal_ticks_ms();
@@ -430,15 +442,13 @@ STATIC mp_uint_t lwip_tcp_send(lwip_socket_obj_t *socket, const byte *buf, mp_ui
430442
while (socket->state >= STATE_CONNECTED && (available = tcp_sndbuf(socket->pcb.tcp)) < 16) {
431443
if (socket->timeout != -1 && mp_hal_ticks_ms() - start > socket->timeout) {
432444
*_errno = ETIMEDOUT;
433-
return -1;
445+
return MP_STREAM_ERROR;
434446
}
435447
poll_sockets();
436448
}
437449

438-
if (socket->state < 0) {
439-
*_errno = error_lookup_table[-socket->state];
440-
return -1;
441-
}
450+
// While we waited, something could happen
451+
STREAM_ERROR_CHECK(socket);
442452
}
443453

444454
u16_t write_len = MIN(available, len);
@@ -447,7 +457,7 @@ STATIC mp_uint_t lwip_tcp_send(lwip_socket_obj_t *socket, const byte *buf, mp_ui
447457

448458
if (err != ERR_OK) {
449459
*_errno = error_lookup_table[-err];
450-
return -1;
460+
return MP_STREAM_ERROR;
451461
}
452462

453463
return write_len;

0 commit comments

Comments
 (0)