木铎源码剖析—TcpServer

TcpServer

此类是net设计中的最重要的一个类,他给出了底层服务器连接的接口,TODO:需要后续补充

成员变量

EventLoop* loop_;  // the acceptor loop
const string ipPort_; //服务器IP和Port的字符串表示
const string name_;//服务器的名称
std::unique_ptr<Acceptor> acceptor_; // avoid revealing Acceptor 用于处理新连接
std::shared_ptr<EventLoopThreadPool> threadPool_;// 用于处理连接的线程池
ConnectionCallback connectionCallback_;
MessageCallback messageCallback_;
WriteCompleteCallback writeCompleteCallback_;
ThreadInitCallback threadInitCallback_;
AtomicInt32 started_;
// always in loop thread
int nextConnId_;
ConnectionMap connections_;//用于存储以及存在的连接

属性访问及设置函数

const string& ipPort() const { return ipPort_; }
const string& name() const { return name_; }
EventLoop* getLoop() const { return loop_; }
void setThreadNum(int numThreads);
void setThreadInitCallback(const ThreadInitCallback& cb){ threadInitCallback_ = cb; }
/// valid after calling start()
std::shared_ptr<EventLoopThreadPool> threadPool(){ return threadPool_; }
void setConnectionCallback(const ConnectionCallback& cb)  { connectionCallback_ = cb; }
  /// Set message callback.
  /// Not thread safe.
void setMessageCallback(const MessageCallback& cb)  { messageCallback_ = cb; }
  /// Set write complete callback.
  /// Not thread safe.
void setWriteCompleteCallback(const WriteCompleteCallback& cb)  { writeCompleteCallback_ = cb; }

构造函数及析构函数

TcpServer::TcpServer(EventLoop* loop,
                     const InetAddress& listenAddr,
                     const string& nameArg,
                     Option option)
  : loop_(CHECK_NOTNULL(loop)),
    ipPort_(listenAddr.toIpPort()),
    name_(nameArg),
    acceptor_(new Acceptor(loop, listenAddr, option == kReusePort)),
    threadPool_(new EventLoopThreadPool(loop, name_)),
    connectionCallback_(defaultConnectionCallback),
    messageCallback_(defaultMessageCallback),
    nextConnId_(1)
{
  acceptor_->setNewConnectionCallback(
      std::bind(&TcpServer::newConnection, this, _1, _2));
}

TcpServer::~TcpServer()
{
  loop_->assertInLoopThread();
  LOG_TRACE << "TcpServer::~TcpServer [" << name_ << "] destructing";

  for (auto& item : connections_)
  {
    TcpConnectionPtr conn(item.second);
    item.second.reset();
    conn->getLoop()->runInLoop(
      std::bind(&TcpConnection::connectDestroyed, conn));
  }
}

服务器执行监听职责

void TcpServer::start()
{
  if (started_.getAndSet(1) == 0)
  {
    threadPool_->start(threadInitCallback_);

    assert(!acceptor_->listening());
    loop_->runInLoop(
        std::bind(&Acceptor::listen, get_pointer(acceptor_)));
  }
}

重要成员

void TcpServer::newConnection(int sockfd, const InetAddress& peerAddr)

此函数是新的连接到达后执行的函数,他会作为回调函数传递给acceptor_中处理新连接的回调函数;

他需要根据新连接的套接字及地址构建新的TcpConnectionPtr,并将给连接存储到map中;

移除连接方法

void TcpServer::removeConnection(const TcpConnectionPtr& conn)
{
  // FIXME: unsafe
  loop_->runInLoop(std::bind(&TcpServer::removeConnectionInLoop, this, conn));
}

void TcpServer::removeConnectionInLoop(const TcpConnectionPtr& conn)
{
  loop_->assertInLoopThread();
  LOG_INFO << "TcpServer::removeConnectionInLoop [" << name_
           << "] - connection " << conn->name();
  size_t n = connections_.erase(conn->name());
  (void)n;
  assert(n == 1);
  EventLoop* ioLoop = conn->getLoop();
  ioLoop->queueInLoop(
      std::bind(&TcpConnection::connectDestroyed, conn));
}
posted @ 2024-03-01 11:22  孟秋十三  阅读(30)  评论(0)    收藏  举报