Example #1
0
/*
 * comm_setselect
 *
 * This is a needed exported function which will be called to register
 * and deregister interest in a pending IO state for a given FD.
 */
void
comm_setselect(fde_t *F, unsigned int type, PF *handler,
               void *client_data, time_t timeout)
{  
  int new_events;

  if ((type & COMM_SELECT_READ))
  {
    F->read_handler = handler;
    F->read_data = client_data;
  }

  if ((type & COMM_SELECT_WRITE))
  {
    F->write_handler = handler;
    F->write_data = client_data;
  }

  new_events = (F->read_handler ? POLLRDNORM : 0) |
    (F->write_handler ? POLLWRNORM : 0);

  if (timeout != 0)
    F->timeout = CurrentTime + (timeout / 1000);

  if (new_events != F->evcache)
  {
    if (new_events == 0)
    {
      pollfds[F->comm_index].fd = -1;
      pollfds[F->comm_index].revents = 0;

      if (pollmax == F->comm_index)
        while (pollmax >= 0 && pollfds[pollmax].fd == -1)
	  pollmax--;
    }
    else
    {
      if (F->evcache == 0)
      {
        F->comm_index = poll_findslot();
	if (F->comm_index > pollmax)
	  pollmax = F->comm_index;

	pollfds[F->comm_index].fd = F->fd;
      }
      pollfds[F->comm_index].events = new_events;
      pollfds[F->comm_index].revents = 0;
    }

    F->evcache = new_events;
  }
}
Example #2
0
/*
 * set and clear entries in the pollfds[] array.
 */
static void poll_update_pollfds(int fd, short event, PF * handler)
{
    fde_t *F = &fd_table[fd];
    int comm_index;

    if (F->comm_index < 0)
    {
        F->comm_index = poll_findslot();
    }
    comm_index = F->comm_index;

    /* Update the events */
    if (handler)
    {
        F->list = FDLIST_IDLECLIENT;
        pollfd_list.pollfds[comm_index].events |= event;
        pollfd_list.pollfds[comm_index].fd = fd;
        /* update maxindex here */
        if (comm_index > pollfd_list.maxindex)
            pollfd_list.maxindex = comm_index;
    } else
    {
        if (comm_index >= 0)
        {
            pollfd_list.pollfds[comm_index].events &= ~event;
            if (pollfd_list.pollfds[comm_index].events == 0)
            {
                pollfd_list.pollfds[comm_index].fd = -1;
                pollfd_list.pollfds[comm_index].revents = 0;
                F->comm_index = -1;
                F->list = FDLIST_NONE;

                /* update pollfd_list.maxindex here */
                if (comm_index == pollfd_list.maxindex)
                { 
                    while (pollfd_list.maxindex >= 0 &&
                           pollfd_list.pollfds[pollfd_list.maxindex].fd == -1)
                        pollfd_list.maxindex--;
                }
            }
        }
    }
}