I want to customize my receive function for libcurl to receive large data(may be up to 10K).
My current implementation is as below:
static size_t wt_callback(char *ptr, size_t size, size_t nmb,
void *data) {
int len = size * nmb;
struct response *rsp = (struct response *)data;
int byte = len < rsp->buf_left ? len : rsp->buf_left;
if (rsp->buf_offset == 0) {
memset(rsp->buffer, 0, rsp->buf_left);
}
memcpy(rsp->buffer + rsp->buf_offset, ptr, byte);
rsp->buf_offset += byte;
rsp->buf_left -= byte;
return byte;
}
and structure definition:
struct response {
char *buffer;
int buf_left;
int buf_offset;
};
I install the customized receive function:
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, wt_callback);
/* How data is received */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
/* Where to store incoming data */
response is typed as struct response *.
The current problem is the wt_callback function is not invoked, though I’m sure(I sniff the network with wireshark) that server responsed.
I don’t know where I’m trapped, or any problem with the receive function implementation. Any hint or suggestion will be highly appreciated.
Thanks and Best regards.
If the write callback is not called then the problem can hardly be the callback function itself. As you didn’t show us the rest of the code, it is really hard for us to guess. Perhaps you can find inspiration from an example code with similar functionality that we know works:
http://curl.haxx.se/libcurl/c/getinmemory.html