muduomuduoCallbackEchoServer
EchoServer(EventLoop *loop,
const InetAddress &addr,
const std::string &name)
: server_(loop, addr, name)
, loop_(loop)
{
// 注册回调函数
server_.setConnectionCallback(
std::bind(&EchoServer::onConnection, this, std::placeholders::_1)
);
server_.setMessageCallback(
std::bind(&EchoServer::onMessage, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)
);
// 设置适合的loop线程数量 loopthread
server_.setThreadNum(3);
}
setConnectionCallbacksetMessageCallbackTcpServersetConnectionCallback
void setConnectionCallback(const ConnectionCallback &cb) { connectionCallback_ = cb; }
EchoServer::onConnectionTcpServerTcpServerAcceptorTcpServerconnfdpeerAddrTcpConnectionTcpServerTcpConnectionTcpServer
// 依据连贯胜利的sockfd,创立TcpConnection
TcpConnectionPtr conn(new TcpConnection(
ioLoop,
connName,
sockfd, // Socket Channel
localAddr,
peerAddr));
connections_[connName] = conn;
// 上面的回调时用户设置给TcpServer,TcpServer又设置给TcpConnection,TcpConnetion又设置给Channel,Channel又设置给Poller,Poller告诉channel调用这个回调
conn->setConnectionCallback(connectionCallback_);
conn->setMessageCallback(messageCallback_);
conn->setWriteCompleteCallback(writeCompleteCallback_);
conn->setMessageCallback(messageCallback_);connTcpConnectionsetMessageCallback
void setConnectionCallback(const ConnectionCallback& cb)
{ connectionCallback_ = cb; }
onMessageBuffer
本人的网址:www.shicoder.top
欢送加群聊天 452380935
本文由博客一文多发平台 OpenWrite 公布!