MCPcopy Create free account
hub / github.com/F-Stack/f-stack / loop

Function loop

example/main_epoll.c:59–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

57"</html>";
58
59int loop(void *arg)
60{
61 /* Wait for events to happen */
62
63 int nevents = ff_epoll_wait(epfd, events, MAX_EVENTS, 0);
64 int i;
65
66 for (i = 0; i < nevents; ++i) {
67 /* Handle new connect */
68 if (events[i].data.fd == sockfd) {
69 while (1) {
70 int nclientfd = ff_accept(sockfd, NULL, NULL);
71 if (nclientfd < 0) {
72 break;
73 }
74
75 /* Add to event list */
76 ev.data.fd = nclientfd;
77 ev.events = EPOLLIN;
78 if (ff_epoll_ctl(epfd, EPOLL_CTL_ADD, nclientfd, &ev) != 0) {
79 ff_log(FF_LOG_ERR, FF_LOGTYPE_FSTACK_APP, "ff_epoll_ctl failed:%d, %s\n", errno,
80 strerror(errno));
81 break;
82 }
83 }
84 } else {
85 if (events[i].events & EPOLLERR ) {
86 /* Simply close socket */
87 ff_epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
88 ff_close(events[i].data.fd);
89 } else if (events[i].events & EPOLLIN) {
90 char buf[256];
91 size_t readlen = ff_read( events[i].data.fd, buf, sizeof(buf));
92 if(readlen > 0) {
93 ff_write( events[i].data.fd, html, sizeof(html) - 1);
94 } else {
95 ff_epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
96 ff_close( events[i].data.fd);
97 }
98 } else {
99 ff_log(FF_LOG_WARNING, FF_LOGTYPE_FSTACK_APP, "unknown event: %8.8X\n", events[i].events);
100 }
101 }
102 }
103}
104
105int main(int argc, char * argv[])
106{

Callers

nothing calls this directly

Calls 7

ff_epoll_waitFunction · 0.85
ff_acceptFunction · 0.85
ff_epoll_ctlFunction · 0.85
ff_logFunction · 0.85
ff_closeFunction · 0.85
ff_readFunction · 0.85
ff_writeFunction · 0.85

Tested by

no test coverage detected