Skip to content

Commit

Permalink
network: implement net_select() for the Wii (#192)
Browse files Browse the repository at this point in the history
poll() is generally more efficient and does not have a limitation on the
number of file descriptors, but many libraries and programs out there
still use select(), so let's make it easier to port them too
  • Loading branch information
mardy authored and Extrems committed Feb 11, 2025
1 parent 91308d5 commit c960155
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions libogc/network_wii.c
Original file line number Diff line number Diff line change
Expand Up @@ -992,8 +992,54 @@ s32 net_close(s32 s)

s32 net_select(s32 maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout)
{
// not yet implemented
return -EINVAL;
struct pollsd sds[FD_SETSIZE];
int i, n_sds = 0;
s32 timeout_msecs;
s32 ret;

memset(sds, 0, sizeof(sds));
for (i = 0; i < maxfdp1; i++) {
bool watched = false;

if (readset && FD_ISSET(i, readset)) {
sds[n_sds].events |= POLLIN;
watched = true;
}
if (writeset && FD_ISSET(i, writeset)) {
sds[n_sds].events |= POLLOUT;
watched = true;
}
if (exceptset && FD_ISSET(i, exceptset)) {
sds[n_sds].events |= POLLPRI;
watched = true;
}

if (watched) {
sds[n_sds++].socket = i;
}
}

timeout_msecs = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
ret = net_poll(sds, n_sds, timeout_msecs);
if (ret <= 0) return ret;

ret = 0; /* Return the total number of 1 bits in the three fd sets */
for (i = 0; i < n_sds; i++) {
u8 fd = sds[i].socket;
if (sds[i].events & POLLIN) {
if (sds[i].revents & POLLIN) ret++;
else FD_CLR(fd, readset);
}
if (sds[i].events & POLLOUT) {
if (sds[i].revents & POLLOUT) ret++;
else FD_CLR(fd, writeset);
}
if (sds[i].events & POLLPRI) {
if (sds[i].revents & (POLLERR | POLLPRI | POLLHUP)) ret++;
else FD_CLR(fd, exceptset);
}
}
return ret;
}

s32 net_getsockname(s32 s, struct sockaddr *name, socklen_t *namelen)
Expand Down

0 comments on commit c960155

Please sign in to comment.