加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TcpConnection.cpp 1.79 KB
一键复制 编辑 原始数据 按行查看 历史
leo_1 提交于 2023-11-01 03:44 . tcpconnection部分实现
/***
* @Description:
* @Author: LeoTao
* @Date: 2023-10-21 21:53:53
* @LastEditTime: 2023-11-01 03:44:17
*/
#include "TcpConnection.h"
#include "Socket.h"
#include "Channel.h"
#include "logger.h"
static EventLoop* CheckLoopNotNull(EventLoop *loop){
if(loop == nullptr){
LOG_FATAL("%s:%s:%d mianloop is nullptr", __FILE__, __FUNCTION__, __LINE__);
}
return loop;
}
TcpConnection::TcpConnection(EventLoop* loop,
const std::string& name,
int sockfd,
const InetAddress& localAddr,
const InetAddress& peerAddr)
: loop_(CheckLoopNotNull(loop)),
name_(name),
state_(kConnecting),
reading_(true),
socket_(new Socket(sockfd)),
channel_(new Channel(loop, sockfd)),
localAddr_(localAddr),
peerAddr_(peerAddr),
highWaterMark_(64*1024*1024)//64M
{
//下面给channel设置相应得回调函数,poller给channel通知感兴趣得事件发生了,channel会回调相应得操作函数
channel_->setReadCallback(std::bind(&TcpConnection::handleRead, this, std::placeholders::_1));
channel_->setWriteCallback(std::bind(&TcpConnection::handleWrite, this));
channel_->setCloseCallback(std::bind(&TcpConnection::handleClose,this));
channel_->setErrorCallback(std::bind(&TcpConnection::handleError,this));
LOG_INFO("TcpConnection::ctor[%s] at fd = %d \n", name_.c_str(), sockfd);
socket_->setKeepAlive(true);
}
TcpConnection::~TcpConnection(){
LOG_INFO("TcpConnection::dtor[%s] at fd = %d\n", name_.c_str(), channel_->fd());
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化