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.
This commit is contained in:
Andrew Leech 2021-09-17 11:48:37 +10:00 committed by Damien George
parent 2ceeabf180
commit cc42b7c88b
1 changed files with 24 additions and 0 deletions

View File

@ -46,6 +46,7 @@
#include "py/builtin.h"
#include "py/mphal.h"
#include "py/mpthread.h"
#include <poll.h>
/*
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;