• muduo网络库中重要的函数


    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_)));
    }
    

      

  • 相关阅读:
    [原创]ASP.NET MVC调用美图秀秀开放平台拼图实现
    使用Lucene检索文档中的关键字
    Unitils+hibernate+Spring+PostgreSql做dao层测试遇到的错误
    初探IronJS
    IntelliJ IDEA 12 创建Web项目 教程 超详细版
    百度面试题:求绝对值最小的数
    jquery+css实现简单的评分功能
    Knockot JS 数字输入插件
    Diagnostic Policy Service 服务处于起不来
    WCF学习笔记(一) 之 开门见山
  • 原文地址:https://www.cnblogs.com/zyj23/p/14701429.html
Copyright © 2020-2023  润新知