2017-11-02 06:37:45 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2017-11-02 06:37:45 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2017-11-02 06:37:45 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2017-11-02 06:37:45 +08:00
|
|
|
***************************************************************************/
|
|
|
|
/* <DESC>
|
2018-03-16 10:51:03 +08:00
|
|
|
* Connection cache shared between easy handles with the share interface
|
2017-11-02 06:37:45 +08:00
|
|
|
* </DESC>
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2017-11-05 00:23:32 +08:00
|
|
|
static void my_lock(CURL *handle, curl_lock_data data,
|
|
|
|
curl_lock_access laccess, void *useptr)
|
|
|
|
{
|
|
|
|
(void)handle;
|
|
|
|
(void)data;
|
|
|
|
(void)laccess;
|
|
|
|
(void)useptr;
|
|
|
|
fprintf(stderr, "-> Mutex lock\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void my_unlock(CURL *handle, curl_lock_data data, void *useptr)
|
|
|
|
{
|
|
|
|
(void)handle;
|
|
|
|
(void)data;
|
|
|
|
(void)useptr;
|
|
|
|
fprintf(stderr, "<- Mutex unlock\n");
|
|
|
|
}
|
|
|
|
|
2017-11-02 06:37:45 +08:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
CURLSH *share;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
share = curl_share_init();
|
|
|
|
curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
|
|
|
|
|
2017-11-05 00:23:32 +08:00
|
|
|
curl_share_setopt(share, CURLSHOPT_LOCKFUNC, my_lock);
|
|
|
|
curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, my_unlock);
|
|
|
|
|
2017-11-02 06:37:45 +08:00
|
|
|
/* Loop the transfer and cleanup the handle properly every lap. This will
|
|
|
|
still reuse connections since the pool is in the shared object! */
|
|
|
|
|
|
|
|
for(i = 0; i < 3; i++) {
|
2019-05-21 16:44:16 +08:00
|
|
|
CURL *curl = curl_easy_init();
|
2017-11-02 06:37:45 +08:00
|
|
|
if(curl) {
|
2019-05-21 16:44:16 +08:00
|
|
|
CURLcode res;
|
|
|
|
|
2020-11-04 21:02:01 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, "https://curl.se/");
|
2017-11-02 06:37:45 +08:00
|
|
|
|
2017-11-05 00:23:32 +08:00
|
|
|
/* use the share object */
|
2017-11-02 06:37:45 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_SHARE, share);
|
|
|
|
|
|
|
|
/* Perform the request, res will get the return code */
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
/* Check for errors */
|
|
|
|
if(res != CURLE_OK)
|
|
|
|
fprintf(stderr, "curl_easy_perform() failed: %s\n",
|
|
|
|
curl_easy_strerror(res));
|
|
|
|
|
|
|
|
/* always cleanup */
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_share_cleanup(share);
|
|
|
|
return 0;
|
|
|
|
}
|