1 /* Keep Alive engine for wss server example
2 
3    This example code is in the Public Domain (or CC0 licensed, at your option.)
4 
5    Unless required by applicable law or agreed to in writing, this
6    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7    CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #pragma once
10 
11 #define KEEP_ALIVE_CONFIG_DEFAULT() \
12     { \
13     .max_clients = 10,                      \
14     .task_stack_size = 2048,                \
15     .task_prio = tskIDLE_PRIORITY+1,        \
16     .keep_alive_period_ms = 5000,           \
17     .not_alive_after_ms = 10000,            \
18 }
19 
20 struct wss_keep_alive_storage;
21 typedef struct wss_keep_alive_storage* wss_keep_alive_t;
22 typedef bool (*wss_check_client_alive_cb_t)(wss_keep_alive_t h, int fd);
23 typedef bool (*wss_client_not_alive_cb_t)(wss_keep_alive_t h, int fd);
24 
25 /**
26  * @brief Confiuration struct
27  */
28 typedef struct {
29     size_t max_clients;                                      /*!< max number of clients */
30     size_t task_stack_size;                                  /*!< stack size of the created task */
31     size_t task_prio;                                        /*!< priority of the created task */
32     size_t keep_alive_period_ms;                             /*!< check every client after this time */
33     size_t not_alive_after_ms;                               /*!< consider client not alive after this time */
34     wss_check_client_alive_cb_t check_client_alive_cb;       /*!< callback function to check if client is alive */
35     wss_client_not_alive_cb_t client_not_alive_cb;           /*!< callback function to notify that the client is not alive */
36     void *user_ctx;                                          /*!< user context available in the keep-alive handle */
37 } wss_keep_alive_config_t;
38 
39 /**
40  * @brief Adds a new client to internal set of clients to keep an eye on
41  *
42  * @param h keep-alive handle
43  * @param fd socket file descriptor for this client
44  * @return ESP_OK on success
45  */
46 esp_err_t wss_keep_alive_add_client(wss_keep_alive_t h, int fd);
47 
48 /**
49  * @brief Removes this client from the set
50  *
51  * @param h keep-alive handle
52  * @param fd socket file descriptor for this client
53  * @return ESP_OK on success
54  */
55 esp_err_t wss_keep_alive_remove_client(wss_keep_alive_t h, int fd);
56 
57 /**
58  * @brief Notify that this client is alive
59  *
60  * @param h keep-alive handle
61  * @param fd socket file descriptor for this client
62  * @return ESP_OK on success
63  */
64 
65 esp_err_t wss_keep_alive_client_is_active(wss_keep_alive_t h, int fd);
66 
67 /**
68  * @brief Starts keep-alive engine
69  *
70  * @param config keep-alive configuration
71  * @return keep alive handle
72  */
73 wss_keep_alive_t wss_keep_alive_start(wss_keep_alive_config_t *config);
74 
75 /**
76  * @brief Stops keep-alive engine
77  *
78  * @param h keep-alive handle
79  */
80 void wss_keep_alive_stop(wss_keep_alive_t h);
81 
82 /**
83  * @brief Sets user defined context
84  *
85  * @param h keep-alive handle
86  * @param ctx user context
87  */
88 void wss_keep_alive_set_user_ctx(wss_keep_alive_t h, void *ctx);
89 
90 /**
91  * @brief Gets user defined context
92  *
93  * @param h keep-alive handle
94  * @return ctx user context
95  */
96 void* wss_keep_alive_get_user_ctx(wss_keep_alive_t h);
97