CURL超時處理

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

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

因爲urlserver只是忽略了該鏈接上的請求消息,並非斷開了,因此php那邊沒法判斷消息是不是正常發成功了仍是如何curl

因此玩家積分消耗了道具沒拿到的兌換問題沒法經過php捕獲服務器沒有收到來作。url

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;
}