muduo库 Poller

本文介绍了Poller类在事件驱动编程中的作用,它是IO复用抽象接口的实现,负责管理Channel对象。Poller具备添加、删除和检查Channel的功能,并在指定超时时间内轮询IO事件,返回活跃的Channel列表。同时,文章展示了Poller的构造和析构,以及如何判断Channel是否存在于Poller中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#pragma once

#include"noncopyable.h"
#include"Timestamp.h"

#include<vector>
#include<unordered_map>

class Channel;
class EventLoop;

class Poller:noncopyable
{
public:
    using ChannelList=std::vector<Channel*>;

    Poller(EventLoop *loop);
    virtual ~Poller()=default;

    //给所有的IO复用保留统一的接口
    virtual Timestamp poll(int timeoutMs,ChannelList *activeChannels) = 0;
    virtual void updateChannel(Channel*channel) = 0;
    virtual void removeChannel(Channel*channel) = 0;

    //判断参数channel是否在当前Poller中
    bool hasChannel(Channel*channel)const;

    //EventLoop可以通过该接口获取默认的IO复用的具体实现
    static Poller* newDefaultPoller(EventLoop*loop);

protected:
    // map的key:sockfd  value:sockfd所属的channel通道类型
    using ChannelMap=std::unordered_map<int,Channel*>;
    ChannelMap channels_;
private:
    EventLoop *ownerLoop_;//定义Poller所属的事件循环EventLoop
};
#include"Poller.h"
#include"Channel.h"

Poller::Poller(EventLoop*loop)
    :ownerLoop_(loop)
    {     
    }

bool Poller::hasChannel(Channel*channel) const
{
    auto it=channels_.find(channel->fd());
    return it!=channels_.end()&&it->second==channel;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值