bundles.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2012, Linus Nielsen Feltzing, <linus@haxx.se>
  9. * Copyright (C) 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at http://curl.haxx.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. ***************************************************************************/
  23. #include "curl_setup.h"
  24. #include <curl/curl.h>
  25. #include "urldata.h"
  26. #include "url.h"
  27. #include "progress.h"
  28. #include "multiif.h"
  29. #include "bundles.h"
  30. #include "sendf.h"
  31. #include "rawstr.h"
  32. #include "curl_memory.h"
  33. /* The last #include file should be: */
  34. #include "memdebug.h"
  35. static void conn_llist_dtor(void *user, void *element)
  36. {
  37. struct connectdata *data = element;
  38. (void)user;
  39. data->bundle = NULL;
  40. }
  41. CURLcode Curl_bundle_create(struct SessionHandle *data,
  42. struct connectbundle **cb_ptr)
  43. {
  44. (void)data;
  45. DEBUGASSERT(*cb_ptr == NULL);
  46. *cb_ptr = malloc(sizeof(struct connectbundle));
  47. if(!*cb_ptr)
  48. return CURLE_OUT_OF_MEMORY;
  49. (*cb_ptr)->num_connections = 0;
  50. (*cb_ptr)->server_supports_pipelining = FALSE;
  51. (*cb_ptr)->conn_list = Curl_llist_alloc((curl_llist_dtor) conn_llist_dtor);
  52. if(!(*cb_ptr)->conn_list) {
  53. Curl_safefree(*cb_ptr);
  54. return CURLE_OUT_OF_MEMORY;
  55. }
  56. return CURLE_OK;
  57. }
  58. void Curl_bundle_destroy(struct connectbundle *cb_ptr)
  59. {
  60. if(!cb_ptr)
  61. return;
  62. if(cb_ptr->conn_list) {
  63. Curl_llist_destroy(cb_ptr->conn_list, NULL);
  64. cb_ptr->conn_list = NULL;
  65. }
  66. Curl_safefree(cb_ptr);
  67. }
  68. /* Add a connection to a bundle */
  69. CURLcode Curl_bundle_add_conn(struct connectbundle *cb_ptr,
  70. struct connectdata *conn)
  71. {
  72. if(!Curl_llist_insert_next(cb_ptr->conn_list, cb_ptr->conn_list->tail, conn))
  73. return CURLE_OUT_OF_MEMORY;
  74. conn->bundle = cb_ptr;
  75. cb_ptr->num_connections++;
  76. return CURLE_OK;
  77. }
  78. /* Remove a connection from a bundle */
  79. int Curl_bundle_remove_conn(struct connectbundle *cb_ptr,
  80. struct connectdata *conn)
  81. {
  82. struct curl_llist_element *curr;
  83. curr = cb_ptr->conn_list->head;
  84. while(curr) {
  85. if(curr->ptr == conn) {
  86. Curl_llist_remove(cb_ptr->conn_list, curr, NULL);
  87. cb_ptr->num_connections--;
  88. conn->bundle = NULL;
  89. return 1; /* we removed a handle */
  90. }
  91. curr = curr->next;
  92. }
  93. return 0;
  94. }