Acceptor类解析——muduo源码分析

Acceptor仅用于接受来自客户端的连接,在创建连接之后将其放入一个Eventloop中。选择Eventloop的方式是round robin。

Acceptor.h


#ifndef MUDUO_NET_ACCEPTOR_H
#define MUDUO_NET_ACCEPTOR_H

#include <functional>

#include "muduo/net/Channel.h"
#include "muduo/net/Socket.h"

namespace muduo
{
namespace net
{

class EventLoop; //前向声明,头文件不需要重复包含
class InetAddress; //前向声明

///
/// Acceptor of incoming TCP connections.
///
class Acceptor : noncopyable
{
 public:
    //注册一个NewConnectionCallback回调,参数为文件描述符sockfd和muduo封装的地址类InetAddress
  typedef std::function<void (int sockfd, const InetAddress&)> NewConnectionCallback;
    //构造函数中的loop为Acceptor所在的EventLoop,保证Acceptor在同一个thread中运行。
    //reuseport表示是否复用端口号。是一种套接字复用机制,它允许多个套接字bind在同一个IP地址/端口对上,
    //这样一来,就可以建立多个服务来接受到同一个端口的连接。
  Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport);
  ~Acceptor();
    //把NewConnectionCallback注册进去,如果有事件发生,就调用->NewConnectionCallback。
  void setNewConnectionCallback(const NewConnectionCallback& cb)
  { newConnectionCallback_ = cb; }

  void listen();

  bool listening() const { return listening_; }

  // Deprecated, use the correct spelling one above.
  // Leave the wrong spelling here in case one needs to grep it for error messages.
  // bool listenning() const { return listening(); }

 private:
  void handleRead();

  EventLoop* loop_; //当前线程所属的Eventloop
  Socket acceptSocket_; //监听的socket
  Channel acceptChannel_; //监听socket所属的channel
  NewConnectionCallback newConnectionCallback_; //回调函数
  bool listening_; //是否处于监听状态
    //一个空闲的文件描述符,在运行Acceptor时创建,用于“优雅关闭连接”。
    //muduo的设计考虑到文件描述符满的情况:此时无法处理新的连接,应当如何处理呢?
    //Acceptor会关闭idleFd,然后接受新的连接,再断开新的连接,重新启用idleFd。保证了所有连接都能够被处理。
  int idleFd_;
};

}  // namespace net
}  // namespace muduo

#endif  // MUDUO_NET_ACCEPTOR_H

Acceptor.cc

#include "muduo/net/Acceptor.h"

#include "muduo/base/Logging.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/InetAddress.h"
#include "muduo/net/SocketsOps.h"

#include <errno.h>
#include <fcntl.h>
//#include <sys/types.h>
//#include <sys/stat.h>
#include <unistd.h>

using namespace muduo;
using namespace muduo::net;
//在构造函数中创建一个acceptChannel,其初始化列表所在的位置应晚于loop,acceptsocket这两个形参。

Acceptor::Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport)
  : loop_(loop),
    acceptSocket_(sockets::createNonblockingOrDie(listenAddr.family())),
    acceptChannel_(loop, acceptSocket_.fd()),
    listening_(false),
    idleFd_(::open("/dev/null", O_RDONLY | O_CLOEXEC))
{
  assert(idleFd_ >= 0);
  acceptSocket_.setReuseAddr(true);
  acceptSocket_.setReusePort(reuseport);
  acceptSocket_.bindAddress(listenAddr);
  acceptChannel_.setReadCallback(
      std::bind(&Acceptor::handleRead, this));
}
//仅idleFd的生命周期由Acceptor控制
Acceptor::~Acceptor()
{
  acceptChannel_.disableAll();
  acceptChannel_.remove();
  ::close(idleFd_);
}
//启用监听
void Acceptor::listen()
{
  loop_->assertInLoopThread();
  listening_ = true;
  acceptSocket_.listen();
  acceptChannel_.enableReading();
}
//接受连接,并调用newConnectionCallback_
void Acceptor::handleRead()
{
  loop_->assertInLoopThread();
  InetAddress peerAddr;
  //FIXME loop until no more
  int connfd = acceptSocket_.accept(&peerAddr);
  if (connfd >= 0)
  {
    // string hostport = peerAddr.toIpPort();
    // LOG_TRACE << "Accepts of " << hostport;
      //调用newConnectionCallback,转而调用
    if (newConnectionCallback_)
    {
      newConnectionCallback_(connfd, peerAddr);
    }
    else
    {
      sockets::close(connfd);
    }
  }
  else
  {
    LOG_SYSERR << "in Acceptor::handleRead";
    // Read the section named "The special problem of
    // accept()ing when you can't" in libev's doc.
    // By Marc Lehmann, author of libev.
      //处理文件描述符满的情况
    if (errno == EMFILE)
    {
      ::close(idleFd_);
      idleFd_ = ::accept(acceptSocket_.fd(), NULL, NULL);
      ::close(idleFd_);
      idleFd_ = ::open("/dev/null", O_RDONLY | O_CLOEXEC);
    }
  }
}


思考:
为什么是重定向到/dev/null,而不是关闭文件描述符可读可写?

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值