ReceiverState ReceiverThread::acceptFirstConnection()

in ReceiverThread.cpp [138:190]


ReceiverState ReceiverThread::acceptFirstConnection() {
  WTVLOG(1) << "entered ACCEPT_FIRST_CONNECTION state";

  reset();
  socket_->closeNoCheck();
  auto timeout = options_.accept_timeout_millis;
  int acceptAttempts = 0;
  while (true) {
    // Move to timeout state if some other thread was successful
    // in getting a connection
    if (wdtParent_->hasNewTransferStarted()) {
      return ACCEPT_WITH_TIMEOUT;
    }
    switch (wdtParent_->getAcceptMode()) {
      case Receiver::AcceptMode::ACCEPT_WITH_RETRIES: {
        if (acceptAttempts >= options_.max_accept_retries) {
          WTLOG(ERROR) << "Unable to accept after " << acceptAttempts
                       << " attempts";
          threadStats_.setLocalErrorCode(CONN_ERROR);
          return FINISH_WITH_ERROR;
        }
        break;
      }
      case Receiver::AcceptMode::ACCEPT_FOREVER: {
        WTVLOG(2) << "Receiver is configured to accept for-ever";
        break;
      }
      case Receiver::AcceptMode::STOP_ACCEPTING: {
        WTLOG(ERROR) << "Receiver is asked to stop accepting, attempts : "
                     << acceptAttempts;
        threadStats_.setLocalErrorCode(CONN_ERROR);
        return FINISH_WITH_ERROR;
      }
    }
    if (wdtParent_->getCurAbortCode() != OK) {
      WTLOG(ERROR) << "Thread marked to abort while trying to accept "
                   << "first connection. Num attempts " << acceptAttempts;
      threadStats_.setLocalErrorCode(ABORT);
      return FINISH_WITH_ERROR;
    }
    ErrorCode code =
        socket_->acceptNextConnection(timeout, curConnectionVerified_);
    if (code == OK) {
      break;
    }
    ++acceptAttempts;
  }
  // Make the parent start new global session. This is executed
  // only by the first thread that calls this function
  controller_->executeAtStart(
      [&]() { wdtParent_->startNewGlobalSession(socket_->getPeerIp()); });
  return READ_NEXT_CMD;
}