keep_alive.h 3.1 KB

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