libcurl CURLOPT_WRITEFUNCTION注意事项
来源:互联网 发布:php二分查找算法 编辑:程序博客网 时间:2024/06/08 12:31
NAME
CURLOPT_WRITEFUNCTION - set callback for writing received data
SYNOPSIS
#include <curl/curl.h> size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata); CURLcode curl_easy_setopt(CURL *handle, CURLOPT_WRITEFUNCTION, write_callback);
DESCRIPTION
Pass a pointer to your callback function, which should match the prototype shown above.
This callback function gets called by libcurl as soon as there is data received that needs to be saved. ptr points to the delivered data, and the size of that data is size multiplied with nmemb.
The callback function will be passed as much data as possible in all invokes, but you must not make any assumptions. It may be one byte, it may be thousands. The maximum amount of body data that will be passed to the write callback is defined in the curl.h header file: CURL_MAX_WRITE_SIZE (the usual default is 16K). IfCURLOPT_HEADER is enabled, which makes header data get passed to the write callback, you can get up toCURL_MAX_HTTP_HEADER bytes of header data passed into it. This usually means 100K.
This function may be called with zero bytes data if the transferred file is empty.
The data passed to this function will not be zero terminated!
Set the userdata argument with the CURLOPT_WRITEDATA option.
Your callback should return the number of bytes actually taken care of. If that amount differs from the amount passed to your callback function, it'll signal an error condition to the library. This will cause the transfer to get aborted and the libcurl function used will return CURLE_WRITE_ERROR.
If your callback function returns CURL_WRITEFUNC_PAUSE it will cause this transfer to become paused. Seecurl_easy_pause for further details.
Set this option to NULL to get the internal default function used instead of your callback. The internal default function will write the data to the FILE * given with CURLOPT_WRITEDATA.
遇到的坑:
申请了一块buffer用于接收FTP下载的数据,在缓存不足以接收传递到write_callback的数据大小时,只是拷贝了其中的一部分,并返回实际拷贝的数据长度,结果后续一直没有再接收数据。ftp_ctx->current_download_len= 3181375488, recv_buffer_len=16384, end-last=4096, last=0x7f51c7d41010
其中,recv_buffer_len就是接收到的数据大小, end-last=4096就是剩余的缓存空间的大小。
对于这种情况,正确的处理是:在接收了一块数据之后,要对剩下的缓存进行判断,如果剩下的缓存小于CURL_MAX_WRITE_SIZE,那么暂停接收数据,转而去处理接收到的数据,处理完成腾出空间后再次接收。
memcpy(ftp_ctx->pRecvBuf->last, buffer, recvlen);
ftp_ctx->pRecvBuf->last += recvlen;
ftp_ctx->current_down_len += recvlen;
remain_len = ftp_ctx->pRecvBuf->end - ftp_ctx->pRecvBuf->last;
if (remain_len < CURL_MAX_WRITE_SIZE)
{
curl_easy_pause(ftp_ctx->pDownCurl, CURLPAUSE_RECV);
curl_easy_pause(ftp_ctx->pUploadCurl, CURLPAUSE_SEND_CONT);
}
return recvlen;
(完)
- libcurl CURLOPT_WRITEFUNCTION注意事项
- libcurl中CURLOPT_WRITEFUNCTION设置回调函数
- libcurl 多线程使用注意事项
- libcurl 多线程使用注意事项
- 安装libcurl 时注意事项
- libcurl 多线程使用注意事项
- 关于使用libcurl的注意事项
- 关于使用libcurl的注意事项
- libcurl 使用的几个注意事项
- libcurl 使用的几个注意事项
- 关于使用libcurl的注意事项
- 关于使用libcurl的注意事项
- 关于使用libcurl的注意事项
- Linux libcurl安装及注意事项
- Linux libcurl安装及注意事项
- curl CURLOPT_WRITEFUNCTION 的参数设置
- IOS中是使用libcurl注意事项
- libcurl 多线程使用注意事项(转载)
- C++Primer(5th)第三章学习总结-vector篇
- 支付宝支付
- 取石子问题
- target XXX do ->end 常见问题
- 浅谈Java设计模式(十九)备忘录模式(Memento)
- libcurl CURLOPT_WRITEFUNCTION注意事项
- Codeforces 225D Snake(状压BFS)
- eclipse远程调试Tomcat方法(测试成功并且说说遇到的坑)
- IOS 开发大牛首选之路
- HDFS的Block数据balancer重分布实战
- JQUERY弹出层 三种弹出效果
- RTMP协议分析
- 杂
- c/c++static关键字