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;

(完)

1 0
原创粉丝点击