handy源码阅读(六):tcp类
首先是tcpconn和tcpserver类:
struct TcpConn : public std::enable_shared_from_this<TcpConn>, private noncopyable {
enum State {
Invalid = 1,
Handshaking,
Connected,
Closed,
Failed,
};
TcpConn();
virtual ~TcpConn();
template <class C = TcpConn>
static TcpConnPtr createConnection(EventBase* base, const std::string& host, unsigned short port, int timeout = 0, const std::string& localip = "") {
TcpConnPtr con(new C);
con->connect(base, host, port, timeout, localip);
return con;
}
template <class C = TcpConn>
static TcpConnPtr createConnection(EventBase* base, int fd, Ip4Addr local, Ip4Addr peer) {
TcpConnPtr con(new C);
con->attach(base, fd, local, peer);
return con;
}
bool isClient() {
return destPort_ > 0;
}
template <class T>
T& context() {
return ctx_.context<T>();
}
EventBase* getBase() {
return base_;
}
State getState() {
return state_;
}
Buffer& getInput() {
return input_;
}
Buffer& getOutput() {
return output_;
}
Channel* getChannel() {
return channel_;
}
bool writable() {
return channel_ ? channel_->writeEnabled() : false;
}
void sendOutput() {
send(output_);
}
void send(Buffer& msg);
void send(const char* buf, size_t len);
void send(const std::string& s) {
send(s.data(), s.size());
}
void send(const char* s) {
send(s, strlen(s));
}
void onRead(const TcpCallBack& cb) {
assert(!readcb_);
readcb_ = cb;
}
};
struct TcpServer : private noncopyable {
TcpServer(EventBase* bases);
int bind(const std::string& host, unsigned short port, bool reusePort = false);
static TcpServerPtr startServer(EventBases* bases, const std::string& host, unsigned short port, bool reusePort = false);
~TcpServer() {
delete listen_channel_;
}
Ip4Addr getAddr() {
return addr_;
}
EventBase* getBase() {
return base_;
}
void onConnCreate(const std::function<TcpConnPtr()>& cb) {
createcb_ = cb;
}
void onConnRead(const TcpCallBack& cb) {
readcb_ = cb;
assert(!msgcb_);
}
private:
EventBase* base_;
EventBases* bases_;
Ip4Addr addr_;
Channel* listen_channel_;
TcpCallBack statecb_, readcb_;
MsgCallBack msgcb_;
std::function<TcpConnPtr()> createcb_;
std::unique_ptr<CodecBase> codec_;
void handleAccept();
};
tcp类和udp类实现很相似,除了在处理连接监听方式不同外,都是用epoll_wait来等待内核通知处理指定的文件描述符的事件。
handy源码阅读(六):tcp类的更多相关文章
- Spark常用函数(源码阅读六)
源码层面整理下我们常用的操作RDD数据处理与分析的函数,从而能更好的应用于工作中. 连接Hbase,读取hbase的过程,首先代码如下: def tableInitByTime(sc : SparkC ...
- handy源码阅读(六):udp类
分为UdpServer类和UdpConn类. struct UdpServer : public std::enable_shared_from_this<UdpServer>, priv ...
- handy源码阅读(四):Channel类
通道,封装了可以进行epoll的一个fd. struct Channel: private noncopyable { Channel(EventBase* base, int fd, int eve ...
- handy源码阅读(三):SafeQueue类
SafeQueue类继承与信号量mutex(用于加锁),nonocopyable 定义如下: template <typename T> struct SafeQueue : privat ...
- handy源码阅读(二):EventsImp类
EventsImp用于完成事件的处理. class EventsImp { EventBase* base_; PollerBase* poller_; std::atomic<bool> ...
- handy源码阅读(一):EventBase类
类EventBase继承于类EventBases,继承于noncopyable. 其中noncopyable是一个去除了拷贝构造和赋值构造的类. noncopyable: class noncopy ...
- handy源码阅读(五):PollerBase类
使用poll内核函数等待事件发生: struct PollerBase: private noncopyable { int64_t id_; int lastActive_; PollerBase( ...
- JDK源码阅读:Object类阅读笔记
Object 1. @HotSpotIntrinsicCandidate @HotSpotIntrinsicCandidate public final native Class<?> g ...
- go语言nsq源码解读六 tcp.go、tcp_server.go
本篇讲nsqlookupd中tcp.go.tcp_server.go tcp_server.go位于util目录下. 12345678910111213141516171819202122232425 ...
随机推荐
- 【奇技淫巧】使用 SSH 转发 Sock5 流量
标题:使用 SSH 转发 Sock5 流量 日期:2018-06-27 介绍:使用 ssh 来做个 sock5 的代理,穿透到内网中做后渗透 0x01. 基本信息 在 ubuntu(10.211.55 ...
- ARTS-2
ARTS的初衷 Algorithm:主要是为了编程训练和学习.每周至少做一个 leetcode 的算法题(先从Easy开始,然后再Medium,最后才Hard).进行编程训练,如果不训练你看再多的算法 ...
- 正则表达式分组(Grouping)
一 捕获型 (x) 匹配 x ,并且捕获匹配项 const regExp = /(\w+)\s+(\d+)/; const str = 'Android 8'; str.replace(regExp, ...
- docker安装tomcat&部署javaweb程序
一.docker定制简单的java-web应用镜像 网址: 1.jdk下载网址:https://www.oracle.com/technetwork/java/javase/downloads/jdk ...
- Web安全测试中常见逻辑漏洞解析(实战篇)
Web安全测试中常见逻辑漏洞解析(实战篇) 简要: 越权漏洞是比较常见的漏洞类型,越权漏洞可以理解为,一个正常的用户A通常只能够对自己的一些信息进行增删改查,但是由于程序员的一时疏忽,对信息进行增删改 ...
- 20191110 Spring Boot官方文档学习(4.2)
4.2.外部化配置 Spring Boot使您可以外部化配置,以便可以在不同环境中使用相同的应用程序代码.您可以使用Properties文件,YAML文件,环境变量和命令行参数来外部化配置.属性值可以 ...
- 手把手教你SOAP访问webservice并DOM解析返回的XML数据(转)
http://blog.csdn.net/u012534831/article/details/51357111 前言: 目前我们项目组还在采用webservice这种http方式,并且某些网站服务提 ...
- linux 通配符与正则表达式
linux通配符和三剑客(grep.awk.sed)正则表达式是不一样的 通配符一般用户命令行bash环境,而linux正则表达式用于awk.grep.sed
- python学习第五十二天logging模块的使用
很多程序都有记录日志的需求,并且日志包含的信息即有正常的程序访问日志,还可能有错误,警告等信息输出,python的 logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...
- SpringMVC_放行静态资源
静态资源到处都是坑!明白原理才能绕过这些坑! web.xml配置servlet中四种路径的区别 在web.xml文件的配置中,四种路径编写方式优先级如下图: 其中b和d都能接收所有请求,仅仅是在优先级 ...