From cc42b7c88b0b2b11e1e6d389c773e36545e2aef0 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Fri, 17 Sep 2021 11:48:37 +1000 Subject: [PATCH] unix/modusocket: Support MP_STREAM_POLL in unix socket_ioctl. Allows asyncio reading of network sockets when MICROPY_PY_USELECT is used in the build configuration. --- ports/unix/modusocket.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ports/unix/modusocket.c b/ports/unix/modusocket.c index 1c9ef33622..951cb7a21c 100644 --- a/ports/unix/modusocket.c +++ b/ports/unix/modusocket.c @@ -46,6 +46,7 @@ #include "py/builtin.h" #include "py/mphal.h" #include "py/mpthread.h" +#include /* The idea of this module is to implement reasonable minimum of @@ -144,6 +145,29 @@ STATIC mp_uint_t socket_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, i case MP_STREAM_GET_FILENO: return self->fd; + #if MICROPY_PY_USELECT + case MP_STREAM_POLL: { + mp_uint_t ret = 0; + uint8_t pollevents = 0; + if (arg & MP_STREAM_POLL_RD) { + pollevents |= POLLIN; + } + if (arg & MP_STREAM_POLL_WR) { + pollevents |= POLLOUT; + } + struct pollfd pfd = { .fd = self->fd, .events = pollevents }; + if (poll(&pfd, 1, 0) > 0) { + if (pfd.revents & POLLIN) { + ret |= MP_STREAM_POLL_RD; + } + if (pfd.revents & POLLOUT) { + ret |= MP_STREAM_POLL_WR; + } + } + return ret; + } + #endif + default: *errcode = MP_EINVAL; return MP_STREAM_ERROR;