void TcpAcceptor::HandleRead() { // listen socket is readable, a new connetion is request InternetAddress peerAddress; socket_t connfd = listenSocket_.Accept(peerAddress); assert(connfd >= 0); if (newConnCallBack_) { newConnCallBack_(connfd, peerAddress); } else { // since non handler for the read event, shutdown the connection directly SocketsApi::Close(connfd); } }
void Acceptor::handleRead() { loop_->assertInLoopThread(); int errorCode = 0; while(true) { InetAddress peerAddr; sockaddr addr = {0}; socket_t newFd = SocketOps::accept(listenFd_, &addr); if(newFd < 0) { errorCode = GetLastErrorCode(); break; } SocketOps::setSocketNoneBlocking(newFd); peerAddr.setSockAddr(addr); if(newConnCallBack_) { newConnCallBack_(newFd, peerAddr); } else { SocketOps::closeSocket(newFd); } } if(!ERR_ACCEPT_RETRIABLE(errorCode)) { LOG_PRINT(LogType_Error, "accept socket failed:%s %s:%d", GetErrorText(errorCode).c_str(), __FUNCTION__, __LINE__); } }