… | |
… | |
1874 | callback (EV_P_ ev_timer *w, int revents) |
1874 | callback (EV_P_ ev_timer *w, int revents) |
1875 | { |
1875 | { |
1876 | // calculate when the timeout would happen |
1876 | // calculate when the timeout would happen |
1877 | ev_tstamp after = last_activity - ev_now (EV_A) + timeout; |
1877 | ev_tstamp after = last_activity - ev_now (EV_A) + timeout; |
1878 | |
1878 | |
1879 | // if negative, it means we the timeout already occured |
1879 | // if negative, it means we the timeout already occurred |
1880 | if (after < 0.) |
1880 | if (after < 0.) |
1881 | { |
1881 | { |
1882 | // timeout occurred, take action |
1882 | // timeout occurred, take action |
1883 | } |
1883 | } |
1884 | else |
1884 | else |
… | |
… | |
1902 | |
1902 | |
1903 | Otherwise, we now the earliest time at which the timeout would trigger, |
1903 | Otherwise, we now the earliest time at which the timeout would trigger, |
1904 | and simply start the timer with this timeout value. |
1904 | and simply start the timer with this timeout value. |
1905 | |
1905 | |
1906 | In other words, each time the callback is invoked it will check whether |
1906 | In other words, each time the callback is invoked it will check whether |
1907 | the timeout cocured. If not, it will simply reschedule itself to check |
1907 | the timeout occurred. If not, it will simply reschedule itself to check |
1908 | again at the earliest time it could time out. Rinse. Repeat. |
1908 | again at the earliest time it could time out. Rinse. Repeat. |
1909 | |
1909 | |
1910 | This scheme causes more callback invocations (about one every 60 seconds |
1910 | This scheme causes more callback invocations (about one every 60 seconds |
1911 | minus half the average time between activity), but virtually no calls to |
1911 | minus half the average time between activity), but virtually no calls to |
1912 | libev to change the timeout. |
1912 | libev to change the timeout. |
… | |
… | |
1926 | if (activity detected) |
1926 | if (activity detected) |
1927 | last_activity = ev_now (EV_A); |
1927 | last_activity = ev_now (EV_A); |
1928 | |
1928 | |
1929 | When your timeout value changes, then the timeout can be changed by simply |
1929 | When your timeout value changes, then the timeout can be changed by simply |
1930 | providing a new value, stopping the timer and calling the callback, which |
1930 | providing a new value, stopping the timer and calling the callback, which |
1931 | will agaion do the right thing (for example, time out immediately :). |
1931 | will again do the right thing (for example, time out immediately :). |
1932 | |
1932 | |
1933 | timeout = new_value; |
1933 | timeout = new_value; |
1934 | ev_timer_stop (EV_A_ &timer); |
1934 | ev_timer_stop (EV_A_ &timer); |
1935 | callback (EV_A_ &timer, 0); |
1935 | callback (EV_A_ &timer, 0); |
1936 | |
1936 | |
… | |
… | |
3921 | |
3921 | |
3922 | ... |
3922 | ... |
3923 | ev_set_syserr_cb (fatal_error); |
3923 | ev_set_syserr_cb (fatal_error); |
3924 | |
3924 | |
3925 | The only API functions that can currently throw exceptions are C<ev_run>, |
3925 | The only API functions that can currently throw exceptions are C<ev_run>, |
3926 | C<ev_inoke>, C<ev_invoke_pending> and C<ev_loop_destroy> (the latter |
3926 | C<ev_invoke>, C<ev_invoke_pending> and C<ev_loop_destroy> (the latter |
3927 | because it runs cleanup watchers). |
3927 | because it runs cleanup watchers). |
3928 | |
3928 | |
3929 | Throwing exceptions in watcher callbacks is only supported if libev itself |
3929 | Throwing exceptions in watcher callbacks is only supported if libev itself |
3930 | is compiled with a C++ compiler or your C and C++ environments allow |
3930 | is compiled with a C++ compiler or your C and C++ environments allow |
3931 | throwing exceptions through C libraries (most do). |
3931 | throwing exceptions through C libraries (most do). |