通道,封装了可以进行epoll的一个fd。

struct Channel: private noncopyable {
Channel(EventBase* base, int fd, int events);
~Channel();
EventBase* getBase() { return base_; }
int fd() { return fd_; } //通道id
int64_d id() { return id_; }
short events() { return events_; }
//关闭通道
void close(); void onRead(const Task& readcb) { readcb_ = readcb; }
void onWrite(const Task& writecb) { writecb_ = writecb; }
void onRead(Task&& readcb) { readcb_ = std::move(readcb); }
void onWrite(Task&& writecb) { writecb_ = std::move(writecb); } //启动读写监听
void enableRead(bool enable);
void enableWrite(bool enable);
void enableReadWrite(bool readable, bool writeable);
bool readEnabled();
bool writeEnabled(); //处理读写事件
void handleRead() { readcb_(); }
void handleWrite() { writecb_(); } protected:
EventBase* base_;
PollerBase* poller_;
int fd_;
short events_;
int64_t id_;
std::function<void()> readcb_, writecb_, errorcb_;
};

其实现为:

Channel::Channel(EventBase* base, int fd, int events): base_(base), fd_(fd), events_(events) {
fatalif(net::setNonBlock(fd_) < , "channel set non block failed");
static atomic<int64_t> id();
id_ = ++id;
poller_ = base->imp_->poller_;
poller_->addChannel(this);
} Channel::~Channel() {
close();
} void Channle::enableRead(bool enable) {
if (enable) {
events_ |= kReadEvent;
} else {
events_ &= ~KReadEvent;
}
poller_->updateChannel(this);
} void Channel::enableWrite(bool enable) {
if (enable) {
events_ |= kWriteEvent;
} else {
events_ &= ~kWriteEvent;
}
poller_->updateChannel(this);
} void Channel::enableReadWrite(bool readable, bool writable) {
if (readable) {
events_ |= kReadEvent;
} else {
events_ &= ~kReadEvent;
}
if (writable) {
events_ |= kWriteEvent;
} else {
events_ &= ~kWriteEvent;
}
poller_->updateChannel(this);
} void Channel::close() {
if (fd_ >= ) {
trace("close channel %ld fd %d", (long)id_, fd_);
poller_->removeChannel(this);
::close(fd_);
fd_ = -;
handleRead();
}
} bool Channel::readEnabled() {
return events_ & kReadEvent;
} bool Channel::writeEnabled() {
return events_ & kWriteEvent;
}

本类的核心在于使用poller类进行添加删除更新channel状态,或是直接调用对应的函数对象。

