Background Combined with the previous Clion C + + framework-optimization framework, the introduction of Boost (iii), continue to optimize the framework! In the project, we often pull third-party resources through get, which introduces class library curl, which is used to pull the third-party repository. Open source Framework code:https://github.com/rtxbc/cplus/tree/master/work configuration using
Cmake_minimum_required (VERSION 3.11.2) project (work) message (STATUS "Start load boost =============================== ========= ") # boost## set a variable control set (boost_min_version" 1.67.0 ") # # Dynamic Find Find_package (BOOST ${boost_min_version} REQUIRED) if (not boost_found) message (fatal_error "FATAL error:boost (Version >=${boost_min_version}) required.\n") endif () Message (Status "Boost_include_dirs: ${boost_include_dirs}") Message (status "boost_libraries: ${boost_library_dirs}" ) Message (STATUS "boost_version: ${boost_version}") # # Header file Include_directories (${boost_include_dirs}) link_ Directories (${boost_library_dirs}) #. Boostmessage (STATUS "End load Boost ========================================") # Compile Google test, Generates LIBTEST.A Static library add_subdirectory (Lib/ext/googletest) #头文件INCLUDE_DIRECTORIES in the current directory (${project_source_dir}/src/ Include ${project_source_dir}lib/ext/googletest/include) #库文件: Libtest.a added to the link path link_directories (${project_source _dir}/lib ${project_source_dir}/lib/ext/googletest/usr/local/opt/curl/lib/) #编译器相关设置Set (cmake_runtime_output_directory "${cmake_current_source_dir}/output/bin") set (LIBRARIES pthread) set (cmake_cxx_ Standard one) Set (Cmake_cxx_compiler "clang++") # Displays the C + + compiler set specified for use (cmake_cxx_flags "-g") # Debug Info Set (cmake_cxx_flags "-wall") # Turn on all warning set (cmake_cxx_flags "-lboost_date_time-mt-d") # boost# Source Record file (glob_recurse SOURCEFILES ${project_source_dir}/src/utility/*.cpp) file (glob_recurse test_sourcefiles ${ Project_source_dir}/src/test/*.cpp) Add_custom_target (cmake-build-debug) add_executable (${PROJECT_NAME} ${PROJECT _source_dir}/src/main/main.cpp ${sourcefiles}) add_executable (Work_test ${test_sourcefiles} ${SOURCEFILES}) target_ Link_libraries (${project_name} gtest ${boost_libraries} curl) target_link_libraries (work_test gtest ${Boost_ LIBRARIES} Curl)
Using the SET header file:
Created by Zhou,baochuan on 18/6/5.//#ifndef work_http_h#define work_http_h#include "Common.h" #include <curl/ Curl.h>namespace Work { class HTTP {public : http (); ~http (); static string get (string url, unsigned retries = 3);} ;} #endif//work_http_h
Note: The retries retry mechanism is added to the Get method. Look at the details in the implementation!
Created by Zhou,baochuan in 18/6/5.//#include "http.h" using namespace work; Http::http () {curl_global_init (curl_global_nothing);} Http::~http () {curl_global_cleanup ();} size_t req_reply (void* ptr, size_t size, size_t nmemb, void* stream) {//cout << "----->reply" << Endl; std::string* str = (std::string*) stream; cout << *str << Endl; (*STR). Append ((char*) PTR, size * nmemb); return size * NMEMB;} String http::get (string url, unsigned int retries) {string response; CURL *curl; struct Curl_slist *headers = NULL; headers = curl_slist_append (headers, "accept:agent-007"); Curl = Curl_easy_init (); if (Curl) {//curl_easy_setopt (curl, Curlopt_proxy, "10.99.60.201:8080");//proxy curl_easy_setopt (curl, Curlop T_httpheader, headers); Curl_easy_setopt (Curl, Curlopt_url, url.c_str ()); Curl_easy_setopt (Curl, curlopt_writefunction, req_reply); Curl_easy_setopt (Curl, Curlopt_writedata, (void*) & response); Curl_easy_setopt (Curl, curlopt_verbose, 1); Curl_easy_setopt (Curl, curlopt_timeout, 0); Transmission Timeout curl_easy_setopt (curl, curlopt_connecttimeout, 0); Connection Timeout curl_easy_setopt (curl, curlopt_nosignal, 1L); Curl_easy_setopt (Curl, curlopt_forbid_reuse, 1); Curlcode res = curl_easy_perform (curl); Execute/Retry while (res! = CURLE_OK &&--retries > 0) {res = Curl_easy_perform (curl); Execute} curl_easy_cleanup (curl); } curl_slist_free_all (headers); return response;}
Test code:
#include "common.h" #include "http.h" #include <gtest/gtest.h>using namespace work;//Curl version requires test (curl, all) { expect_eq (3, curlversion_now); HTTP http; String url = "http://47.95.220.249/"; Assert_false (Http.get (URL). empty ());
CURL Considerations
1. Solve thread safety and avoid core error problem mode
1) curl_global_init () is thread insecure in a multithreaded environment. So in a multithreaded environment, this method is called in the main thread. The Curl_global_cleanup () method is called in the main thread.
2) curl_easy_setopt (curl, curlopt_nosignal, 1L); Controls the timeout for domain name resolution, which requires a global variable of type SIGJMP_BUF, which is modified when multi-threading.
3) curl_easy_setopt (Curl, curlopt_forbid_reuse, 1); By default Libcurl after a task is completed, the consideration for reusing a connection does not close immediately. If there is no new TCP request to reuse this connection, then only wait until the close_wait timeout, the time default is 7,200 seconds or higher, too many close_wait connections can cause performance problems
2, want to let Curl_easy_perform (), can execute, must have a matching method curl_easy_setopt (curl, curlopt_writefunction, req_reply);
Recommended
Clion C + + framework-optimize open source framework, introduce curl, achieve get resources (iv)