tars源码 tc_epoll

tars/tc_epoll.h

tc_epoller要来进行封装操作,且封装成默认ET模式, 如构造函数:

TC_Epoller(bool bEt = true); 

随后, 每一次ctl, 都判断了是ET还是LT:

void TC_Epoller::ctrl(int fd, long long data, __uint32_t events, int op)
{
    struct epoll_event ev;
    ev.data.u64 = data;
    if(_et)
    {
        ev.events   = events | EPOLLET;
    }
    else
    {
        ev.events   = events;
    }

    epoll_ctl(_iEpollfd, op, fd, &ev);
}

创建epoll管理句柄, 也是我们熟悉的方式:

void TC_Epoller::create(int max_connections)
{
    _max_connections = max_connections;

    _iEpollfd = epoll_create(_max_connections + 1);

    if(_pevs != NULL)
    {
        delete[] _pevs;
    }

    _pevs = new epoll_event[_max_connections + 1];
}

如下也是封装,方便操作:

void TC_Epoller::add(int fd, long long data, __uint32_t event)
{
    ctrl(fd, data, event, EPOLL_CTL_ADD);
}

void TC_Epoller::mod(int fd, long long data, __uint32_t event)
{
    ctrl(fd, data, event, EPOLL_CTL_MOD);
}

void TC_Epoller::del(int fd, long long data, __uint32_t event)
{
    ctrl(fd, data, event, EPOLL_CTL_DEL);
}

int TC_Epoller::wait(int millsecond)
{
    return epoll_wait(_iEpollfd, _pevs, _max_connections + 1, millsecond);
}

可以看到, tc_epoller就是对原生epoll的封装

猜你喜欢

转载自blog.csdn.net/grace_fang/article/details/80644430