通道,封装了可以进行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. maven 导出项目所依赖的jar包

    1.在 pom文件中 点击 Run As->Maven Build 2.在 Goals 中输入 dependency:copy-dependencies 3.之后会在 项目目录的 target/ ...

  2. 关于Polyaxon的使用

    1.首先upload代码 polyaxon upload 注意,upload只能在新建notebook前进行,且每次只能upload一次,所以在结束每次项目测试(开发)前,都应该在Polyaxon上修 ...

  3. [DS+Algo] 009 树的介绍

    目录 1. 树的概念 2. 树的术语 3. 树的种类 4. 常见应用场景 5. 二叉树 1. 树的概念 每个结点(节点)有 0 个或多个子结点 没有父结点的结点称为根结点 每一个非根结点有且只有一个父 ...

  4. [19/06/06-星期四] CSS基础_盒子模型

    一.盒子模型(框模型.盒模型) CSS处理网页时,它认为每个元素都在一个不可见的矩形盒子里. 为什么想象成盒子模型?因为把所有元素想象成盒子,那么我们对网页的布局就相当于摆放盒子.我们只需要把相应的盒 ...

  5. 使用批处理命令注册运行mysql数据库,无需注册mysql服务,可以在任意电脑登录使用

    使用批处理命令初始化和开启mysql服务,移植数据库之后可以直接运行访问,对于学习数据库的人来说特别的方便哦. 我们可以从mysql官网下载官方社区版本的mysql: 这里使用之前下载的8.0.15来 ...

  6. Java 14 可能带来什么新特性?

    JDK/Java 13 在一个月前已经发布,该版本带来了 5 大新特性,笔者观察到其中的 Text Blocks(文本块)特性似乎被讨论最多. 文本块特性与常见的 Python "" ...

  7. Android怎么改图标都不生效&&Android studio 如何修改APP图标和名字

    去这里(我自己写的),解决方法包你满意: https://blog.csdn.net/qq_43141611/article/details/101875545

  8. java基础笔记(4)

    二进制运算: &的应用:清零.得到指定位的数: |的应用:将指定位置取1: ^的应用:取反.保留原值:交换两个bian变量:A= A^B,B =A ^ B,A = A^B;(原理就是本身异或本 ...

  9. OI那些事——AFO

    \(OI\)那些事--\(AFO\) 世界上从此少了一个\(Oier\)也不会有人知道,也许只有某个人在某年某月某日翻到了Eternal 风度的博客才会发现:哇,这哥们怎么\(Noip\)就退役了 两 ...

  10. 搜索专题: HDU1312Red and Black

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...