コード例 #1
0
ファイル: ReactorKqueue.c プロジェクト: leoozhao/swoole-src
static int swReactorKqueue_del(swReactor *reactor, int fd)
{
    swReactorKqueue *this = reactor->object;
    struct kevent e;
    int ret;

    swConnection *socket = swReactor_get(reactor, fd);

    if (socket->events & SW_EVENT_READ)
    {
        EV_SET(&e, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
        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 (socket->events & SW_EVENT_WRITE)
    {
        EV_SET(&e, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
        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", SwooleTG.id, this->epfd, fd);
    reactor->event_num = reactor->event_num <= 0 ? 0 : reactor->event_num - 1;
    swReactor_del(reactor, fd);
    return SW_OK;
}
コード例 #2
0
ファイル: ReactorEpoll.c プロジェクト: JayenLee/swoole-src
int swReactorEpoll_del(swReactor *reactor, int fd)
{
    swReactorEpoll *object = reactor->object;
    int ret;

    if (fd <= 0)
    {
        return SW_ERR;
    }
    ret = epoll_ctl(object->epfd, EPOLL_CTL_DEL, fd, NULL);
    if (ret < 0)
    {
        swWarn("epoll remove fd[=%d] failed. Error: %s[%d]", fd, strerror(errno), errno);
        return SW_ERR;
    }

    if (swReactor_del(reactor, fd) < 0)
    {
        return SW_ERR;
    }

    reactor->event_num = reactor->event_num <= 0 ? 0 : reactor->event_num - 1;
    swTraceLog(SW_TRACE_EVENT, "remove event[reactor_id=%d|fd=%d]", reactor->id, fd);
    return SW_OK;
}
コード例 #3
0
ファイル: ReactorKqueue.c プロジェクト: leoozhao/swoole-src
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;
}
コード例 #4
0
ファイル: ReactorPoll.c プロジェクト: HunterChen/swoole-src
static int swReactorPoll_del(swReactor *reactor, int fd)
{
    uint32_t i;
    swReactorPoll *object = reactor->object;

    swTrace("fd=%d", fd);

    if (swReactor_del(reactor, fd) < 0)
    {
        return SW_ERR;
    }

    for (i = 0; i < reactor->event_num; i++)
    {
        //找到了
        if (object->events[i].fd == fd)
        {
            uint32_t old_num = reactor->event_num;
            reactor->event_num = reactor->event_num <= 0 ? 0 : reactor->event_num - 1;
            for (; i < old_num; i++)
            {
                if (i == old_num)
                {
                    object->fds[i].fdtype = 0;
                    object->events[i].fd = 0;
                    object->events[i].events = 0;
                }
                else
                {
                    object->fds[i] = object->fds[i + 1];
                    object->events[i] = object->events[i + 1];
                }
            }
            return SW_OK;
        }
    }
    return SW_ERR;
}