static int swReactorKqueue_wait(swReactor *reactor, struct timeval *timeo) { swEvent event; swFd fd_; swReactorKqueue *object = reactor->object; swReactor_handle handle; int i, n, ret; struct timespec t; struct timespec *t_ptr; bzero(&t, sizeof(t)); if (reactor->timeout_msec == 0) { if (timeo == NULL) { reactor->timeout_msec = -1; } else { reactor->timeout_msec = timeo->tv_sec * 1000 + timeo->tv_usec / 1000; } } reactor->start = 1; while (reactor->running > 0) { if (reactor->onBegin != NULL) { reactor->onBegin(reactor); } if (reactor->timeout_msec > 0) { t.tv_sec = reactor->timeout_msec / 1000; t.tv_nsec = (reactor->timeout_msec - t.tv_sec * 1000) * 1000; t_ptr = &t; } else { t_ptr = NULL; } n = kevent(object->epfd, NULL, 0, object->events, object->event_max, t_ptr); if (n < 0) { swTrace("kqueue error.EP=%d | Errno=%d\n", object->epfd, errno); if (swReactor_error(reactor) < 0) { swWarn("Kqueue[#%d] Error: %s[%d]", reactor->id, strerror(errno), errno); return SW_ERR; } else { continue; } } else if (n == 0) { if (reactor->onTimeout != NULL) { reactor->onTimeout(reactor); } continue; } for (i = 0; i < n; i++) { swTrace("n %d events.", n); if (object->events[i].udata) { memcpy(&fd_, &(object->events[i].udata), sizeof(fd_)); event.fd = fd_.fd; event.from_id = reactor->id; event.type = fd_.fdtype; event.socket = swReactor_get(reactor, event.fd); //read if (object->events[i].filter == EVFILT_READ) { if (event.socket->removed) { continue; } handle = swReactor_getHandle(reactor, SW_EVENT_READ, event.type); ret = handle(reactor, &event); if (ret < 0) { swSysError("kqueue event read socket#%d handler failed.", event.fd); } } //write else if (object->events[i].filter == EVFILT_WRITE) { if (event.socket->removed) { continue; } handle = swReactor_getHandle(reactor, SW_EVENT_WRITE, event.type); ret = handle(reactor, &event); if (ret < 0) { swSysError("kqueue event write socket#%d handler failed.", event.fd); } } else { swWarn("unknown event filter[%d].", object->events[i].filter); } } } if (reactor->onFinish != NULL) { reactor->onFinish(reactor); } if (reactor->once) { break; } } return 0; }
static int swReactorPoll_wait(swReactor *reactor, struct timeval *_timeo) { swReactorPoll *object = reactor->object; swDataHead event; swReactor_handle handle; struct timeval timeo = *_timeo; int ret; int i; while (SwooleG.running > 0) { ret = poll(object->events, reactor->event_num, timeo.tv_sec * 1000 + timeo.tv_usec / 1000); if (ret < 0) { if (swReactor_error(reactor) < 0) { swWarn("poll error. Error: %s[%d]", strerror(errno), errno); } continue; } else if (ret == 0) { if(reactor->onTimeout != NULL) { reactor->onTimeout(reactor); } continue; } else { for (i = 0; i < reactor->event_num; i++) { event.fd = object->events[i].fd; event.from_id = reactor->id; event.type = object->fds[i].fdtype; swTrace("Event: fd=%d|from_id=%d|type=%d", event.fd, reactor->id, object->fds[i].fdtype); //in if (object->events[i].revents & POLLIN) { handle = swReactor_getHandle(reactor, SW_EVENT_READ, event.type); ret = handle(reactor, &event); if (ret < 0) { swWarn("poll[POLLIN] handler failed. fd=%d|errno=%d.Error: %s[%d]", event.fd, errno, strerror(errno), errno); } } //out if (object->events[i].revents & POLLOUT) { if (event.fd > 0) { handle = swReactor_getHandle(reactor, SW_EVENT_WRITE, event.type); ret = handle(reactor, &event); if (ret < 0) { swWarn("poll[POLLOUT] handler failed. fd=%d|errno=%d.Error: %s[%d]", event.fd, errno, strerror(errno), errno); } } } //error if (object->events[i].revents & (POLLHUP | POLLERR)) { if (event.fd > 0) { handle = swReactor_getHandle(reactor, SW_EVENT_READ, event.type); ret = handle(reactor, &event); if (ret < 0) { swWarn("poll[POLLERR] handler failed. fd=%d|errno=%d.Error: %s[%d]", event.fd, errno, strerror(errno), errno); } } } } if (reactor->onFinish != NULL) { reactor->onFinish(reactor); } } } return SW_OK; }
static int swReactorKqueue_wait(swReactor *reactor, struct timeval *timeo) { swEvent event; swFd fd_; swReactorKqueue *this = reactor->object; swReactor_handle handle; int i, n, ret; struct timespec t; t.tv_sec = timeo->tv_sec; t.tv_nsec = timeo->tv_usec; while (SwooleG.running > 0) { n = kevent(this->epfd, NULL, 0, this->events, this->event_max, &t); if (n < 0) { //swTrace("kqueue error.EP=%d | Errno=%d\n", this->epfd, errno); if(swReactor_error(reactor) < 0) { swWarn("Kqueue[#%d] Error: %s[%d]", reactor->id, strerror(errno), errno); return SW_ERR; } else { continue; } } else if (n == 0) { if(reactor->onTimeout != NULL) { reactor->onTimeout(reactor); } continue; } for (i = 0; i < n; i++) { if (this->events[i].udata) { memcpy(&fd_, &(this->events[i].udata), sizeof(fd_)); event.fd = fd_.fd; event.from_id = reactor->id; event.type = fd_.fdtype; //read if (this->events[i].filter == EVFILT_READ) { handle = swReactor_getHandle(reactor, SW_EVENT_READ, event.type); ret = handle(reactor, &event); if (ret < 0) { swWarn("kqueue event handler fail. fd=%d|errno=%d.Error: %s[%d]", event.fd, errno, strerror(errno), errno); } } //write else if (this->events[i].filter == EVFILT_WRITE) { handle = swReactor_getHandle(reactor, SW_EVENT_WRITE, event.type); ret = handle(reactor, &event); if (ret < 0) { swWarn("kqueue event handler fail. fd=%d|errno=%d.Error: %s[%d]", event.fd, errno, strerror(errno), errno); } } else { swWarn("kqueue event unknow filter=%d", this->events[i].filter); } } } if(reactor->onFinish != NULL) { reactor->onFinish(reactor); } } return 0; }
int swReactorEpoll_wait(swReactor *reactor, struct timeval *timeo) { swEvent ev; swFd fd_; swReactorEpoll *object = reactor->object; swReactor_handle handle; int i, n, ret, usec; if (timeo == NULL) { usec = SW_MAX_UINT; } else { usec = timeo->tv_sec * 1000 + timeo->tv_usec / 1000; } while (swoole_running > 0) { n = epoll_wait(object->epfd, object->events, object->event_max + 1, usec); if (n < 0) { if (swReactor_error(reactor) < 0) { swWarn("Epoll[#%d] Error: %s[%d]", reactor->id, strerror(errno), errno); return SW_ERR; } else { continue; } } else if (n == 0) { reactor->timeout = 1; continue; } for (i = 0; i < n; i++) { //取出事件 memcpy(&fd_, &(object->events[i].data.u64), sizeof(fd_)); ev.fd = fd_.fd; ev.from_id = reactor->id; ev.type = fd_.fdtype; //read if (object->events[i].events & EPOLLIN) { //read handle = swReactor_getHandle(reactor, SW_EVENT_READ, ev.type); ret = handle(reactor, &ev); if (ret < 0) { swWarn("[Reactor#%d] epoll handle fail. fd=%d|type=%d", reactor->id, ev.fd, ev.type); } } //error if ((object->events[i].events & (EPOLLRDHUP | EPOLLERR | EPOLLHUP))) { handle = swReactor_getHandle(reactor, SW_EVENT_ERROR, ev.type); ret = handle(reactor, &ev); if (ret < 0) { swWarn("[Reactor#%d] epoll handle fail. fd=%d|type=%d", reactor->id, ev.fd, ev.type); } } //write if ((object->events[i].events & EPOLLOUT)) { handle = swReactor_getHandle(reactor, SW_EVENT_WRITE, ev.type); ret = handle(reactor, &ev); if (ret < 0) { swWarn("[Reactor#%d] epoll event[type=SW_EVENT_WRITE] handler fail. fd=%d|errno=%d", reactor->id, ev.type, ev.fd, errno); } } } reactor->timeout = 0; } return 0; }
int swReactorEpoll_wait(swReactor *reactor, struct timeval *timeo) { swEvent ev; swReactorEpoll *object = reactor->object; swReactor_handle handle; int i, n, ret, msec; int reactor_id = reactor->id; int epoll_fd = object->epfd; int max_event_num = reactor->max_event_num; struct epoll_event *events = object->events; if (reactor->timeout_msec == 0) { if (timeo == NULL) { reactor->timeout_msec = -1; } else { reactor->timeout_msec = timeo->tv_sec * 1000 + timeo->tv_usec / 1000; } } while (SwooleG.running > 0) { msec = reactor->timeout_msec; n = epoll_wait(epoll_fd, events, max_event_num, msec); if (n < 0) { if (swReactor_error(reactor) < 0) { swWarn("[Reactor#%d] epoll_wait failed. Error: %s[%d]", reactor_id, strerror(errno), errno); return SW_ERR; } else { continue; } } else if (n == 0) { if (reactor->onTimeout != NULL) { reactor->onTimeout(reactor); } continue; } for (i = 0; i < n; i++) { ev.fd = events[i].data.u64; ev.from_id = reactor_id; ev.type = events[i].data.u64 >> 32; //read if (events[i].events & EPOLLIN) { //read handle = swReactor_getHandle(reactor, SW_EVENT_READ, ev.type); ret = handle(reactor, &ev); if (ret < 0) { swWarn("[Reactor#%d] epoll [EPOLLIN] handle failed. fd=%d. Error: %s[%d]", reactor_id, ev.fd, strerror(errno), errno); } } //write, ev.fd == 0, connection is closed. if ((events[i].events & EPOLLOUT) && ev.fd > 0) { handle = swReactor_getHandle(reactor, SW_EVENT_WRITE, ev.type); ret = handle(reactor, &ev); if (ret < 0) { swWarn("[Reactor#%d] epoll [EPOLLOUT] handle failed. fd=%d. Error: %s[%d]", reactor_id, ev.fd, strerror(errno), errno); } } //error #ifndef NO_EPOLLRDHUP if ((events[i].events & (EPOLLRDHUP | EPOLLERR | EPOLLHUP)) && ev.fd > 0) #else if ((events[i].events & (EPOLLERR | EPOLLHUP)) && ev.fd > 0) #endif { handle = swReactor_getHandle(reactor, SW_EVENT_ERROR, ev.type); ret = handle(reactor, &ev); if (ret < 0) { swWarn("[Reactor#%d] epoll [EPOLLERR] handle failed. fd=%d. Error: %s[%d]", reactor_id, ev.fd, strerror(errno), errno); } } } if (reactor->onFinish != NULL) { reactor->onFinish(reactor); } } return 0; }
static int swReactorPoll_wait(swReactor *reactor, struct timeval *timeo) { swReactorPoll *object = reactor->object; swEvent event; swReactor_handle handle; int ret, msec, i; if (reactor->timeout_msec == 0) { if (timeo == NULL) { reactor->timeout_msec = -1; } else { reactor->timeout_msec = timeo->tv_sec * 1000 + timeo->tv_usec / 1000; } } while (reactor->running > 0) { msec = reactor->timeout_msec; ret = poll(object->events, reactor->event_num, msec); if (ret < 0) { if (swReactor_error(reactor) < 0) { swWarn("poll error. Error: %s[%d]", strerror(errno), errno); } continue; } else if (ret == 0) { if (reactor->onTimeout != NULL) { reactor->onTimeout(reactor); } continue; } else { for (i = 0; i < reactor->event_num; i++) { event.fd = object->events[i].fd; event.from_id = reactor->id; event.type = object->fds[i].fdtype; event.socket = swReactor_get(reactor, event.fd); swTrace("Event: fd=%d|from_id=%d|type=%d", event.fd, reactor->id, object->fds[i].fdtype); //in if ((object->events[i].revents & POLLIN) && !event.socket->removed) { handle = swReactor_getHandle(reactor, SW_EVENT_READ, event.type); ret = handle(reactor, &event); if (ret < 0) { swWarn("poll[POLLIN] handler failed. fd=%d. Error: %s[%d]", event.fd, strerror(errno), errno); } } //out if ((object->events[i].revents & POLLOUT) && !event.socket->removed) { handle = swReactor_getHandle(reactor, SW_EVENT_WRITE, event.type); ret = handle(reactor, &event); if (ret < 0) { swWarn("poll[POLLOUT] handler failed. fd=%d. Error: %s[%d]", event.fd, strerror(errno), errno); } } //error if ((object->events[i].revents & (POLLHUP | POLLERR)) && !event.socket->removed) { //ignore ERR and HUP, because event is already processed at IN and OUT handler. if ((object->events[i].revents & POLLIN) || (object->events[i].revents & POLLOUT)) { continue; } handle = swReactor_getHandle(reactor, SW_EVENT_ERROR, event.type); ret = handle(reactor, &event); if (ret < 0) { swWarn("poll[POLLERR] handler failed. fd=%d. Error: %s[%d]", event.fd, strerror(errno), errno); } } } if (reactor->onFinish != NULL) { reactor->onFinish(reactor); } } } return SW_OK; }