当前位置:网站首页>Implementation of synchronization interface of 6 libcurl based on libco
Implementation of synchronization interface of 6 libcurl based on libco
2022-07-19 05:21:00 【HjasnJH】
libcurl:
stay c++ To send http/ https request , I believe many people will use libcurl The library of , The library provides two interfaces
curl_easy_perform Blocking interface
curl_multi_perform Non blocking interface among , Most if it is a client request , It is generally used curl_easy_perform Interface .
By some scenes , such as , There is a service , You need to receive requests from clients , then , Unified by the server like another http The server requests data , Now , The server then uses the blocking interface , It will show that we are stretched .

Our usual practice is to use libcurl Of curl_multi_perform Interface , This interface is implemented by several , Can pass select perhaps poll, But it can't be avoided , In the asynchronous process , We must deal with the mapping of message communication , And maybe your original design , It is the implementation of multithreading through synchronous interface , Now , Whether by a way , Without changing to asynchronous , Without blocking , This requires the use of synergy .
libco Is thread based , therefore , It's best if your system uses other third-party libraries , But you don't want to affect the operation of other third-party libraries , At this time, you need to start the collaboration only in your thread , in other words , Only your thread can take hook system call , such , Other network libraries or third-party libraries of your system IO operation , Will not be affected in any way .

libco You can start in each thread , Now I write a simple test program
int main(int argc, char* argv[])
{
curl_global_init(CURL_GLOBAL_ALL);
ArrayLockFreeQueue<long> vRspList;
ArrayLockFreeQueue<long> vReqList;
std::thread first(TestThread, &vReqList, &vRspList);
int nCnt = 0;
long nReqCount = 0;
while (1)
{
long data;
if (vRspList.try_dequeue(data))
{
printf("GetData from Queue = %d %ld\n", data, GetTickMS());
}
else
{
usleep(1000);
printf("pushData to Queue = %d \n", nReqCount);
vReqList.enqueue(nReqCount++);
}
}
curl_global_cleanup();
return 0;
}among , Two lockless queues are defined , Requests and responses , If you want to see the implementation of lockless queue , In my previous article, I shared the related implementation methods of lockless queue .
Thread started
int EndEventLoop(void* p)
{
return -1;
}
void TestThread(ArrayLockFreeQueue<long>* pReqList, ArrayLockFreeQueue<long>* pRspList)
{
long data = 0;
while (true)
{
if (pReqList->try_dequeue(data))
{
stCoRoutine_t* consumer_routine;
co_create(&consumer_routine, nullptr, Producer, pRspList);
co_resume(consumer_routine);
}
co_eventloop(co_get_epoll_ct(), EndEventLoop, NULL);
}
}Constantly pull data from the queue , I used co_eventloop, But I don't want it to circulate all the time , As a result, I can't get the queue data . So here Tencent libco Provides a return mechanism , I used it
Then there is the realization of producers
void* Producer(void* pArgs)
{
co_enable_hook_sys();
CURL* curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
//curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
// curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com/");
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.88.130/");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
long nStart = GetTickMS();
// printf("perform begin %ld\n", nStart);
res = curl_easy_perform(curl);
if (res != 0)
{
static int nErrCount = 0;
printf("curl_ret = %d %d\n", res, ++nErrCount);
}
// printf("perform end ret=%d, %ld\n", res, GetTickMS() - nStart);
curl_easy_cleanup(curl);
if (pArgs)
{
ArrayLockFreeQueue<long>* pList = (ArrayLockFreeQueue<long>*)pArgs;
static int nSend = 0;
// printf("push data to queue %ld\n", GetTickMS());
pList->enqueue(nSend++);
}
}
return NULL;
}
Here I simply return a number , When actually applied , We can return a result set pointer , This is very flexible , No restrictions .
This code just shares the general logic implementation . Some modifications can be made and applied to the real environment .
边栏推荐
猜你喜欢

How to deal with the mismatch between subtitle files and video files

使用Echars实现水滴状、环形图、分割图、堆叠、组织架构图、地图轮廓等图表

mysql - 索引

Use echars to realize water drop, ring, segmentation, stacking, organization chart, map outline and other charts

Router loopback port experiment

Nacos configuration management

实习项目1-个性化主页配置

基于libco的协程实现6 libcurl的同步接口的实现方案

Data visualization

redis 源码分析3 间断遍历的实现
随机推荐
2.6.2 memory leakage
Rk356x u-boot Institute (command section) 3.4 usage of MEM memory related commands
How to deal with the mismatch between subtitle files and video files
[AI] action recognition using simple neural network -- Based on coco key points
BUUCTF 杂项——二维码
Shell script configures root to login to other hosts without secret
es6新增-数组部分
微信小程序 学习笔记
遍历的方法总结
js 原生对象加属性
基于PaddleOCR解决文本检测训练模型与inference模型预测效果不一致的问题
UML(用例图,类图,对象图,包图)
运维安全要了解的二三事
微信小程序云开发使用方法-1
路由器loopback口实验
Easypoi之excel模板导出
Single arm routing configuration
Data visualization
Interface parameters return encapsulated class result
Class object automatic injection attribute operation tool