📄 poll.c
字号:
};
APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset,
apr_uint32_t size,
apr_pool_t *p,
apr_uint32_t flags)
{
#if !defined(HAVE_POLL) && defined(FD_SETSIZE)
if (size > FD_SETSIZE) {
*pollset = NULL;
return APR_EINVAL;
}
#endif
*pollset = apr_palloc(p, sizeof(**pollset));
(*pollset)->nelts = 0;
(*pollset)->nalloc = size;
#ifdef HAVE_POLL
(*pollset)->pollset = apr_palloc(p, size * sizeof(struct pollfd));
#else
FD_ZERO(&((*pollset)->readset));
FD_ZERO(&((*pollset)->writeset));
FD_ZERO(&((*pollset)->exceptset));
(*pollset)->maxfd = 0;
#ifdef NETWARE
(*pollset)->set_type = APR_NO_DESC;
#endif
#endif
(*pollset)->query_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
(*pollset)->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
(*pollset)->pool = p;
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t *pollset)
{
/* A no-op function for now. If we later implement /dev/poll
* support, we'll need to close the /dev/poll fd here
*/
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset,
const apr_pollfd_t *descriptor)
{
#ifndef HAVE_POLL
apr_os_sock_t fd;
#endif
if (pollset->nelts == pollset->nalloc) {
return APR_ENOMEM;
}
pollset->query_set[pollset->nelts] = *descriptor;
#ifdef HAVE_POLL
if (descriptor->desc_type == APR_POLL_SOCKET) {
pollset->pollset[pollset->nelts].fd = descriptor->desc.s->socketdes;
}
else {
pollset->pollset[pollset->nelts].fd = descriptor->desc.f->filedes;
}
pollset->pollset[pollset->nelts].events = get_event(descriptor->reqevents);
#else
if (descriptor->desc_type == APR_POLL_SOCKET) {
#ifdef NETWARE
/* NetWare can't handle mixed descriptor types in select() */
if (HAS_PIPES(pollset->set_type)) {
return APR_EBADF;
}
else {
pollset->set_type = APR_POLL_SOCKET;
}
#endif
fd = descriptor->desc.s->socketdes;
}
else {
#if !APR_FILES_AS_SOCKETS
return APR_EBADF;
#else
#ifdef NETWARE
/* NetWare can't handle mixed descriptor types in select() */
if (descriptor->desc.f->is_pipe && !HAS_SOCKETS(pollset->set_type)) {
pollset->set_type = APR_POLL_FILE;
fd = descriptor->desc.f->filedes;
}
else {
return APR_EBADF;
}
#else
fd = descriptor->desc.f->filedes;
#endif
#endif
}
#if !defined(WIN32) && !defined(NETWARE) /* socket sets handled with array of handles */
if (fd >= FD_SETSIZE) {
/* XXX invent new error code so application has a clue */
return APR_EBADF;
}
#endif
if (descriptor->reqevents & APR_POLLIN) {
FD_SET(fd, &(pollset->readset));
}
if (descriptor->reqevents & APR_POLLOUT) {
FD_SET(fd, &(pollset->writeset));
}
if (descriptor->reqevents &
(APR_POLLPRI | APR_POLLERR | APR_POLLHUP | APR_POLLNVAL)) {
FD_SET(fd, &(pollset->exceptset));
}
if ((int)fd > pollset->maxfd) {
pollset->maxfd = (int)fd;
}
#endif
pollset->nelts++;
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset,
const apr_pollfd_t *descriptor)
{
apr_uint32_t i;
#ifndef HAVE_POLL
apr_os_sock_t fd;
#endif
#ifdef HAVE_POLL
for (i = 0; i < pollset->nelts; i++) {
if (descriptor->desc.s == pollset->query_set[i].desc.s) {
/* Found an instance of the fd: remove this and any other copies */
apr_uint32_t dst = i;
apr_uint32_t old_nelts = pollset->nelts;
pollset->nelts--;
for (i++; i < old_nelts; i++) {
if (descriptor->desc.s == pollset->query_set[i].desc.s) {
pollset->nelts--;
}
else {
pollset->pollset[dst] = pollset->pollset[i];
pollset->query_set[dst] = pollset->query_set[i];
dst++;
}
}
return APR_SUCCESS;
}
}
#else /* no poll */
if (descriptor->desc_type == APR_POLL_SOCKET) {
fd = descriptor->desc.s->socketdes;
}
else {
#if !APR_FILES_AS_SOCKETS
return APR_EBADF;
#else
fd = descriptor->desc.f->filedes;
#endif
}
for (i = 0; i < pollset->nelts; i++) {
if (descriptor->desc.s == pollset->query_set[i].desc.s) {
/* Found an instance of the fd: remove this and any other copies */
apr_uint32_t dst = i;
apr_uint32_t old_nelts = pollset->nelts;
pollset->nelts--;
for (i++; i < old_nelts; i++) {
if (descriptor->desc.s == pollset->query_set[i].desc.s) {
pollset->nelts--;
}
else {
pollset->query_set[dst] = pollset->query_set[i];
dst++;
}
}
FD_CLR(fd, &(pollset->readset));
FD_CLR(fd, &(pollset->writeset));
FD_CLR(fd, &(pollset->exceptset));
if (((int)fd == pollset->maxfd) && (pollset->maxfd > 0)) {
pollset->maxfd--;
}
return APR_SUCCESS;
}
}
#endif /* no poll */
return APR_NOTFOUND;
}
#ifdef HAVE_POLL
APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
apr_interval_time_t timeout,
apr_int32_t *num,
const apr_pollfd_t **descriptors)
{
int rv;
apr_uint32_t i, j;
if (timeout > 0) {
timeout /= 1000;
}
rv = poll(pollset->pollset, pollset->nelts, timeout);
(*num) = rv;
if (rv < 0) {
return apr_get_netos_error();
}
if (rv == 0) {
return APR_TIMEUP;
}
j = 0;
for (i = 0; i < pollset->nelts; i++) {
if (pollset->pollset[i].revents != 0) {
pollset->result_set[j] = pollset->query_set[i];
pollset->result_set[j].rtnevents =
get_revent(pollset->pollset[i].revents);
j++;
}
}
*descriptors = pollset->result_set;
return APR_SUCCESS;
}
#else /* no poll */
APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
apr_interval_time_t timeout,
apr_int32_t *num,
const apr_pollfd_t **descriptors)
{
int rv;
apr_uint32_t i, j;
struct timeval tv, *tvptr;
fd_set readset, writeset, exceptset;
if (timeout < 0) {
tvptr = NULL;
}
else {
tv.tv_sec = (long)apr_time_sec(timeout);
tv.tv_usec = (long)apr_time_usec(timeout);
tvptr = &tv;
}
memcpy(&readset, &(pollset->readset), sizeof(fd_set));
memcpy(&writeset, &(pollset->writeset), sizeof(fd_set));
memcpy(&exceptset, &(pollset->exceptset), sizeof(fd_set));
#ifdef NETWARE
if (HAS_PIPES(pollset->set_type)) {
rv = pipe_select(pollset->maxfd + 1, &readset, &writeset, &exceptset, tvptr);
}
else
#endif
rv = select(pollset->maxfd + 1, &readset, &writeset, &exceptset, tvptr);
/* Set initial *num now for expected -1 / 0 failures, or errors below */
(*num) = rv;
if (rv < 0) {
return apr_get_netos_error();
}
if (rv == 0) {
return APR_TIMEUP;
}
j = 0;
for (i = 0; i < pollset->nelts; i++) {
apr_os_sock_t fd;
if (pollset->query_set[i].desc_type == APR_POLL_SOCKET) {
fd = pollset->query_set[i].desc.s->socketdes;
}
else {
#if !APR_FILES_AS_SOCKETS
return APR_EBADF;
#else
fd = pollset->query_set[i].desc.f->filedes;
#endif
}
if (FD_ISSET(fd, &readset) || FD_ISSET(fd, &writeset) ||
FD_ISSET(fd, &exceptset)) {
pollset->result_set[j] = pollset->query_set[i];
pollset->result_set[j].rtnevents = 0;
if (FD_ISSET(fd, &readset)) {
pollset->result_set[j].rtnevents |= APR_POLLIN;
}
if (FD_ISSET(fd, &writeset)) {
pollset->result_set[j].rtnevents |= APR_POLLOUT;
}
if (FD_ISSET(fd, &exceptset)) {
pollset->result_set[j].rtnevents |= APR_POLLERR;
}
j++;
}
}
/* Reset computed *num to account for multiply-polled fd's which
* select() - on some platforms, treats as a single fd result.
* The *num returned must match the size of result_set[]
*/
(*num) = j;
*descriptors = pollset->result_set;
return APR_SUCCESS;
}
#endif /* no poll */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -