1、EventLoop类 EventLoop::loop() { poller_->poll(); { epoll_create1() EPollPoller::poll() { epoll_wait fillActiveChannels(); } } currentActiveChannel_->handleEvent(pollReturnTime_); } 2、EpollPoller类 EPollPoller::updateChannel() { } 3、Channel类 Channel::enableReading() { Channel::update() { EventLoop::updateChannel() { Poller::updateChannel() { EPollPoller::update { //监听的时候需要、连接成功的时候需要 epoll_ctl(); } } } } } Channel::handleEvent() { Channel::handleEventWithGuard() { closeCallback_(); writeCallback_(); readCallback_(); } } Channel::setReadCallback(){readCallback_ } Channel::setWriteCallback() Channel::setCloseCallback() Channel::setErrorCallback() 4、TcpConnection类 便会将对应的读写回调处理函数注册进Channel //构造 TcpConnection::TcpConnection { Channel::setReadCallback(std::bind(&TcpConnection::handleRead, this, _1)); { TcpConnection::handleRead(Timestamp receiveTime) { inputBuffer_ messageCallback_(shared_from_this(), &inputBuffer_, receiveTime); } } Channel::setWriteCallback(std::bind(&TcpConnection::handleWrite, this)); { TcpConnection::handleWrite { outputBuffer_ writeCompleteCallback_(); } } Channel::setCloseCallback(std::bind(&TcpConnection::handleClose, this)); { TcpConnection::handleClose { connectionCallback_(); closeCallback_(); } } Channel::setErrorCallback(std::bind(&TcpConnection::handleError, this)); { TcpConnection::handleError { sockets::getSocketError(); } } } 5、Acceptor类 EventLoop Socket Channel //主要负责的就是对lfd的读事件的处理 Acceptor::Acceptor { Channel::setReadCallback(std::bind(&Acceptor::handleRead, this)); { Acceptor::handleRead { accept() newConnectionCallback_(); } } } Acceptor::setNewConnectionCallback{newConnectionCallback_} Acceptor::listen() { listen() Channel::enableReading(); } 6、TcpServer类 TcpServer::TcpServer { Acceptor::setNewConnectionCallback(std::bind(&TcpServer::newConnection, this, _1, _2)); { TcpServer::newConnection { 1)、EventLoop* ioLoop = threadPool_->getNextLoop(); //从线程池取一个loop 2)、new TcpConnection //这些都是暴露给用户的接口 conn->setConnectionCallback(connectionCallback_); conn->setMessageCallback(messageCallback_); conn->setWriteCompleteCallback(writeCompleteCallback_); conn->setCloseCallback( std::bind(&TcpServer::removeConnection, this, _1)); ioLoop->runInLoop(std::bind(&TcpConnection::connectEstablished, conn)); { TcpConnection::connectEstablished() { Channel::enableReading(); connectionCallback_ } } } } } TcpServer::start() { loop_->runInLoop(std::bind(&Acceptor::listen, get_pointer(acceptor_))); }
原文:https://www.cnblogs.com/zyj23/p/14701429.html