本文链接:https://blog.youkuaiyun.com/Mr_scott_j/article/details/111059068
TcpConnection是muduo中最为复杂的类,其实就是对已连接套接字的一个抽象。
TcpConnection使用Channel来获得socket上的IO事件,它可以自己处理writable事件,而把readable事件通过MessageCallback传达给客户。在TcpConnection析构时候会close(fd)(在Socket析构函数中发生)。
TcpConnection在构造函数中将handleRead注册到Channel::ReadCallback上,并在其中调用messageCallback_;而handleWrite由自己处理。
TcpConnection::TcpConnection(EventLoop* loop,
const string& nameArg,
int sockfd,
const InetAddress& localAddr,
const InetAddress& peerAddr)
: loop_(CHECK_NOTNULL(loop)),//不能为空,否则触发FATAL
name_(nameArg),
state_(kConnecting),
reading_(true),
socket_(new Socket(sockfd)),
channel_(new Channel(loop, sockfd)),
localAddr_(localAddr),//本地地址
peerAddr_(peerAddr),
highWaterMark_(64*1024*1024)
{
//handleRead中会调用messageCallback_
channel_->setReadCallback(
std::bind(&TcpConnection::handleRead, this, _1));
channel_->setWriteCallback(
std::bind