summaryrefslogtreecommitdiff
path: root/extmod/modlwip.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-04-17 02:20:05 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-04-17 02:20:05 +0300
commitb830f4c61089996bde73400ebbcefe88cb7a67f1 (patch)
treed1ce2af66d12dcc195f86e14507974aeec0d010f /extmod/modlwip.c
parentd49a5470640d29ce9ee7f2ebedbf9f2fcd8cc524 (diff)
extmod/modlwip: lwip_tcp_send(): Full error handling.
Diffstat (limited to 'extmod/modlwip.c')
-rw-r--r--extmod/modlwip.c24
1 files changed, 17 insertions, 7 deletions
diff --git a/extmod/modlwip.c b/extmod/modlwip.c
index 4499aa9b3..e4abe23ed 100644
--- a/extmod/modlwip.c
+++ b/extmod/modlwip.c
@@ -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;
}
+// 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
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);
if (available == 0) {
// Non-blocking socket
if (socket->timeout == 0) {
*_errno = EAGAIN;
- return -1;
+ return MP_STREAM_ERROR;
}
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) {
if (socket->timeout != -1 && mp_hal_ticks_ms() - start > socket->timeout) {
*_errno = ETIMEDOUT;
- return -1;
+ return MP_STREAM_ERROR;
}
poll_sockets();
}
- if (socket->state < 0) {
- *_errno = error_lookup_table[-socket->state];
- return -1;
- }
+ // While we waited, something could happen
+ STREAM_ERROR_CHECK(socket);
}
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) {
*_errno = error_lookup_table[-err];
- return -1;
+ return MP_STREAM_ERROR;
}
return write_len;