src/ae.cc
/* Process every pending time event, then every pending file event
 * (that may be registered by time event callbacks just processed).
 * Without special flags the function sleeps until some file event
 * fires, or when the next time event occurs (if any).
 *
 * If flags is 0, the function does nothing and returns.
 * if flags has AE_ALL_EVENTS set, all the kind of events are processed.
 * if flags has AE_FILE_EVENTS set, file events are processed.
 * if flags has AE_TIME_EVENTS set, time events are processed.
 * if flags has AE_DONT_WAIT set the function returns ASAP until all
 * the events that's possible to process without to wait are processed.
 *
 * The function returns the number of events processed. */
Process every pending time event, then every pending file event
(that may be registered by time event callbacks just processed).
Without special flags the function sleeps until some file event
fires, or when the next time event occurs (if any).

If flags is 0, the function does nothing and returns.
if flags has AE_ALL_EVENTS set, all the kind of events are processed.
if flags has AE_FILE_EVENTS set, file events are processed.
if flags has AE_TIME_EVENTS set, time events are processed.
if flags has AE_DONT_WAIT set the function returns ASAP until all
the events that's possible to process without to wait are processed.

The function returns the number of events processed.
Process every pending time event then every pending file event ( that may be registered by time event callbacks just processed ) . Without special flags the function sleeps until some file event fires or when the next time event occurs ( if any ) . If flags is 0 the function does nothing and returns . if flags has AE_ALL_EVENTS set all the kind of events are processed . if flags has AE_FILE_EVENTS set file events are processed . if flags has AE_TIME_EVENTS set time events are processed . if flags has AE_DONT_WAIT set the function returns ASAP until all the events that ' s possible to process without to wait are processed . The function returns the number of events processed .
int aeProcessEvents(aeEventLoop *eventLoop, int flags) {
  int processed = 0, numevents;
  if (!(flags & AE_TIME_EVENTS) && !(flags & AE_FILE_EVENTS))
    return 0;
  if (eventLoop->maxfd != -1
      || ((flags & AE_TIME_EVENTS) && !(flags & AE_DONT_WAIT))) {
    int j;
    aeTimeEvent *shortest = NULL;
    struct timeval tv, *tvp;
    if (flags & AE_TIME_EVENTS && !(flags & AE_DONT_WAIT))
      shortest = aeSearchNearestTimer(eventLoop);
    if (shortest) {
      long now_sec, now_ms;
      aeGetTime(&now_sec, &now_ms);
      tvp = &tv;
      tvp->tv_sec = shortest->when_sec - now_sec;
      if (shortest->when_ms < now_ms) {
        tvp->tv_usec = ((shortest->when_ms + 1000) - now_ms) * 1000;
        tvp->tv_sec--;
      } else {
        tvp->tv_usec = (shortest->when_ms - now_ms) * 1000;
      }
      if (tvp->tv_sec < 0)
        tvp->tv_sec = 0;
      if (tvp->tv_usec < 0)
        tvp->tv_usec = 0;
    } else {
      if (flags & AE_DONT_WAIT) {
        tv.tv_sec = tv.tv_usec = 0;
        tvp = &tv;
      } else {
        tvp = NULL; 
      }
    }
    numevents = aeApiPoll(eventLoop, tvp);
    for (j = 0; j < numevents; j++) {
      aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd];
      int mask = eventLoop->fired[j].mask;
      int fd = eventLoop->fired[j].fd;
      int rfired = 0;
      if (fe->mask & mask & AE_READABLE) {
        rfired = 1;
        fe->rfileProc(eventLoop, fd, fe->clientData, mask);
      }
      if (fe->mask & mask & AE_WRITABLE) {
        if (!rfired || fe->wfileProc != fe->rfileProc)
          fe->wfileProc(eventLoop, fd, fe->clientData, mask);
      }
      processed++;
    }
  }
  if (flags & AE_TIME_EVENTS)
    processed += processTimeEvents(eventLoop);
  return processed; 
}
int aeProcessEvents ( aeEventLoop * eventLoop , int flags ) { int processed = 0 , numevents ; if ( ! ( flags & AE_TIME_EVENTS ) && ! ( flags & AE_FILE_EVENTS ) ) return 0 ; if ( eventLoop -> maxfd != -1 || ( ( flags & AE_TIME_EVENTS ) && ! ( flags & AE_DONT_WAIT ) ) ) { int j ; aeTimeEvent * shortest = NULL ; struct timeval tv , * tvp ; if ( flags & AE_TIME_EVENTS && ! ( flags & AE_DONT_WAIT ) ) shortest = aeSearchNearestTimer ( eventLoop ) ; if ( shortest ) { long now_sec , now_ms ; aeGetTime ( & now_sec , & now_ms ) ; tvp = & tv ; tvp -> tv_sec = shortest -> when_sec - now_sec ; if ( shortest -> when_ms < now_ms ) { tvp -> tv_usec = ( ( shortest -> when_ms + 1000 ) - now_ms ) * 1000 ; tvp -> tv_sec -- ; } else { tvp -> tv_usec = ( shortest -> when_ms - now_ms ) * 1000 ; } if ( tvp -> tv_sec < 0 ) tvp -> tv_sec = 0 ; if ( tvp -> tv_usec < 0 ) tvp -> tv_usec = 0 ; } else { if ( flags & AE_DONT_WAIT ) { tv . tv_sec = tv . tv_usec = 0 ; tvp = & tv ; } else { tvp = NULL ; } } numevents = aeApiPoll ( eventLoop , tvp ) ; for ( j = 0 ; j < numevents ; j ++ ) { aeFileEvent * fe = & eventLoop -> events [ eventLoop -> fired [ j ] . fd ] ; int mask = eventLoop -> fired [ j ] . mask ; int fd = eventLoop -> fired [ j ] . fd ; int rfired = 0 ; if ( fe -> mask & mask & AE_READABLE ) { rfired = 1 ; fe -> rfileProc ( eventLoop , fd , fe -> clientData , mask ) ; } if ( fe -> mask & mask & AE_WRITABLE ) { if ( ! rfired || fe -> wfileProc != fe -> rfileProc ) fe -> wfileProc ( eventLoop , fd , fe -> clientData , mask ) ; } processed ++ ; } } if ( flags & AE_TIME_EVENTS ) processed += processTimeEvents ( eventLoop ) ; return processed ; }
Process every pending time event, then every pending file event
 (that may be registered by time event callbacks just processed).
Process every pending time event then every pending file event ( that may be registered by time event callbacks just processed ) .
/* Nothing to do? return ASAP */ /* Note that we want call select() even if there are no
   * file events to process as long as we want to process time
   * events, in order to sleep until the next time event is ready
   * to fire. */ /* Calculate the time missing for the nearest
       * timer to fire. */ /* If we have to check for events but need to return
       * ASAP because of AE_DONT_WAIT we need to set the timeout
       * to zero */ /* Otherwise we can block */ /* wait forever */ /* note the fe->mask & mask & ... code: maybe an already processed
       * event removed an element that fired and we still didn't
       * processed, so we check if the event is still valid. */ /* Check time events */ /* return the number of processed file/time events */