CURL超時處理


一般會設置一個超時時間1S,就是說如果php那邊在1S內沒有返回給urlserver的話就忽略掉該請求,及不阻塞等待返回了,直接處理下面的操作。

現在php那邊有時候會卡,這樣一卡就無法再1S內返回消息給服務器

由於urlserver只是忽略了該連接上的請求消息,並不是斷開了,所以php那邊無法判斷消息是否是正常發成功了還是如何

所以玩家積分消耗了道具沒拿到的兌換問題無法通過php捕獲服務器沒有收到來做。

int connectURL(char * strUrl, char ** strResult)
{
    *strResult = 0;
    if (strUrl == 0)
    {
        return 0;
    }

    CURL * pCurl;
    CURLcode res;

    int nReturn = 0;
    pCurl = curl_easy_init();       // init pCurl
    if (pCurl == NULL)
    {
        return nReturn;
    }
    res = curl_easy_setopt(pCurl, CURLOPT_ERRORBUFFER, errorBuffer);    // set error buffer
    if (res != CURLE_OK)
    {
        goto error_return;
    }
    res = curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 1);    // set time out s
    if (res != CURLE_OK)
    {
        goto error_return;
    }
    res = curl_easy_setopt(pCurl, CURLOPT_URL, strUrl);    // set url
    if (res != CURLE_OK)
    {
        goto error_return;
    }

    res = curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, writer); // set write func
    if (res != CURLE_OK)
    {
        goto error_return;
    }
    p_buffer.current_length = 0;
    if (p_buffer.cstring)
    p_buffer.cstring[0] = 0;
    res = curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &p_buffer); // set result buffer
    if (res != CURLE_OK)
    {
        goto error_return;
    }
    res = curl_easy_perform(pCurl);        // run
    if (res != CURLE_OK)
    {
        goto error_return;
    }
    else
    {
        nReturn = 1;
        *strResult = p_buffer.cstring;
    }

error_return:

    if (nReturn == 0)
    {
        printf("[WARNING][%s][%d][%s]\n", __FUNCTION__, res, errorBuffer);
    }
    curl_easy_cleanup(pCurl);

    return nReturn;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM