加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
TcpServer.cc 3.71 KB
一键复制 编辑 原始数据 按行查看 历史
#include "TcpServer.h"
#include "Logger.h"
#include "TcpConnection.h"
#include <strings.h>
static EventLoop* CheckLoopNotNull(EventLoop* loop){
if(loop == nullptr){
LOG_FATAL("%s:%s:%d mainLoop is null! \n",__FILE__,__FUNCTION__,__LINE__);
}
return loop;
}
TcpServer::TcpServer(EventLoop* loop, const InetAddress& listenAddr, const std::string& nameArg, Option option)
:loop_(CheckLoopNotNull(loop))
,ipPort_(listenAddr.toIpPort())
,name_(nameArg)
,acceptor_(new Acceptor(loop, listenAddr, option == kReusePort))
,threadPool_(new EventLoopThreadPool(loop, name_))
,connectionCallback_()
,messageCallback_()
,nextConnId_(1)
,started_(0)
{
//当有新用户连接时会执行TcpServer::newConneciton回调
acceptor_->setNewConnectionCallback(std::bind(&TcpServer::newConneciton,this
,std::placeholders::_1,std::placeholders::_2));
}
TcpServer::~TcpServer(){
for(auto& item : connections_){
TcpConnectionPtr conn(item.second);//这个局部的智能指针对象出了右括号后会自动释放
item.second.reset();
//销毁连接
conn->getLoop()->runInLoop(std::bind(&TcpConnection::connectDestroyed, conn));
}
}
//设置底层subLoop的个数
void TcpServer::setThreadNum(int numThreads){
threadPool_->setThreadNum(numThreads);
}
//开启服务器监听 loop.loop()
void TcpServer::start(){
if(started_++ == 0){ //防止一个TcpServer对象被start多次
threadPool_->start(threadInitCallback_); //启动底层的loop线程池
loop_->runInLoop(std::bind(&Acceptor::listen, acceptor_.get()));
}
}
//有一个新的客户端的连接,acceptor会执行这个回调操作
void TcpServer::newConneciton(int sockfd, const InetAddress& peerAddr){
//轮询选择一个subLoop来管理channel
EventLoop* ioLoop = threadPool_->getNextLoop();
char buf[64] = {0};
snprintf(buf, sizeof buf, "-%s#%d",ipPort_.c_str(), nextConnId_);
++nextConnId_;
std::string connName = name_ + buf;
LOG_INFO("TcpServer::newConneciton [%s] - new connection [%s] from %s \n"
, name_.c_str(), connName.c_str(), peerAddr.toIpPort().c_str());
//通过sockfd获取其绑定的本机的ip地址和端口信息
sockaddr_in local;
::bzero(&local, sizeof local);
socklen_t addrlen = sizeof local;
if(::getsockname(sockfd, (sockaddr*)&local, &addrlen) < 0){
LOG_ERROR("sockets::getLocalAddr");
}
InetAddress localAddr(local);
//根据连接成功的 sockfd,创建 TcpConnection 连接对象
TcpConnectionPtr conn(new TcpConnection(ioLoop, connName, sockfd, localAddr, peerAddr));
connections_[connName] = conn;
//下面的回调都是用户设置给TcpServer的,然后TCPServer又设置给TcpConnection,然后TcpConnection又设置给Channel
//最后Channel又设置给了Poller,Poller最终又通知Channel调用回调
conn->setConnectionCallback(connectionCallback_);
conn->setMessageCallback(messageCallback_);
conn->setWriteCompleteCallback(writeCompleteCallback_);
//设置了如何关闭连接的回调
conn->setCloseCallback(std::bind(&TcpServer::removeConnection, this, std::placeholders::_1));
//直接调用TcpConnection::connectEstablished()
ioLoop->runInLoop(std::bind(&TcpConnection::connectEstablished, conn));
}
void TcpServer::removeConnection(const TcpConnectionPtr& conn){
loop_->runInLoop(std::bind(&TcpServer::removeConnectionInLoop, this, conn));
}
void TcpServer::removeConnectionInLoop(const TcpConnectionPtr& conn){
LOG_INFO("TcpServer::removeConnectionInLoop [%s] - connection %s \n"
,name_.c_str(), conn->name().c_str());
connections_.erase(conn->name());
EventLoop* ioLoop = conn->getLoop();
ioLoop->queueInLoop(std::bind(&TcpConnection::connectDestroyed, conn));
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化