開源代碼庫,32muduo_net庫源碼分析(八)

 2023-11-10 阅读 25 评论 0

摘要:1.Acceptor (1)Acceptor用于accept(2)接受TCP連接 (2)Acceptor的數據成員包括Socket、Channel,Acceptor的socket是listening socket(即serversocket)。Channel用于觀察此socket的readable事件,并回調Accptor::handle

1.Acceptor

(1)Acceptor用于accept(2)接受TCP連接

(2)Acceptor的數據成員包括Socket、Channel,Acceptor的socket是listening socket(即serversocket)。Channel用于觀察此socket的readable事件,并回調Accptor::handleRead(),后者調用accept(2)來接受新連接,并回調用戶callback。

2.代碼

1.Acceptor.h

// Copyright 2010, Shuo Chen.  All rights reserved.
// http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.// Author: Shuo Chen (chenshuo at chenshuo dot com)
//
// This is an internal header file, you should not include this.#ifndef MUDUO_NET_ACCEPTOR_H
#define MUDUO_NET_ACCEPTOR_H#include <boost/function.hpp>
#include <boost/noncopyable.hpp>#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 : boost::noncopyable
{public:typedef boost::function<void (int sockfd,const InetAddress&)> NewConnectionCallback;Acceptor(EventLoop* loop, const InetAddress& listenAddr);~Acceptor();void setNewConnectionCallback(const NewConnectionCallback& cb){ newConnectionCallback_ = cb; }bool listenning() const { return listenning_; }void listen();private:void handleRead();EventLoop* loop_;Socket acceptSocket_;Channel acceptChannel_;NewConnectionCallback newConnectionCallback_;bool listenning_;int idleFd_;
};}
}#endif  // MUDUO_NET_ACCEPTOR_H

開源代碼庫,

2.Acceptor.cc

// Copyright 2010, Shuo Chen.  All rights reserved.
// http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.// Author: Shuo Chen (chenshuo at chenshuo dot com)#include <muduo/net/Acceptor.h>#include <muduo/net/EventLoop.h>
#include <muduo/net/InetAddress.h>
#include <muduo/net/SocketsOps.h>#include <boost/bind.hpp>#include <errno.h>
#include <fcntl.h>
//#include <sys/types.h>
//#include <sys/stat.h>using namespace muduo;
using namespace muduo::net;Acceptor::Acceptor(EventLoop* loop, const InetAddress& listenAddr): loop_(loop),acceptSocket_(sockets::createNonblockingOrDie()),acceptChannel_(loop, acceptSocket_.fd()),listenning_(false),idleFd_(::open("/dev/null", O_RDONLY | O_CLOEXEC))
{assert(idleFd_ >= 0);acceptSocket_.setReuseAddr(true);acceptSocket_.bindAddress(listenAddr);acceptChannel_.setReadCallback(boost::bind(&Acceptor::handleRead, this));
}Acceptor::~Acceptor()
{acceptChannel_.disableAll();acceptChannel_.remove();::close(idleFd_);
}void Acceptor::listen()
{loop_->assertInLoopThread();listenning_ = true;acceptSocket_.listen();acceptChannel_.enableReading();
}void Acceptor::handleRead()
{loop_->assertInLoopThread();InetAddress peerAddr(0);//FIXME loop until no moreint connfd = acceptSocket_.accept(&peerAddr);if (connfd >= 0){// string hostport = peerAddr.toIpPort();// LOG_TRACE << "Accepts of " << hostport;if (newConnectionCallback_){newConnectionCallback_(connfd, peerAddr);}else{sockets::close(connfd);}}else{// Read the section named "The special problem of// accept()ing when you can't" in libev's doc.// By Marc Lehmann, author of livev.if (errno == EMFILE){::close(idleFd_);idleFd_ = ::accept(acceptSocket_.fd(), NULL, NULL);::close(idleFd_);idleFd_ = ::open("/dev/null", O_RDONLY | O_CLOEXEC);}}
}

3.Accepotr示例

jdk源碼剖析手冊。

void newConnection(int sockfd, const InetAddress& peerAddr)
{printf("newConnection(): accepted a new connection from %s\n",peerAddr.toIpPort().c_str());::write(sockfd, "How are you?\n", 13);sockets::close(sockfd);
}int main()
{printf("main(): pid = %d\n", getpid());InetAddress listenAddr(8888);EventLoop loop;Acceptor acceptor(&loop, listenAddr);acceptor.setNewConnectionCallback(newConnection);acceptor.listen();loop.loop();
}


版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/169636.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息