| 322 | } |
| 323 | |
| 324 | int CChannelSSHTunnel::DoWait(bool bWrite, int timeout) |
| 325 | { |
| 326 | int nRet = 0; |
| 327 | if(!m_Channel || !ssh_channel_is_open(m_Channel) |
| 328 | || ssh_channel_is_eof(m_Channel)) { |
| 329 | QString szErr = "The channel is not open"; |
| 330 | qCritical(log) << szErr; |
| 331 | setErrorString(szErr); |
| 332 | return -1; |
| 333 | } |
| 334 | |
| 335 | fd_set set; |
| 336 | FD_ZERO(&set); |
| 337 | |
| 338 | struct timeval tm = {0, timeout}; |
| 339 | ssh_channel channels[2], channel_out[2]; |
| 340 | channels[0] = m_Channel; |
| 341 | channels[1] = nullptr; |
| 342 | |
| 343 | if(bWrite) { |
| 344 | socket_t fd = SSH_INVALID_SOCKET; |
| 345 | if(m_pEvent) |
| 346 | fd = GetSocket(); |
| 347 | if(SSH_INVALID_SOCKET != fd) |
| 348 | FD_SET(fd, &set); |
| 349 | nRet = select(fd + 1, nullptr, &set, nullptr, &tm); |
| 350 | if(0 > nRet) return nRet; |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | //qDebug(log) << "ssh_select:" << fd; |
| 355 | nRet = ssh_select(channels, channel_out, 1, &set, &tm); |
| 356 | //qDebug(log) << "ssh_select end:" << nRet; |
| 357 | if(EINTR == nRet) |
| 358 | return 0; |
| 359 | |
| 360 | if(SSH_OK != nRet) { |
| 361 | QString szErr; |
| 362 | szErr = "ssh_channel_select failed: " + QString::number(nRet); |
| 363 | szErr += ssh_get_error(m_Session); |
| 364 | qCritical(log) << szErr; |
| 365 | setErrorString(szErr); |
| 366 | return -3; |
| 367 | } |
| 368 | |
| 369 | if(!channel_out[0]) { |
| 370 | //qDebug(log) << "The channel is not select"; |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | if(ssh_channel_is_eof(m_Channel)) { |
| 375 | qWarning(log) << "Channel is eof"; |
| 376 | setErrorString(tr("The channel is eof")); |
| 377 | // Stop |
| 378 | return -1; |
| 379 | } |
| 380 | |
| 381 | // Get channel data length |