diff options
author | Yurii Nakonechnyi <inobelar@gmail.com> | 2023-11-23 18:05:26 +0200 |
---|---|---|
committer | Andy Green <andy@warmcat.com> | 2023-11-24 05:48:24 +0000 |
commit | cd6ce19525d0065c5a83622656629fbb1ab53c15 (patch) | |
tree | 015cb36a9fa6bb14d078e5f116d85b269fb7badd | |
parent | 4af988600f12b93373979935d66aee29388d9c34 (diff) | |
download | libwebsockets-cd6ce19525d0065c5a83622656629fbb1ab53c15.tar.gz |
Subject: sul: Update README.lws_sul.md
Described `lws_container_of(...)` usage, added note about `lws_sorted_usec_list_t` zeroing.
-rw-r--r-- | READMEs/README.lws_sul.md | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/READMEs/README.lws_sul.md b/READMEs/README.lws_sul.md index f1cae4e2..234bc8e3 100644 --- a/READMEs/README.lws_sul.md +++ b/READMEs/README.lws_sul.md @@ -30,33 +30,44 @@ scheduler from your own code; it uses it to spread out connection attempts so they are staggered in time. You must create an `lws_sorted_usec_list_t` object somewhere, eg, in you own existing object. -``` +```c static lws_sorted_usec_list_t sul_stagger; ``` Create your own callback for the event... the argument points to the sul object used when the callback was scheduled. You can use pointer arithmetic to translate -that to your own struct when the `lws_sorted_usec_list_t` was a member of the -same struct. +that to your own struct (when the `lws_sorted_usec_list_t` was a member of the +some struct) by using `lws_container_of(sul, container_struct_type, field_name)`. + +```c +typedef struct my_connection_data { + ... + lws_sorted_usec_list_t sul_stagger; + ... +} my_connection_data_t; -``` static void stagger_cb(lws_sorted_usec_list_t *sul) { -... + my_connection_data_t* my_data = lws_container_of(sul, my_connection_data_t, sul_stagger); + ... } ``` +**Important note**: make sure, that `lws_sorted_usec_list_t` data initiallized by +zeros (`memset(&sul_stagger, 0, sizeof(lws_sorted_usec_list_t)`). This struct +contains pointers, so them must initially pointing to `NULL`! + When you want to schedule the callback, use `lws_sul_schedule()`... this will call it 10ms in the future -``` +```c lws_sul_schedule(context, 0, &sul_stagger, stagger_cb, 10 * LWS_US_PER_MS); ``` In the case you destroy your object and need to cancel the scheduled callback, use -``` +```c lws_sul_schedule(context, 0, &sul_stagger, NULL, LWS_SET_TIMER_USEC_CANCEL); ``` |