int swReactor_setHandle(swReactor *reactor, int _fdtype, swReactor_handle handle) { int fdtype = swReactor_fdtype(_fdtype); if (fdtype >= SW_MAX_FDTYPE) { swWarn("fdtype > SW_MAX_FDTYPE[%d]", SW_MAX_FDTYPE); return SW_ERR; } if (swReactor_event_read(_fdtype)) { reactor->handle[fdtype] = handle; } else if (swReactor_event_write(_fdtype)) { reactor->write_handle[fdtype] = handle; } else if (swReactor_event_error(_fdtype)) { reactor->error_handle[fdtype] = handle; } else { swWarn("unknow fdtype"); return SW_ERR; } return SW_OK; }
static int swReactorPoll_add(swReactor *reactor, int fd, int fdtype) { swReactorPoll *object = reactor->object; int cur = reactor->event_num; if (reactor->event_num == object->max_fd_num) { swError("too many connection, more than %d", object->max_fd_num); return SW_ERR; } object->fds[cur].fdtype = swReactor_fdtype(fdtype); object->events[cur].fd = fd; //object->events[cur].events = POLLRDHUP; 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; }
static int swReactorPoll_set(swReactor *reactor, int fd, int fdtype) { uint32_t i; swReactorPoll *object = reactor->object; for (i = 0; i < reactor->event_num; i++) { //found if (object->events[i].fd == fd) { object->fds[i].fdtype = swReactor_fdtype(fdtype); //object->events[i].events = POLLRDHUP; object->events[i].events = 0; if (swReactor_event_read(fdtype)) { object->events[i].events |= POLLIN; } if (swReactor_event_write(fdtype)) { object->events[i].events |= POLLOUT; } return SW_OK; } } return SW_ERR; }
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 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); 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) { swWarn("kevent fail. Error: %s[%d]", strerror(errno), errno); 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) { swWarn("kevent fail. Error: %s[%d]", strerror(errno), errno); return SW_ERR; } } memcpy(&e.udata, &fd_, sizeof(swFd)); swTrace("[THREAD #%ld]EP=%d|FD=%d\n", pthread_self(), this->epfd, fd); 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; }
SWINLINE static int swReactorEpoll_event_set(int fdtype) { uint32_t flag = 0; #ifdef SW_USE_EPOLLET flag = EPOLLET; #endif if (swReactor_event_read(fdtype)) { flag |= EPOLLIN; } if (swReactor_event_write(fdtype)) { flag |= EPOLLOUT; } if (swReactor_event_error(fdtype)) { flag |= (EPOLLRDHUP | EPOLLHUP | EPOLLERR); } return flag; }
static int swReactorKqueue_set(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); 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("kqueue->set(%d, SW_EVENT_READ) failed.", fd); return SW_ERR; } } else { EV_SET(&e, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); memcpy(&e.udata, &fd_, sizeof(swFd)); ret = kevent(this->epfd, &e, 1, NULL, 0, NULL); if (ret < 0) { swSysError("kqueue->del(%d, SW_EVENT_READ) failed.", 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("kqueue->set(%d, SW_EVENT_WRITE) failed.", fd); return SW_ERR; } } else { EV_SET(&e, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); memcpy(&e.udata, &fd_, sizeof(swFd)); ret = kevent(this->epfd, &e, 1, NULL, 0, NULL); if (ret < 0) { swSysError("kqueue->del(%d, SW_EVENT_WRITE) failed.", fd); return SW_ERR; } } swTrace("[THREAD #%d]EP=%d|FD=%d, events=%d", SwooleTG.id, this->epfd, fd, fdtype); //execute parent method swReactor_set(reactor, fd, fdtype); return SW_OK; }