Exemplo n.º 1
0
/*
 * Wait for file or timer events and handle them. This function returns 1 if there are no files or
 * timers to wait for, -1 if some error occurred, or 0 if any number of events was successfully
 * handled.
 */
int disHandleEvents(Dispatcher *dis)
{
    int nfds, r;
    fd_set rfds, wfds;
    struct timeval *tv;

P   dbgPrint(stderr, "Calling disPrepareSelect.\n");

    r = disPrepareSelect(dis, &nfds, &rfds, &wfds, &tv);

P   dbgPrint(stderr, "disPrepareSelect returned:\n\tr:    %d\n\tnfds: %d\n", r, nfds);

    if (r < 0) {
        /* Fake a timeout. */

        disProcessSelect(dis, 0, 0, NULL, NULL);

        return 0;
    }
    else if (nfds > 0) {
P       dumpfds(stderr, "\trfds:", nfds, &rfds);
P       dumpfds(stderr, "\twfds:", nfds, &wfds);
    }

P   {
        if (tv == NULL) {
            fprintf(stderr, "\ttv:   NULL\n");
        }
        else {
            fprintf(stderr, "\ttv:   %ld.%06ld\n", tv->tv_sec, tv->tv_usec);
        }
    }

    if (nfds == 0 && tv == NULL) {
P       dbgPrint(stderr, "No more files, no more timeouts: return 1.\n");
        return 1;
    }

P   dbgPrint(stderr, "Calling select.\n");

    r = select(nfds, &rfds, &wfds, NULL, tv);

P   dbgPrint(stderr, "select returned %d\n", r);

    if (r < 0) {
        return r;
    }
    else if (r > 0) {
P       dumpfds(stderr, "\trfds:", nfds, &rfds);
P       dumpfds(stderr, "\twfds:", nfds, &wfds);
    }

P   dbgPrint(stderr, "Calling disProcessSelect.\n");

    disProcessSelect(dis, r, nfds, &rfds, &wfds);

    return 0;
}
Exemplo n.º 2
0
/*
 * Prepare a call to select() based on the files and timeouts set in <ns>. The necessary parameters
 * to select() are returned through <nfds>, <rfds>, <wfds> and <tv> (exception-fds should be set to
 * NULL). <*tv> is set to point to an appropriate timeout value, or NULL if no timeout is to be set.
 * This function will clear <rfds> and <wfds>, so if callers want to add their own file descriptors,
 * they should do so after calling this function. This function returns -1 if the first timeout
 * should already have occurred, otherwise 0.
 */
int nsPrepareSelect(NS *ns, int *nfds, fd_set *rfds, fd_set *wfds, struct timeval **tv)
{
    return disPrepareSelect(&ns->dis, nfds, rfds, wfds, tv);
}