(1)Acceptor用于accept(2)接受TCP連接
// 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
開源代碼庫,
// 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);}}
}
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();
}
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态