bash;collapse:true;;gutter:true;
cmake_minimum_required(VERSION 3.11.2)</p>
<p>project(work)</p>
<p>message(STATUS "start load boost ========================================")
BOOST</p>
<h2>设置个变量控制</h2>
<p>SET(BOOST_MIN_VERSION "1.67.0")</p>
<h2>动态查找</h2>
<p>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}")</p>
<h2>头文件</h2>
<p>INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
.BOOST
message(STATUS "end load boost ========================================")</p>
<p>编译google test,会在当前目录生成libtest.a静态库
add_subdirectory(lib/ext/googletest)</p>
<h1>头文件</h1>
<p>INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/src/include ${PROJECT_SOURCE_DIR}lib/ext/googletest/include)</p>
<h1>库文件 : libtest.a 添加到链接路径中</h1>
<p>link_directories(${PROJECT_SOURCE_DIR}/lib ${PROJECT_SOURCE_DIR}/lib/ext/googletest /usr/local/opt/curl/lib/)</p>
<h1>编译器相关设置</h1>
<p>set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/output/bin")
set(LIBRARIES pthread)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_COMPILER "clang++" ) # 显示指定使用的C++编译器
set(CMAKE_CXX_FLAGS "-g") # 调试信息
set(CMAKE_CXX_FLAGS "-Wall") # 开启所有警告
set(CMAKE_CXX_FLAGS "-lboost_date_time-mt-d") # boost</p>
<h1>源码目录</h1>
<p>FILE(GLOB_RECURSE SOURCEFILES ${PROJECT_SOURCE_DIR}/src/utility/<em>.cpp)
FILE(GLOB_RECURSE TEST_SOURCEFILES ${PROJECT_SOURCE_DIR}/src/test/</em>.cpp)</p>
<p>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})</p>
<p>target_link_libraries(${PROJECT_NAME} gtest ${Boost_LIBRARIES} curl)
target_link_libraries(work_test gtest ${Boost_LIBRARIES} curl)</p>
<pre><code>
使用
设定头部文件:
![CLion之C++框架篇-优化开源框架,引入curl,实现get方式获取资源(四)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/152332-20180606180615277-1867635669.png)
;collapse:true;;gutter:true;
//
// Created by Zhou,Baochuan on 18/6/5.
//
#ifndef WORK_HTTP_H
#define WORK_HTTP_H
#include "common.h"
#include
namespace work {
class Http {
public:
Http();
~Http();
static string get(string url, unsigned retries = 3);
};
}
#endif //WORK_HTTP_H
注意:get方法中增加了retries重试机制。在实现中看一下细节!
cpp;collapse:true;;gutter:true;
//
// Created by Zhou,Baochuan on 18/6/5.</p>
<p>//</p>
<h1>include "http.h"</h1>
<p>using namespace work;</p>
<p>Http::Http()
{
curl_global_init(CURL_GLOBAL_NOTHING);
}</p>
<p>Http::~Http()
{
curl_global_cleanup();
}</p>
<p>size_t req_reply(void<em> ptr, size_t size, size_t nmemb, void</em> stream) {
//cout << "----->reply" << endl;
std::string<em> str = (std::string</em>)stream;
//cout << <em>str << endl;
(</em>str).append((char<em>)ptr, size * nmemb);
return size * nmemb;
}
string Http::get(string url, unsigned int retries)
{
string response;
CURL </em>curl;
struct curl_slist *headers = NULL;
//headers = curl_slist_append(headers, "Accept: Agent-007");</p>
<pre><code>curl = curl_easy_init() ;
if (curl) {
//curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080");// 代理
curl_easy_setopt(curl, CURLOPT_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); // 传输超时
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 0); // 连接超时
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1);
CURLcode res = curl_easy_perform(curl); // 执行
// 重试
while (res != CURLE_OK && --retries > 0) {
res = curl_easy_perform(curl); // 执行
}
curl_easy_cleanup(curl);
}
curl_slist_free_all(headers);
return response;
</code></pre>
<p>}</p>
<pre><code>
测试代码:
![CLion之C++框架篇-优化开源框架,引入curl,实现get方式获取资源(四)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230526/152332-20180606181026310-259292454.png)
;collapse:true;;gutter:true;
#include "common.h"
#include "http.h"
#include
using namespace work;
// curl版本要求
TEST(curl, all)
{
EXPECT_EQ(3, CURLVERSION_NOW);
Http http;
string url = "http://47.95.220.249/";
ASSERT_FALSE(http.get(url).empty());
}
CURL 注意事项
1、解决线程安全及避免core错误问题方式
1) curl_global_init()在多线程环境下,是线程不安全的。所以在多线程环境下,要在主线程中调用这个方法。配套的,在主线程中调用curl_global_cleanup()方法。
2)curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); 控制域名解析的超时,其需要一个sigjmp_buf型的全局变量,多线程时会修改它。
3) curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1);默认情况下libcurl完成一个任务以后,出于重用连接的考虑不会马上关闭。如果没有新的TCP请求来重用这个连接,那么只能等到CLOSE_WAIT超时,这个时间默认在7200秒甚至更高,太多的CLOSE_WAIT连接会导致性能问题
2、要想让curl_easy_perform(),能够执行,必须得有个配套方法curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);
Original: https://www.cnblogs.com/baochuan/p/9141712.html
Author: 川山甲
Title: CLion之C++框架篇-优化开源框架,引入curl,实现get方式获取资源(四)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/535590/
转载文章受原作者版权保护。转载请注明原作者出处!