Skip to content

Commit

Permalink
extmod/modlwip: socket polling behaviour fixes
Browse files Browse the repository at this point in the history
This adds support for returning POLLHUP and POLLERR flags. Also, for
listening sockets, also returns POLLIN when a client is connected.

Fixes micropython#3449
  • Loading branch information
Richard Kojedzinszky committed Nov 25, 2017
1 parent 50cffcf commit 3eefd88
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions extmod/modlwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,8 @@ STATIC mp_obj_t lwip_socket_listen(mp_obj_t self_in, mp_obj_t backlog_in) {
socket->pcb.tcp = new_pcb;
tcp_accept(new_pcb, _lwip_tcp_accept);

socket->state = STATE_CONNECTING;

return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(lwip_socket_listen_obj, lwip_socket_listen);
Expand Down Expand Up @@ -1168,16 +1170,17 @@ STATIC mp_uint_t lwip_socket_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_
ret |= MP_STREAM_POLL_RD;
}

if (flags & MP_STREAM_POLL_WR && tcp_sndbuf(socket->pcb.tcp) > 0) {
if (flags & MP_STREAM_POLL_WR && (socket->pcb.tcp == NULL || tcp_sndbuf(socket->pcb.tcp) > 0)) {
ret |= MP_STREAM_POLL_WR;
}

if (socket->state == STATE_PEER_CLOSED) {
// Peer-closed socket is both readable and writable: read will
// return EOF, write - error. Without this poll will hang on a
// socket which was closed by peer.
ret |= flags & (MP_STREAM_POLL_RD | MP_STREAM_POLL_WR);
}
if (socket->state != STATE_CONNECTING && socket->state != STATE_CONNECTED) {
ret |= MP_STREAM_POLL_HUP;
}

if (socket->state < 0) {
ret |= MP_STREAM_POLL_ERR;
}

} else {
*errcode = MP_EINVAL;
Expand Down

0 comments on commit 3eefd88

Please sign in to comment.