void Pair::changeState()

in gloo/transport/tcp/pair.cc [873:914]


void Pair::changeState(state nextState) noexcept {
  if (nextState == CLOSED) {
    switch (state_) {
      case INITIALIZING:
        // This state persists from construction up to the point where
        // Pair::listen sets fd_ and calls listen(2). If this fails,
        // it takes care of cleaning up the socket itself.
        // There is no additional cleanup needed here.
        break;
      case LISTENING:
        // The pair may be in the LISTENING state when it is destructed.
        if (fd_ != FD_INVALID) {
          device_->unregisterDescriptor(fd_, this);
          ::close(fd_);
          fd_ = FD_INVALID;
        }
        break;
      case CONNECTING:
        // The pair may be in the CONNECTING state when it is destructed.
        if (fd_ != FD_INVALID) {
          device_->unregisterDescriptor(fd_, this);
          ::close(fd_);
          fd_ = FD_INVALID;
        }
        break;
      case CONNECTED:
        if (!sync_) {
          device_->unregisterDescriptor(fd_, this);
        }
        ::close(fd_);
        fd_ = FD_INVALID;
        break;
      case CLOSED:
        // This can't happen, because we ignore no-op state changes above.
        // We handle it regardless to have a case for every enum value.
        break;
    }
  }

  state_ = nextState;
  cv_.notify_all();
}