int ab_event_poll(int timer) { int i; int rc; int events; ab_connection_t *c; rc = epoll_wait(ep, event_list, nevents, timer); ab_log_debug("ab_event.c->ab_event_poll->epoll_wait() return:%d", rc); if (-1 == rc) { if (EINTR == errno) { return 0; } ab_log_error("ab_event.c->ab_event_poll->epoll_wait() failed, return:%d, errno:%d", rc, errno); return -1; } for (i = 0; i < rc; i++) { c = event_list[i].data.ptr; events = event_list[i].events; if (events & EPOLLIN) { c->event_read = 1; } if (events & EPOLLOUT) { c->event_write = 1; } if (events & (EPOLLERR | EPOLLHUP)) { c->event_error = 1; } ab_connection_handler(c); } return 0; }
void ab_connection_recv(ab_connection_t *c) { ab_buf_t *buf; int rc, n; buf = c->rcv_buf; while (1) { n = buf->last - buf->pos; if (n <= 0) { buf = ab_buf_enlarge(buf); if (buf == NULL) { ab_log_error("ab_buf_enlarge() failed %d", c->idx); c->error = 1; break; } /* recalucate available space */ n = buf->last - buf->pos; } ab_log_debug("recv buf %d", n); rc = recv(c->fd, buf->pos, n, 0); if (rc == 0) { ab_log_info("server close connection %d", c->idx); c->closed = 1; break; } if (rc < 0) { if (errno != EAGAIN && errno != EINTR) { ab_log_error("recv(%d) failed", c->fd); c->error = 1; } break; } ab_log_debug("recv %d bytes", rc); /* rc > 0 */ buf->pos += rc; } }
int ab_event_add_connection(ab_connection_t *c) { struct epoll_event ee; ee.events = EPOLLIN | EPOLLOUT | EPOLLET | EPOLLHUP; ee.data.ptr = (void *) c; if (-1 == epoll_ctl(ep, EPOLL_CTL_ADD, c->fd, &ee)) { ab_log_error("ab_event.c->ab_event_add_connection->epoll_ctl() add events failed, fd:%d", c->fd); return -1; } return 0; }
int ab_event_init() { ep = epoll_create(ab_concurrency); if (-1 == ep) { ab_log_error("ab_event.c->ab_event_init->epoll_create() failed"); return -1; } event_list = (struct epoll_event *)malloc(sizeof(struct epoll_event) * nevents); if (NULL == event_list) { close(ep); return -1; } return 0; }
int ab_event_del_connection(ab_connection_t *c) { struct epoll_event ee; if (-1 == c->fd) return 0; ee.events = 0; ee.data.ptr = NULL; if (-1 == epoll_ctl(ep, EPOLL_CTL_DEL, c->fd, &ee)) { ab_log_error("ab_event.c->ab_event_del_connection->epoll_ctl() del events failed, fd:%d", c->fd); return -1; } return 0; }
void ab_param_dump() { ab_log_error("-n %d", ab_requests); ab_log_error("-c %d", ab_concurrency); ab_log_error("-t 0x%x", ab_time_limit); ab_log_error("-i %s", ab_request_method); ab_log_error("-v %d", ab_verbose); ab_log_error("URI %s", ab_uri); ab_log_error("scheme %s", ab_scheme); ab_log_error("hostname %s", ab_hostname); ab_log_error("addr %s", inet_ntoa(ab_addr)); ab_log_error("port %hu", ab_port); ab_log_error("path %s", ab_path); }