Created
November 22, 2012 05:52
-
-
Save guoxiao/4129603 to your computer and use it in GitHub Desktop.
epoll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define MAX_EVENTS 10 | |
struct epoll_event ev, events[MAX_EVENTS]; | |
int listen_sock, conn_sock, nfds, epollfd; | |
/* Set up listening socket, 'listen_sock' (socket(), | |
bind(), listen()) */ | |
epollfd = epoll_create(10); | |
if (epollfd == -1) { | |
perror("epoll_create"); | |
exit(EXIT_FAILURE); | |
} | |
ev.events = EPOLLIN; | |
ev.data.fd = listen_sock; | |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == -1) { | |
perror("epoll_ctl: listen_sock"); | |
exit(EXIT_FAILURE); | |
} | |
for (;;) { | |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1); | |
if (nfds == -1) { | |
perror("epoll_pwait"); | |
exit(EXIT_FAILURE); | |
} | |
for (n = 0; n < nfds; ++n) { | |
if (events[n].data.fd == listen_sock) { | |
conn_sock = accept(listen_sock, | |
(struct sockaddr *) &local, &addrlen); | |
if (conn_sock == -1) { | |
perror("accept"); | |
exit(EXIT_FAILURE); | |
} | |
setnonblocking(conn_sock); | |
ev.events = EPOLLIN | EPOLLET; | |
ev.data.fd = conn_sock; | |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock, | |
&ev) == -1) { | |
perror("epoll_ctl: conn_sock"); | |
exit(EXIT_FAILURE); | |
} | |
} else { | |
do_use_fd(events[n].data.fd); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment