分析一下下面这段代码while(1) { revents = 0; #ifndef DISABLE_LIBSSH if (session->ssh_chan != NULL) { /* we are getting data from libssh's channel */ status = ssh_channel_poll_timeout(session->ssh_chan, timeout, 0); if (status > 0) { revents = POLLIN; } } else #endif #ifdef ENABLE_TLS if (session->tls != NULL) { /* we are getting data from TLS session using OpenSSL */ fds.fd = SSL_get_fd(session->tls); fds.events = POLLIN; fds.revents = 0; status = poll(&fds, 1, timeout); revents = (unsigned long int) fds.revents; } else #endif if (session->fd_input != -1) { /* we are getting data from standard file descriptor */ fds.fd = session->fd_input; fds.events = POLLIN; fds.revents = 0; status = poll(&fds, 1, timeout); revents = (unsigned long int) fds.revents; } else { ERROR("Invalid session to receive data."); return (NC_MSG_UNKNOWN); } /* process the result */ if (status == 0) { /* timed out */ DBG_UNLOCK("mut_channel"); pthread_mutex_unlock(session->mut_channel); return (NC_MSG_WOULDBLOCK); } else if (((status == -1) && (errno == EINTR)) #ifndef DISABLE_LIBSSH || (status == SSH_AGAIN) #endif ) { /* poll was interrupted */ continue; } else if (status < 0) { /* poll failed - something wrong happend, close this socket and wait for another request */ DBG_UNLOCK("mut_channel"); pthread_mutex_unlock(session->mut_channel); #ifndef DISABLE_LIBSSH if (status == SSH_EOF) { emsg = "end of file"; } else if (!session->ssh_chan) { emsg = strerror(errno); } else if (session->ssh_sess) { emsg = ssh_get_error(session->ssh_sess); } else { emsg = "description not available"; } #else emsg = strerror(errno); #endif WARN("Input channel error (%s)", emsg); nc_session_close(session, NC_SESSION_TERM_DROPPED); if (nc_info) { pthread_rwlock_wrlock(&(nc_info->lock)); nc_info->stats.sessions_dropped++; pthread_rwlock_unlock(&(nc_info->lock)); } return (NC_MSG_UNKNOWN); } /* status > 0 */ /* check the status of the socket */ /* if nothing to read and POLLHUP (EOF) or POLLERR set */ if ((revents & POLLHUP) || (revents & POLLERR)) { /* close client's socket (it's probably already closed by client */ DBG_UNLOCK("mut_channel"); pthread_mutex_unlock(session->mut_channel); ERROR("Input channel closed"); nc_session_close(session, NC_SESSION_TERM_DROPPED); if (nc_info) { pthread_rwlock_wrlock(&(nc_info->lock)); nc_info->stats.sessions_dropped++; pthread_rwlock_unlock(&(nc_info->lock)); } return (NC_MSG_UNKNOWN); } /* we have something to read */ break; }
时间: 2024-04-23 21:27:02 浏览: 174
这段代码是一个循环,用于从网络通道中读取数据,直到有数据可读或者等待超时。在循环中,代码会根据条件编译选择不同的代码路径,从不同的网络通道中获取数据。当获取到数据后,代码会根据获取到的数据的状态进行处理。如果等待超时,则返回NC_MSG_WOULDBLOCK表示没有数据可读;如果获取到的数据出现错误,则关闭网络通道,并返回NC_MSG_UNKNOWN表示未知错误;如果获取到的数据可用,则退出循环,返回获取到的数据。
循环的目的是保证能够不断地从网络通道中获取数据,直到获取到数据为止。在循环中,代码会不断地进行读取数据的尝试,直到获取到数据或者等待超时为止。如果循环执行完毕后仍未获取到数据,则说明网络通道中没有可读数据。
相关问题
#ifndef DISABLE_LIBSSH if (session->ssh_chan != NULL) { /* we are getting data from libssh's channel */ status = ssh_channel_poll_timeout(session->ssh_chan, timeout, 0); if (status > 0) { revents = POLLIN; } } else #endif #ifdef ENABLE_TLS if (session->tls != NULL) { /* we are getting data from TLS session using OpenSSL */ fds.fd = SSL_get_fd(session->tls); fds.events = POLLIN; fds.revents = 0; status = poll(&fds, 1, timeout); revents = (unsigned long int) fds.revents; } else #endif
这段代码是一个网络库的代码,主要是用于从网络通道中读取数据。其中,该代码使用了条件编译,根据是否定义了DISABLE_LIBSSH和ENABLE_TLS宏来选择不同的代码路径。当DISABLE_LIBSSH宏被定义时,该代码段会从libssh的channel中获取数据;当ENABLE_TLS宏被定义时,该代码段会从OpenSSL的TLS session中获取数据。poll函数会等待timeout时间,直到有数据可读或者等待超时返回。如果有数据可读,则将revents设置为POLLIN表示有数据可读。
void nc_session_close(struct nc_session* session, NC_SESSION_TERM_REASON reason) { int i; struct nc_msg *qmsg, *qmsg_aux; NC_SESSION_STATUS sstatus = session->status; int session_fd= nc_session_get_eventfd(session); if(session_fd!=-1&&session_fd!=0) client_tofree_same_socket(session_fd); /* lock session due to accessing its status and other items */ if (sstatus != NC_SESSION_STATUS_DUMMY) { DBG_LOCK("mut_session"); pthread_mutex_lock(&(session->mut_session)); } /* close the SSH session */ if (session != NULL && session->status != NC_SESSION_STATUS_CLOSING && session->status != NC_SESSION_STATUS_CLOSED) { #ifndef DISABLE_LIBSSH if (session->ssh_chan && ssh_channel_is_eof(session->ssh_chan)) { session->status = NC_SESSION_STATUS_ERROR; } #endif announce_nc_session_closing(session); if (sstatus != NC_SESSION_STATUS_DUMMY) { DBG_UNLOCK("mut_session"); pthread_mutex_unlock(&(session->mut_session)); } #ifndef DISABLE_NOTIFICATIONS if (!ncntf_dispatch) { /* let notification receiving/sending function stop, if any */ ncntf_dispatch_stop(session); } /* log closing of the session */ if (sstatus != NC_SESSION_STATUS_DUMMY) { ncntf_event_new(-1, NCNTF_BASE_SESSION_END, session, reason, NULL); } #endif
这是一个函数的具体实现,它接受两个参数:一个指向nc_session类型的指针和一个NC_SESSION_TERM_REASON类型的值。函数的作用是关闭给定的nc_session会话,并指定关闭的原因。
实现中,首先获取会话的状态并锁定该会话,然后关闭SSH会话(如果存在)并更新会话状态。如果通知功能未被禁用,它还将停止通知接收/发送功能并记录会话关闭的事件。最后,释放锁并返回。
阅读全文