int swReactorEpoll_add(swReactor *reactor, int fd, int fdtype) { swReactorEpoll *object = reactor->object; struct epoll_event e; swFd fd_; int ret; bzero(&e, sizeof(struct epoll_event)); fd_.fd = fd; fd_.fdtype = swReactor_fdtype(fdtype); e.events = swReactorEpoll_event_set(fdtype); memcpy(&(e.data.u64), &fd_, sizeof(fd_)); ret = epoll_ctl(object->epfd, EPOLL_CTL_ADD, fd, &e); if (ret < 0) { swWarn("add event failed. Error: %s[%d]", strerror(errno), errno); return SW_ERR; } if (swReactor_add(reactor, fd, fdtype) < 0) { return SW_ERR; } swTraceLog(SW_TRACE_EVENT, "add event[reactor_id=%d|fd=%d]", reactor->id, fd); reactor->event_num++; return SW_OK; }
static int swReactorKqueue_add(swReactor *reactor, int fd, int fdtype) { swReactorKqueue *this = reactor->object; struct kevent e; swFd fd_; int ret; bzero(&e, sizeof(e)); int fflags = 0; fd_.fd = fd; fd_.fdtype = swReactor_fdtype(fdtype); swReactor_add(reactor, fd, fdtype); if (swReactor_event_read(fdtype)) { #ifdef NOTE_EOF fflags = NOTE_EOF; #endif EV_SET(&e, fd, EVFILT_READ, EV_ADD, fflags, 0, NULL); memcpy(&e.udata, &fd_, sizeof(swFd)); ret = kevent(this->epfd, &e, 1, NULL, 0, NULL); if (ret < 0) { swSysError("add events[fd=%d#%d, type=%d, events=read] failed.", fd, reactor->id, fd_.fdtype); swReactor_del(reactor, fd); return SW_ERR; } } if (swReactor_event_write(fdtype)) { EV_SET(&e, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL); memcpy(&e.udata, &fd_, sizeof(swFd)); ret = kevent(this->epfd, &e, 1, NULL, 0, NULL); if (ret < 0) { swSysError("add events[fd=%d#%d, type=%d, events=write] failed.", fd, reactor->id, fd_.fdtype); swReactor_del(reactor, fd); return SW_ERR; } } swTrace("[THREAD #%d]EP=%d|FD=%d, events=%d", SwooleTG.id, this->epfd, fd, fdtype); reactor->event_num++; return SW_OK; }
static int swReactorPoll_add(swReactor *reactor, int fd, int fdtype) { if (swReactorPoll_exist(reactor, fd)) { swWarn("fd#%d is already exists.", fd); return SW_ERR; } if (swReactor_add(reactor, fd, fdtype) < 0) { return SW_ERR; } swReactorPoll *object = reactor->object; int cur = reactor->event_num; if (reactor->event_num == object->max_fd_num) { swWarn("too many connection, more than %d", object->max_fd_num); return SW_ERR; } swTrace("fd=%d, fdtype=%d", fd, fdtype); object->fds[cur].fdtype = swReactor_fdtype(fdtype); object->events[cur].fd = fd; object->events[cur].events = 0; if (swReactor_event_read(fdtype)) { object->events[cur].events |= POLLIN; } if (swReactor_event_write(fdtype)) { object->events[cur].events |= POLLOUT; } if (swReactor_event_error(fdtype)) { object->events[cur].events |= POLLHUP; } reactor->event_num++; return SW_OK; }