multi_ntfy.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "urldata.h"
  27. #include "curl_trc.h"
  28. #include "multihandle.h"
  29. #include "multiif.h"
  30. #include "multi_ntfy.h"
  31. /* The last 3 #include files should be in this order */
  32. #include "curl_printf.h"
  33. #include "curl_memory.h"
  34. #include "memdebug.h"
  35. struct mntfy_entry {
  36. unsigned int mid;
  37. unsigned int type;
  38. };
  39. #define CURL_MNTFY_CHUNK_SIZE 128
  40. struct mntfy_chunk {
  41. struct mntfy_chunk *next;
  42. size_t r_offset;
  43. size_t w_offset;
  44. struct mntfy_entry entries[CURL_MNTFY_CHUNK_SIZE];
  45. };
  46. static struct mntfy_chunk *mnfty_chunk_create(void)
  47. {
  48. return calloc(1, sizeof(struct mntfy_chunk));
  49. }
  50. static void mnfty_chunk_destroy(struct mntfy_chunk *chunk)
  51. {
  52. free(chunk);
  53. }
  54. static void mnfty_chunk_reset(struct mntfy_chunk *chunk)
  55. {
  56. memset(chunk, 0, sizeof(*chunk));
  57. }
  58. static bool mntfy_chunk_append(struct mntfy_chunk *chunk,
  59. struct Curl_easy *data,
  60. unsigned int type)
  61. {
  62. struct mntfy_entry *e;
  63. if(chunk->w_offset >= CURL_MNTFY_CHUNK_SIZE)
  64. return FALSE;
  65. e = &chunk->entries[chunk->w_offset++];
  66. e->mid = data->mid;
  67. e->type = type;
  68. return TRUE;
  69. }
  70. static struct mntfy_chunk *mntfy_non_full_tail(struct curl_multi_ntfy *mntfy)
  71. {
  72. struct mntfy_chunk *chunk;
  73. if(!mntfy->tail) {
  74. chunk = mnfty_chunk_create();
  75. if(!chunk)
  76. return NULL;
  77. DEBUGASSERT(!mntfy->head);
  78. mntfy->head = mntfy->tail = chunk;
  79. return chunk;
  80. }
  81. else if(mntfy->tail->w_offset < CURL_MNTFY_CHUNK_SIZE)
  82. return mntfy->tail;
  83. else { /* tail is full. */
  84. chunk = mnfty_chunk_create();
  85. if(!chunk)
  86. return NULL;
  87. DEBUGASSERT(mntfy->head);
  88. mntfy->tail->next = chunk;
  89. mntfy->tail = chunk;
  90. return chunk;
  91. }
  92. }
  93. static void mntfy_chunk_dispatch_all(struct Curl_multi *multi,
  94. struct mntfy_chunk *chunk)
  95. {
  96. struct mntfy_entry *e;
  97. struct Curl_easy *data;
  98. if(multi->ntfy.ntfy_cb) {
  99. while((chunk->r_offset < chunk->w_offset) && !multi->ntfy.failure) {
  100. e = &chunk->entries[chunk->r_offset];
  101. data = e->mid ? Curl_multi_get_easy(multi, e->mid) : multi->admin;
  102. /* only when notification has not been disabled in the meantime */
  103. if(data && Curl_uint_bset_contains(&multi->ntfy.enabled, e->type)) {
  104. /* this may cause new notifications to be added! */
  105. CURL_TRC_M(multi->admin, "[NTFY] dispatch %d to xfer %u",
  106. e->type, e->mid);
  107. multi->ntfy.ntfy_cb(multi, e->type, data, multi->ntfy.ntfy_cb_data);
  108. }
  109. /* once dispatched, safe to increment */
  110. chunk->r_offset++;
  111. }
  112. }
  113. mnfty_chunk_reset(chunk);
  114. }
  115. void Curl_mntfy_init(struct Curl_multi *multi)
  116. {
  117. memset(&multi->ntfy, 0, sizeof(multi->ntfy));
  118. Curl_uint_bset_init(&multi->ntfy.enabled);
  119. }
  120. CURLMcode Curl_mntfy_resize(struct Curl_multi *multi)
  121. {
  122. if(Curl_uint_bset_resize(&multi->ntfy.enabled, CURLMNOTIFY_EASY_DONE + 1))
  123. return CURLM_OUT_OF_MEMORY;
  124. return CURLM_OK;
  125. }
  126. void Curl_mntfy_cleanup(struct Curl_multi *multi)
  127. {
  128. while(multi->ntfy.head) {
  129. struct mntfy_chunk *chunk = multi->ntfy.head;
  130. multi->ntfy.head = chunk->next;
  131. mnfty_chunk_destroy(chunk);
  132. }
  133. multi->ntfy.tail = NULL;
  134. Curl_uint_bset_destroy(&multi->ntfy.enabled);
  135. }
  136. CURLMcode Curl_mntfy_enable(struct Curl_multi *multi, unsigned int type)
  137. {
  138. if(type > CURLMNOTIFY_EASY_DONE)
  139. return CURLM_UNKNOWN_OPTION;
  140. Curl_uint_bset_add(&multi->ntfy.enabled, type);
  141. return CURLM_OK;
  142. }
  143. CURLMcode Curl_mntfy_disable(struct Curl_multi *multi, unsigned int type)
  144. {
  145. if(type > CURLMNOTIFY_EASY_DONE)
  146. return CURLM_UNKNOWN_OPTION;
  147. Curl_uint_bset_remove(&multi->ntfy.enabled, type);
  148. return CURLM_OK;
  149. }
  150. void Curl_mntfy_add(struct Curl_easy *data, unsigned int type)
  151. {
  152. struct Curl_multi *multi = data ? data->multi : NULL;
  153. if(multi && multi->ntfy.ntfy_cb && !multi->ntfy.failure &&
  154. Curl_uint_bset_contains(&multi->ntfy.enabled, type)) {
  155. /* append to list of outstanding notifications */
  156. struct mntfy_chunk *tail = mntfy_non_full_tail(&multi->ntfy);
  157. CURL_TRC_M(data, "[NTFY] add %d for xfer %u", type, data->mid);
  158. if(tail)
  159. mntfy_chunk_append(tail, data, type);
  160. else
  161. multi->ntfy.failure = CURLM_OUT_OF_MEMORY;
  162. }
  163. }
  164. CURLMcode Curl_mntfy_dispatch_all(struct Curl_multi *multi)
  165. {
  166. DEBUGASSERT(!multi->in_ntfy_callback);
  167. multi->in_ntfy_callback = TRUE;
  168. while(multi->ntfy.head && !multi->ntfy.failure) {
  169. struct mntfy_chunk *chunk = multi->ntfy.head;
  170. /* this may cause new notifications to be added! */
  171. mntfy_chunk_dispatch_all(multi, chunk);
  172. DEBUGASSERT(chunk->r_offset == chunk->w_offset);
  173. if(chunk == multi->ntfy.tail) /* last one, keep */
  174. break;
  175. DEBUGASSERT(chunk->next);
  176. DEBUGASSERT(multi->ntfy.head != multi->ntfy.tail);
  177. multi->ntfy.head = chunk->next;
  178. mnfty_chunk_destroy(chunk);
  179. }
  180. multi->in_ntfy_callback = FALSE;
  181. if(multi->ntfy.failure) {
  182. CURLMcode result = multi->ntfy.failure;
  183. multi->ntfy.failure = CURLM_OK; /* reset, once delivered */
  184. return result;
  185. }
  186. return CURLM_OK;
  187. }