… | |
… | |
1006 | { |
1006 | { |
1007 | struct ev_io io; |
1007 | struct ev_io io; |
1008 | int otherfd; |
1008 | int otherfd; |
1009 | void *somedata; |
1009 | void *somedata; |
1010 | struct whatever *mostinteresting; |
1010 | struct whatever *mostinteresting; |
1011 | } |
1011 | }; |
|
|
1012 | |
|
|
1013 | ... |
|
|
1014 | struct my_io w; |
|
|
1015 | ev_io_init (&w.io, my_cb, fd, EV_READ); |
1012 | |
1016 | |
1013 | And since your callback will be called with a pointer to the watcher, you |
1017 | And since your callback will be called with a pointer to the watcher, you |
1014 | can cast it back to your own type: |
1018 | can cast it back to your own type: |
1015 | |
1019 | |
1016 | static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents) |
1020 | static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents) |
… | |
… | |
1020 | } |
1024 | } |
1021 | |
1025 | |
1022 | More interesting and less C-conformant ways of casting your callback type |
1026 | More interesting and less C-conformant ways of casting your callback type |
1023 | instead have been omitted. |
1027 | instead have been omitted. |
1024 | |
1028 | |
1025 | Another common scenario is having some data structure with multiple |
1029 | Another common scenario is to use some data structure with multiple |
1026 | watchers: |
1030 | embedded watchers: |
1027 | |
1031 | |
1028 | struct my_biggy |
1032 | struct my_biggy |
1029 | { |
1033 | { |
1030 | int some_data; |
1034 | int some_data; |
1031 | ev_timer t1; |
1035 | ev_timer t1; |
1032 | ev_timer t2; |
1036 | ev_timer t2; |
1033 | } |
1037 | } |
1034 | |
1038 | |
1035 | In this case getting the pointer to C<my_biggy> is a bit more complicated, |
1039 | In this case getting the pointer to C<my_biggy> is a bit more |
1036 | you need to use C<offsetof>: |
1040 | complicated: Either you store the address of your C<my_biggy> struct |
|
|
1041 | in the C<data> member of the watcher, or you need to use some pointer |
|
|
1042 | arithmetic using C<offsetof> inside your watchers: |
1037 | |
1043 | |
1038 | #include <stddef.h> |
1044 | #include <stddef.h> |
1039 | |
1045 | |
1040 | static void |
1046 | static void |
1041 | t1_cb (EV_P_ struct ev_timer *w, int revents) |
1047 | t1_cb (EV_P_ struct ev_timer *w, int revents) |
… | |
… | |
1229 | you suspect event processing to be delayed and you I<need> to base the |
1235 | you suspect event processing to be delayed and you I<need> to base the |
1230 | timeout on the current time, use something like this to adjust for this: |
1236 | timeout on the current time, use something like this to adjust for this: |
1231 | |
1237 | |
1232 | ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); |
1238 | ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); |
1233 | |
1239 | |
1234 | If the event loop is suspended for a long time, one can also force an |
1240 | If the event loop is suspended for a long time, you can also force an |
1235 | update of the time returned by C<ev_now ()> by calling C<ev_now_update |
1241 | update of the time returned by C<ev_now ()> by calling C<ev_now_update |
1236 | ()>. |
1242 | ()>. |
1237 | |
1243 | |
1238 | =head3 Watcher-Specific Functions and Data Members |
1244 | =head3 Watcher-Specific Functions and Data Members |
1239 | |
1245 | |