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 h
| 351 | * |
| 352 | * The function returns the number of events processed. */ |
| 353 | int aeProcessEvents(aeEventLoop *eventLoop, int flags) |
| 354 | { |
| 355 | int processed = 0, numevents; |
| 356 | |
| 357 | /* Nothing to do? return ASAP */ |
| 358 | if (!(flags & AE_TIME_EVENTS) && !(flags & AE_FILE_EVENTS)) return 0; |
| 359 | |
| 360 | /* Note that we want to call select() even if there are no |
| 361 | * file events to process as long as we want to process time |
| 362 | * events, in order to sleep until the next time event is ready |
| 363 | * to fire. */ |
| 364 | if (eventLoop->maxfd != -1 || |
| 365 | ((flags & AE_TIME_EVENTS) && !(flags & AE_DONT_WAIT))) { |
| 366 | int j; |
| 367 | struct timeval tv, *tvp; |
| 368 | int64_t usUntilTimer = -1; |
| 369 | |
| 370 | if (flags & AE_TIME_EVENTS && !(flags & AE_DONT_WAIT)) |
| 371 | usUntilTimer = usUntilEarliestTimer(eventLoop); |
| 372 | |
| 373 | if (usUntilTimer >= 0) { |
| 374 | tv.tv_sec = usUntilTimer / 1000000; |
| 375 | tv.tv_usec = usUntilTimer % 1000000; |
| 376 | tvp = &tv; |
| 377 | } else { |
| 378 | /* If we have to check for events but need to return |
| 379 | * ASAP because of AE_DONT_WAIT we need to set the timeout |
| 380 | * to zero */ |
| 381 | if (flags & AE_DONT_WAIT) { |
| 382 | tv.tv_sec = tv.tv_usec = 0; |
| 383 | tvp = &tv; |
| 384 | } else { |
| 385 | /* Otherwise we can block */ |
| 386 | tvp = NULL; /* wait forever */ |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if (eventLoop->flags & AE_DONT_WAIT) { |
| 391 | tv.tv_sec = tv.tv_usec = 0; |
| 392 | tvp = &tv; |
| 393 | } |
| 394 | |
| 395 | if (eventLoop->beforesleep != NULL && flags & AE_CALL_BEFORE_SLEEP) |
| 396 | eventLoop->beforesleep(eventLoop); |
| 397 | |
| 398 | /* Call the multiplexing API, will return only on timeout or when |
| 399 | * some event fires. */ |
| 400 | numevents = aeApiPoll(eventLoop, tvp); |
| 401 | |
| 402 | /* After sleep callback. */ |
| 403 | if (eventLoop->aftersleep != NULL && flags & AE_CALL_AFTER_SLEEP) |
| 404 | eventLoop->aftersleep(eventLoop); |
| 405 | |
| 406 | for (j = 0; j < numevents; j++) { |
| 407 | aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd]; |
| 408 | int mask = eventLoop->fired[j].mask; |
| 409 | int fd = eventLoop->fired[j].fd; |
| 410 | int fired = 0; /* Number of events fired for current fd. */ |
no test coverage detected