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 ...
随机推荐
- Mybatis使用时 resultMap与resultType、parameterMap与 parameterType的区别
Map:映射:Type:Java类型 resultMap 与 resultType.parameterMap 与 parameterType的区别在面试的时候被问到的几率非常高,出现的次数到了令人 ...
- session 、cookie、token的区别(转)
session session的中文翻译是“会话”,当用户打开某个web应用时,便与web服务器产生一次session.服务器使用session把用户的信息临时保存在了服务器上,用户离开网站后ses ...
- ugui点击穿透判断
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Eve ...
- LeetCode.872-叶子值相等的树(Leaf-Similar Trees)
这是悦乐书的第334次更新,第358篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第204题(顺位题号是872).考虑二叉树的所有叶子,从左到右的顺序,这些叶子的值形成叶 ...
- [Git] 018 冲突在所难免,需要巧妙化解
0. 回顾 [Git] 005 初识 Git 与 GitHub 之分支 中"4.2 情形二"的 9 提及了"解决冲突" 当时没有展开,这回详谈 我这回反其道而行 ...
- 【嵌入式linux】用户登录密码验证配置
在配置BusyBox中配置登录密码: 配置位置BusyBox Setting --> General Configuration --> Suppo ...
- 全自动网络安装centos(一)安装前准备工作
centos系统启动文件详解: 注:在centos6里需要给NetworkManager服务关闭并且禁止开机启动,6和7里都需要将selinux关闭,否则会出现网络配置异常情况,并且要将防火墙关闭. ...
- Sql server 执行计划详解
序言 本篇主要目的有二: 1.看懂t-sql的执行计划,明白执行计划中的一些常识. 2.能够分析执行计划,找到优化sql性能的思路或方案. 如果你对sql查询优化的理解或常识不是很深入,那么推荐几骗博 ...
- 遍历文件夹下的csv,把数据读进一张表
import pandas as pd import os if __name__ == '__main__': Path = 'c:\checklog' dfs = [] for dir_path, ...
- POJ 3743 LL’s cake(圆+PSLG)
题意是给你一块在原点半径为10的圆,然后告诉你一条直线在圆弧上的极角,相当于用这条直线把这个圆分成两半,然后一共是n条直线切圆,就好比切蛋糕,问你其中最大一块的面积是多少. 如果我们将圆弧转化成直线边 ...