int Acceptor::accept(struct sockaddr_in* addr) { socklen_t addrlen = static_cast<socklen_t>(sizeof *addr); #if VALGRIND int connfd = accept(m_sockfd, sockaddr_cast(addr), &addrlen); Socket::setNonBlock(connfd); Socket::setCloseExec(connfd); #else int connfd = accept4(m_sockfd, (struct sockaddr*)(addr), &addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC); #endif if (connfd < 0) { int err = errno; if (err == EMFILE || err == ENFILE) { ERROR("accept error %s", strerror(err)); close(m_dummyFd); connfd = ::accept(m_sockfd, nullptr, nullptr); close(connfd); m_dummyFd = createDummyFd(); } return -1; } else { INFO("onAccept %d", connfd); //设置TCP缓冲数据立即发送 Socket::setNoDelay(connfd, true); return connfd; } }
void Acceptor::onAccept(IOLoop* loop, int events) { int sockFd = accept4(m_sockFd, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC); if(sockFd < 0) { int err = errno; if(err == EMFILE || err == ENFILE) { LOG_ERROR("accept error %s", errorMsg(err)); close(m_dummyFd); sockFd = accept(m_sockFd, NULL, NULL); close(sockFd); m_dummyFd = createDummyFd(); } return; } else { LOG_INFO("onAccept %d", sockFd); SockUtil::setNoDelay(sockFd, true); m_callback(loop, sockFd); } }
Acceptor::Acceptor(const NewConnCallback_t& callback) : m_loop(0) , m_sockFd(0) , m_dummyFd(createDummyFd()) , m_running(false) , m_callback(callback) { }
Acceptor::Acceptor(EventLoop *loop) : m_sockfd(0) , m_dummyFd(createDummyFd()),m_loop(loop) { }