extmod/modlwip: lwip_tcp_send(): Full error handling.

This commit is contained in:
Paul Sokolovsky 2016-04-17 02:20:05 +03:00
parent d49a547064
commit b830f4c610
1 changed files with 17 additions and 7 deletions

View File

@ -409,15 +409,27 @@ STATIC mp_uint_t lwip_udp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
return (mp_uint_t) result; return (mp_uint_t) result;
} }
// For use in stream virtual methods
#define STREAM_ERROR_CHECK(socket) \
if (socket->state < 0) { \
*_errno = error_lookup_table[-socket->state]; \
return MP_STREAM_ERROR; \
} \
assert(socket->pcb.tcp);
// Helper function for send/sendto to handle TCP packets // Helper function for send/sendto to handle TCP packets
STATIC mp_uint_t lwip_tcp_send(lwip_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno) { STATIC mp_uint_t lwip_tcp_send(lwip_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno) {
// Check for any pending errors
STREAM_ERROR_CHECK(socket);
u16_t available = tcp_sndbuf(socket->pcb.tcp); u16_t available = tcp_sndbuf(socket->pcb.tcp);
if (available == 0) { if (available == 0) {
// Non-blocking socket // Non-blocking socket
if (socket->timeout == 0) { if (socket->timeout == 0) {
*_errno = EAGAIN; *_errno = EAGAIN;
return -1; return MP_STREAM_ERROR;
} }
mp_uint_t start = mp_hal_ticks_ms(); 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
while (socket->state >= STATE_CONNECTED && (available = tcp_sndbuf(socket->pcb.tcp)) < 16) { while (socket->state >= STATE_CONNECTED && (available = tcp_sndbuf(socket->pcb.tcp)) < 16) {
if (socket->timeout != -1 && mp_hal_ticks_ms() - start > socket->timeout) { if (socket->timeout != -1 && mp_hal_ticks_ms() - start > socket->timeout) {
*_errno = ETIMEDOUT; *_errno = ETIMEDOUT;
return -1; return MP_STREAM_ERROR;
} }
poll_sockets(); poll_sockets();
} }
if (socket->state < 0) { // While we waited, something could happen
*_errno = error_lookup_table[-socket->state]; STREAM_ERROR_CHECK(socket);
return -1;
}
} }
u16_t write_len = MIN(available, len); 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
if (err != ERR_OK) { if (err != ERR_OK) {
*_errno = error_lookup_table[-err]; *_errno = error_lookup_table[-err];
return -1; return MP_STREAM_ERROR;
} }
return write_len; return write_len;