Created
June 26, 2017 02:44
-
-
Save caisan/d398d0ec3c0349248874c055942b4403 to your computer and use it in GitHub Desktop.
epoll server code snippet
This file contains 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
while (1) { | |
time = zv_find_timer(); | |
debug("wait time = %d", time); | |
n = zv_epoll_wait(epfd, events, MAXEVENTS, time); | |
zv_handle_expire_timers(); | |
log_info("loop n:%d\n",n); | |
for (i = 0; i < n; i++) { | |
zv_http_request_t *r = (zv_http_request_t *)events[i].data.ptr; | |
fd = r->fd; | |
log_info("fd:%d\n", fd); | |
if (listenfd == fd) { | |
/* we hava one or more incoming connections */ | |
int infd; | |
while(1) { | |
infd = accept(listenfd, (struct sockaddr *)&clientaddr, &inlen); | |
log_info("Accept fd:%d\n", infd); | |
if (infd < 0) { | |
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { | |
/* we have processed all incoming connections */ | |
break; | |
} else { | |
log_err("accept"); | |
break; | |
} | |
} | |
rc = make_socket_non_blocking(infd); | |
check(rc == 0, "make_socket_non_blocking"); | |
log_info("new connection fd %d", infd); | |
zv_http_request_t *request = (zv_http_request_t *)malloc(sizeof(zv_http_request_t)); | |
if (request == NULL) { | |
log_err("malloc(sizeof(zv_http_request_t))"); | |
break; | |
} | |
zv_init_request_t(request, infd, epfd, &cf); | |
event.data.ptr = (void *)request; | |
event.events = EPOLLIN | EPOLLET | EPOLLONESHOT; | |
zv_epoll_add(epfd, infd, &event); | |
zv_add_timer(request, TIMEOUT_DEFAULT, zv_http_close_conn); | |
} // end of while of accept | |
} else { | |
if ((events[i].events & EPOLLERR) || | |
(events[i].events & EPOLLHUP) || | |
(!(events[i].events & EPOLLIN))) { | |
log_err("epoll error fd: %d", r->fd); | |
close(fd); | |
continue; | |
} | |
log_info("new data from fd %d", fd); | |
//rc = threadpool_add(tp, do_request, events[i].data.ptr); | |
//check(rc == 0, "threadpool_add"); | |
do_request(events[i].data.ptr); | |
} | |
} //end of for | |
} // end of while(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment