示例#1
0
        void Acceptor::handleRead()
        {
            loop_->assertInLoopThread();
            InetAddress peerAddr;
            int connfd = acceptSocket_.accept( &peerAddr );
            if (connfd >= 0)
            {
                MDLog( "peerIp:%s, peerPort:%s", peerAddr.toIp( ).c_str(), peerAddr.toIpPort().c_str());
                if (newConnectionCallback_)
                {
                    newConnectionCallback_( connfd, peerAddr );
                }
                else
                {
                    sockets::close( connfd );
                }
            }
            else
            {

                if (errno == EAGAIN)
                {
                    return;
                }

                MDError( "accept error [%d]", errno );
            }
        }
示例#2
0
void Acceptor::handleRead()
{
	loop_->assertInLoopThread();
	InetAddress peerAddr;

	//FIXME loop until no more
	//用while来包围accept
	//在Socket的accpet方法里面,就已经把connfd设置非阻塞了。
	int connfd = acceptSocket_.accept(&peerAddr);//!Important:connfd为非阻塞
	if (connfd >= 0)
	{
		// string hostport = peerAddr.toIpPort();
		// LOG_TRACE << "Accepts of " << hostport;
		if (newConnectionCallback_)
		{
			newConnectionCallback_(connfd, peerAddr);
		}
		else
		{
			sockettool::close(connfd);
		}

	}
	else
	{
		LOG_ERROR << "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 livev.
		if (errno == EMFILE)
		{
			/*
			::close(idleFd_);
			idleFd_ = ::accept(acceptSocket_.fd(), NULL, NULL);
			::close(idleFd_);
			idleFd_ = ::open("/dev/null", O_RDONLY | O_CLOEXEC);
			*/

		}
	}


}