通道,封装了可以进行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. 【Spring】的【bean】管理(XML配置文件)【Bean实例化的三种方式】

    Bean实例化的三种方式 说明:通过配置文件创建对象就称为Bean实例化. 第一种:使用类的无参构造创建(重点) 实体类 package com.tyzr.ioc; public class User ...

  2. DOM4J解析文件

    转发一篇好文 DOM4J解析文件 Dom4j和Xpath

  3. Ubuntu新建用户以及安装pytorch

    环境:Ubuntu18,Python3.6 首先登录服务器 ssh username@xx.xx.xx.xxx #登录一个已有的username 新建用户 sudo adduser username ...

  4. 应用安全 - 无文件式攻击 - 潜伏型攻击 - MBR - 汇总 (2019-11-29 15:57)

    Petya勒索病毒 Date

  5. vue中的$EventBus.$emit、$on的应用

    今天在项目中遇到的一个需求: 在一个选项卡功能的页面,出现的问题是,当点击选项卡的某个选项时,会同时加载整个选项卡的数据,本身产品就很大,数据很多,所以这个问题无法忽略: 仔细研究下发现,当刚进入页面 ...

  6. Go语言入门篇-环境准备

    一.GO语言特点 静态类型:首先要明确变量类型,如上所示. 编译型:指GO语言要被编译成机器能识别机器代码. GO语言开源. 编程范式:支持“函数式”和“面向对象” GO语言原生的支持并发编程:即GO ...

  7. Dos - 学习总结(1)

    1.控制台复制 1>鼠标右键,标记. 2>选定复制内容后,鼠标右键,完成复制. 2.

  8. select count(*) 底层到底干了啥?

    作者:贾春生,http://dwz.win/myg SELECT COUNT( * ) FROM TABLE 是个再常见不过的 SQL 需求了. 在 MySQL 的使用规范中,我们一般使用事务引擎 I ...

  9. Sequential game

    Sequential game Problem Description Sequential detector is a very important device in Hardware exper ...

  10. 一个简单的Vue.js组件开发示例

    //创建属于自己的vue组件库 (function(Vue, undefined) { Vue.component("my-component", { template: '< ...