Skip to content

Commit daaa9b9

Browse files
committed
implement poll
1 parent 820de7e commit daaa9b9

2 files changed

Lines changed: 121 additions & 0 deletions

File tree

libogc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_library(ogc STATIC
1616
ogc_sockets/soc_connect.c
1717
ogc_sockets/soc_shutdown.c
1818
ogc_sockets/soc_accept.c
19+
ogc_sockets/soc_poll.c
1920

2021
console.c
2122
lwp_queue.c

libogc/ogc_sockets/soc_poll.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#include "soc_common.h"
2+
#include <sys/socket.h>
3+
4+
#include "poll.h"
5+
6+
#include <stdlib.h>
7+
8+
#ifdef HW_RVL
9+
__attribute__((weak)) size_t __ogc_pollfd_sb_max_fds = 64;
10+
11+
int poll(struct pollfd *fds, nfds_t nfds, int timeout) {
12+
struct pollsd *fds2;
13+
int ret = 0;
14+
15+
if(fds == NULL) {
16+
errno = EFAULT;
17+
return -1;
18+
}
19+
20+
if(nfds <= __ogc_pollfd_sb_max_fds)
21+
fds2 = (struct pollsd *)alloca(nfds * sizeof(struct pollfd));
22+
else
23+
fds2 = (struct pollsd *)malloc(nfds * sizeof(struct pollfd));
24+
25+
if(fds2 == NULL) {
26+
errno = ENOMEM;
27+
return -1;
28+
}
29+
30+
for(nfds_t i = 0; i < nfds; i++) {
31+
fds2[i].events = fds[i].events;
32+
fds2[i].revents = 0;
33+
if(fds[i].fd < 0) {
34+
fds2[i].socket = -1;
35+
} else {
36+
fds2[i].socket = soc_get_fd(fds[i].fd);
37+
if(fds2[i].socket == -1) {
38+
ret = -1;
39+
break;
40+
}
41+
}
42+
}
43+
44+
if(ret != -1)
45+
ret = net_poll(fds2, nfds, timeout);
46+
47+
if (ret < 0 ) {
48+
errno = -ret;
49+
ret = -1;
50+
}
51+
52+
if(ret != -1) {
53+
for(nfds_t i = 0; i < nfds; i++) {
54+
fds[i].revents = (short)fds2[i].revents;
55+
}
56+
}
57+
58+
if(nfds > __ogc_pollfd_sb_max_fds)
59+
free(fds2);
60+
return ret;
61+
}
62+
#endif
63+
64+
#ifdef HW_DOL
65+
int poll (struct pollfd *const fds_, nfds_t const nfds_, int const timeout_)
66+
{
67+
fd_set readFds;
68+
fd_set writeFds;
69+
fd_set exceptFds;
70+
71+
FD_ZERO (&readFds);
72+
FD_ZERO (&writeFds);
73+
FD_ZERO (&exceptFds);
74+
75+
for (nfds_t i = 0; i < nfds_; ++i)
76+
{
77+
if (fds_[i].events & POLLIN)
78+
FD_SET (soc_get_fd(fds_[i].fd), &readFds);
79+
if (fds_[i].events & POLLOUT)
80+
FD_SET (soc_get_fd(fds_[i].fd), &writeFds);
81+
}
82+
83+
struct timeval tv;
84+
tv.tv_sec = timeout_ / 1000;
85+
tv.tv_usec = (timeout_ % 1000) * 1000;
86+
const int rc = net_select (nfds_, &readFds, &writeFds, &exceptFds, &tv);
87+
if (rc < 0)
88+
return rc;
89+
90+
int count = 0;
91+
for (nfds_t i = 0; i < nfds_; ++i)
92+
{
93+
bool counted = false;
94+
fds_[i].revents = 0;
95+
96+
if (FD_ISSET (soc_get_fd(fds_[i].fd), &readFds))
97+
{
98+
counted = true;
99+
fds_[i].revents |= POLLIN;
100+
}
101+
102+
if (FD_ISSET (soc_get_fd(fds_[i].fd), &writeFds))
103+
{
104+
counted = true;
105+
fds_[i].revents |= POLLOUT;
106+
}
107+
108+
if (FD_ISSET (soc_get_fd(fds_[i].fd), &exceptFds))
109+
{
110+
counted = true;
111+
fds_[i].revents |= POLLERR;
112+
}
113+
114+
if (counted)
115+
++count;
116+
}
117+
118+
return count;
119+
}
120+
#endif

0 commit comments

Comments
 (0)