cURL / Mailing Lists / curl-library / Single Mail

curl-library

how to keep this work after calling select failed(Bad file descriptor)

From: ÔøÏé°² <wszxayx_at_163.com>
Date: Fri, 11 Oct 2013 15:51:51 +0800 (CST)

Hi all
I have read this(http://curl.haxx.se/libcurl/c/multi-single.html) and carefully,
but I still don't understand how to keep multi interface work again without recreate it when select call failed.

Attached is my code, I call CMultiCurl::Poll in an infinite loop. When I need to post something, I call CMultiCurl::Post.
This work first, but I don't know when and how, select call failed, then nothing goes right.
Please help me out, thanks very much.

___________________________________________________________________________________________
int CMultiCurl::Poll(int timeout/*msec*/)
{
struct timeval rTimeo;
rTimeo.tv_sec = timeout / 1000;
rTimeo.tv_usec = (timeout % 1000) * 1000;

long curlTimeo = -1;
curl_multi_timeout(m_mcurl, &curlTimeo);
if (curlTimeo >= 0 && curlTimeo < timeout)
{
rTimeo.tv_sec = curlTimeo / 1000;
rTimeo.tv_usec = (curlTimeo % 1000) * 1000;
}
//XA_TRACE("multi_curl select timeout: %d, %d", rTimeo.tv_sec, rTimeo.tv_usec);

fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd = -1;
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);

int ret = curl_multi_fdset(m_mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
if (ret != 0)
{
XA_ERROR("%d", ret);
return -1;
}

ret = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &rTimeo);
if (ret == -1)
{
XA_ERROR("%d: %s", ret, strerror(errno));
return -1;
}

int stillR = 0;
while (CURLM_CALL_MULTI_PERFORM
== (ret = curl_multi_perform(m_mcurl, &stillR))) {}
if (ret != 0)
{
XA_ERROR("%d", ret);
}

// check msg
int rest = 0;
CURLMsg *msg = NULL;
while (NULL != (msg = curl_multi_info_read(m_mcurl, &rest)))
{
if (msg->msg == CURLMSG_DONE)
{
MCB *mcb = (MCB *)GetFromMap(msg->easy_handle);
DelFromMap(msg->easy_handle);
XA_ASSERT(mcb != NULL);
if (mcb->ptr != NULL)
{
m_callback(msg->data.result, mcb->ptr, mcb->buf, mcb->used);
delete [] mcb->buf;
}

if (mcb->slist != NULL)
{
curl_slist_free_all(mcb->slist);
}
delete mcb;

curl_multi_remove_handle(m_mcurl, msg->easy_handle);
curl_easy_cleanup(msg->easy_handle);
}
}

return 0;
}

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html

Received on 2013-10-11