int32_t Process(socket_t s) { _recv(s); _send(s); int32_t read_active = s->readable && !LINK_LIST_IS_EMPTY(s->pending_recv); int32_t write_active = s->writeable && !LINK_LIST_IS_EMPTY(s->pending_send); return (read_active || write_active) && s->isactived == 0; }
void on_write_active(socket_t s) { s->writeable = 1; if(!s->isactived && !LINK_LIST_IS_EMPTY(s->pending_send)) { s->isactived = 1; double_link_push(s->engine->actived,(struct double_link_node*)s); } }
int32_t epoll_loop(engine_t n,int32_t timeout) { assert(n); uint32_t ms; uint32_t tick = GetSystemMs(); uint32_t _timeout = tick + timeout; do{ while(!LINK_LIST_IS_EMPTY(n->actived)) { socket_t s = LINK_LIST_POP(socket_t,n->actived); s->isactived = 0; if(Process(s) && s->isactived == 0) { s->isactived = 1; LINK_LIST_PUSH_BACK(n->actived,s); } } ms = _timeout - tick; int32_t nfds = TEMP_FAILURE_RETRY(epoll_wait(n->poller_fd,n->events,MAX_SOCKET,ms)); if(nfds < 0) return -1; int32_t i; for(i = 0 ; i < nfds ; ++i) { socket_t sock = (socket_t)n->events[i].data.ptr; if(sock) { //套接口可读 if(n->events[i].events & EPOLLIN) { on_read_active(sock); } //套接口可写 if(n->events[i].events & EPOLLOUT) on_write_active(sock); if(n->events[i].events & EPOLLERR) { //套接口异常 } } } tick = GetSystemMs(); }while(tick < _timeout); return 0; }