1

Write a program which processed data output from ssh request, data from request returns in real time ~1024 bytes per second and i want to process it in real time (drawing a graphics). But ssh_channel_poll function return non zero only when 4096 bytes or when end of output. Channel and session setting up like non-blocking.

bool SSHConnection::execute_process(const std::string &command) {
  try {
    if (!connect())
      throw std::string("Connection error");

    if (this->is_processed)
      throw std::string("Process already going");

    this->process_channel = ssh_channel_new(this->connection);
    if (this->process_channel == nullptr)
      throw std::string("Channel create error");

    int rc = ssh_channel_open_session(this->process_channel);
    if (rc != SSH_OK) {
      ssh_channel_free(this->process_channel);
      throw std::string("Channel open error");
    }

    rc = ssh_channel_request_exec(this->process_channel, command.c_str());
    if (rc != SSH_OK) {
      ssh_channel_free(this->process_channel);
      throw std::string("Execute command error");
    }
    ssh_set_blocking(this->connection, 0);
    ssh_channel_set_blocking(this->process_channel, 0);
    this->is_processed = true;

    this->process_thread = new std::thread(&SSHConnection::process, this);

  } catch (const std::string &err_msg) {
    std::cerr << err_msg << std::endl;
    emit Error(QString::fromStdString(err_msg));
    return false;
  }
  return true;
}

void SSHConnection::process() {
  std::string output;
  char buffer[256];
  int nbytes;

  while (!ssh_channel_is_eof(this->process_channel) && this->is_processed) {

    if (ssh_channel_poll(this->process_channel, 0) > 0) {
      nbytes =
          ssh_channel_read_nonblocking(this->process_channel, buffer, sizeof(buffer), 0);
      output.clear();
      output.append(buffer, nbytes);
      emit DataReady(QString::fromStdString(output));
    }
  }
  this->is_processed = false;
  ssh_channel_send_eof(this->process_channel);
  ssh_channel_close(this->process_channel);
  ssh_channel_free(this->process_channel);
  disconnect();
  emit DataEnd();
}

How to change polling trigger from 4096 to lower?

0 Answers0