handy源码阅读(四):Channel类的更多相关文章

  1. 35 网络相关函数(三)——live555源码阅读(四)网络

    35 网络相关函数(三)——live555源码阅读(四)网络 35 网络相关函数(三)——live555源码阅读(四)网络 简介 5)NoReuse不重用地址类 6)initializeWinsock ...

  2. 33 网络相关函数(一)——live555源码阅读(四)网络

    33 网络相关函数(一)——live555源码阅读(四)网络 33 网络相关函数(一)——live555源码阅读(四)网络 简介 1)IsMulticastAddress多播(组播)地址判断函数 多播 ...

  3. 32 GroupSock(AddressPortLookupTable)——live555源码阅读(四)网络

    32 GroupSock(AddressPortLookupTable)——live555源码阅读(四)网络 32 GroupSock(AddressPortLookupTable)——live555 ...

  4. 31 GroupSock(AddressString)——live555源码阅读(四)网络

    31 GroupSock(AddressString)——live555源码阅读(四)网络 31 GroupSock(AddressString)——live555源码阅读(四)网络 简介 Addre ...

  5. 30 GroupSock(Port)——live555源码阅读(四)网络

    30 GroupSock(Port)——live555源码阅读(四)网络 30 GroupSock(Port)——live555源码阅读(四)网络 简介 Port类的定义 Port的构造与全局的 &l ...

  6. 29 GroupSock(NetAddressList)——live555源码阅读(四)网络

    29 GroupSock(NetAddressList)——live555源码阅读(四)网络 29 GroupSock(NetAddressList)——live555源码阅读(四)网络 简介 Net ...

  7. 28 GroupSock(NetAddress)——live555源码阅读(四)网络

    28 GroupSock(NetAddress)——live555源码阅读(四)网络 28 GroupSock(NetAddress)——live555源码阅读(四)网络 简介 1) NetAddre ...

  8. 27 GroupSock概述(一)——live555源码阅读(四)网络

    27 GroupSock概述(一)——live555源码阅读(四)网络 27 GroupSock概述(一)——live555源码阅读(四)网络 简介 1.网络通用数据类型定义 2.Tunnel隧道封装 ...

  9. 40 网络相关函数(八)——live555源码阅读(四)网络

    40 网络相关函数(八)——live555源码阅读(四)网络 40 网络相关函数(八)——live555源码阅读(四)网络 简介 15)writeSocket向套接口写数据 TTL的概念 函数send ...

  10. 39 网络相关函数(七)——live555源码阅读(四)网络

    39 网络相关函数(七)——live555源码阅读(四)网络 39 网络相关函数(七)——live555源码阅读(四)网络 简介 14)readSocket从套接口读取数据 recv/recvfrom ...

随机推荐

  1. windows 使用Docker Desktop 使用国内镜像

    ===新增一些比较给力的镜像=== 1.中科大镜像加速地址 https://docker.mirrors.ustc.edu.cn 2.阿里云镜像服务 ========= 原本在配置项中添加了:国内镜像 ...

  2. 那些堪称神器的 Chrome 插件

    Chrome 的简洁快速以及丰富的插件种类使得它在国内日益盛行,帮助了我们很多 Chrome 用户提升了工作效率,而今天要给大家推荐8款实用甚至堪称神器的 Chrome 插件,希望对提升大家的工作效率 ...

  3. eclipse code recommenders cannot download its model repository index

    Cent OS 7 运行 eclipse oxygen 代码提示出现标题所示的错误,解决办法,将网络提供程序设置为手动即可解决. Window->Preference->General-& ...

  4. MySQL学习-入门语句以及增删查改

    1. SQL入门语句 SQL,指结构化查询语言,全称是 Structured Query Language,是一种 ANSI(American National Standards Institute ...

  5. Chapter02 第四节 函数

    2.4 函数 2.4.1 有返回值的函数 函数定义.函数原型.函数调用 函数定义即定义一个函数:形如 :double sqrt(double x){····} 函数调用即调用这个函数,形如 :doub ...

  6. .net core 学习小结之 PostMan报415

    首先415的官方解释是:对于当前请求的方法和所请求的资源,请求中提交的实体并不是服务器中所支持的格式,因此请求被拒绝. 也就是说我所准备的数据格式并不是后台代码使用的数据格式 后台代码如下 using ...

  7. for循环、列表的切片、元组

    一.遍历整个列表 使用for语句循环将列表每取出一个变量,然后存储在中间变量中,打印中间变量:循环取出: 1.简单for循环 示例: carssa = ['richan','fengtian','be ...

  8. Mysql-问题解决记录

    1.查看当前默认的配置文件位置 # mysqld --verbose --help | 'Default options' Default options are read from the foll ...

  9. [转帖]mysql数据库主从配置

    mysql数据库主从配置 https://www.toutiao.com/i6680489302947791371/ 多做实验 其实挺简单的 很多东西 要提高自信 去折腾. 架构与我 2019-04- ...

  10. Vue Element使用第三库icon图标

    一:引入单设图标 1.打开 阿里icon,注册 >登录>图标管理>我的项目 2.新建项目 返回阿里icon首页,点进去你想要的icon库,因为没有批量导入购物车,所以一般情况下需要一 ...