Jump to content
Sign in to follow this  
AMSPeople

The problem with multithreading in CURL

Recommended Posts

AMSPeople

The curl_multi_info_read function constantly returns 0. Can this be corrected? Thank you.

Global Const $CURLOPTTYPE_LONG = 0
Global Const $CURLOPTTYPE_OBJECTPOINT = 10000
Global Const $CURLOPT_URL = 0x2712
Global Const $CURLOPT_WRITEDATA = 0x2711
Global Const $CURLOPT_WRITEFUNCTION = 0x4E2B
Global Const $CURLOPT_PROGRESSFUNCTION = 0x4E58
Global Const $CURLOPT_NOPROGRESS = 0x2B
Global Const $CURLOPT_ERRORBUFFER = 0x271A
Global Const $CURLOPT_TRANSFERTEXT = 0x35
Global Const $CURL_ERROR_SIZE = 0x100
Global Const $CURLOPT_SSL_VERIFYPEER = 0x40
Global Const $CURLOPT_COOKIEFILE = $CURLOPTTYPE_OBJECTPOINT + 31
Global Const $CURLOPT_COOKIEJAR = $CURLOPTTYPE_OBJECTPOINT + 82
Global Const $CURLOPT_FOLLOWLOCATION = $CURLOPTTYPE_LONG + 52
Global Const $CURLOPT_POSTFIELDS = $CURLOPTTYPE_OBJECTPOINT + 15
Global Const $CURLOPT_POST = $CURLOPTTYPE_LONG + 47
Global Const $CURLOPT_POSTFIELDSIZE = $CURLOPTTYPE_LONG + 60
Global Const $CURLOPT_HTTPHEADER = $CURLOPTTYPE_OBJECTPOINT + 23
Global Const $CURLOPT_VERBOSE = $CURLOPTTYPE_LONG + 41

Global Enum $CURLM_CALL_MULTI_PERFORM = -1, $CURLM_OK, $CURLM_BAD_HANDLE, $CURLM_BAD_EASY_HANDLE, $CURLM_OUT_OF_MEMORY, $CURLM_INTERNAL_ERROR, $CURLM_BAD_SOCKET, $CURLM_UNKNOWN_OPTION, $CURLM_ADDED_ALREADY, $CURLM_LAST

Global $hLibCurl = DllOpen(@ScriptDir & "\libcurl.dll")
Global $aURLs = ['ya.ru', 'google.ru']

Global $hCurlMultiHandle = DllCall($hLibCurl, "ptr:cdecl", "curl_multi_init")[0]
Global $aThreads[UBound($aURLs)]
For $i = 0 To UBound($aURLs) - 1
    $hThread = DllCall($hLibCurl, "ptr:cdecl", "curl_easy_init")[0]
    $aThreads[$i]=$hThread
    $tURL = DllStructCreate("char[2048]")
    DllStructSetData($tURL, 1, $aURLs[$i])

    DllCall($hLibCurl, "int:cdecl", "curl_easy_setopt", "ptr", $hThread, "int", $CURLOPT_URL, "ptr", DllStructGetPtr($tURL))
    $bCallbackBuffer = ''
    $pWriteFunc = DllCallbackRegister ("_my_fwrite", "uint:cdecl", "ptr;uint;uint;ptr")
    DllCall($hLibCurl, "int:cdecl", "curl_easy_setopt", "ptr", $hThread, "int", $CURLOPT_WRITEFUNCTION, "ptr", DllCallbackGetPtr($pWriteFunc))
    DllCall($hLibCurl, "int:cdecl", "curl_easy_setopt", "ptr", $hThread, "int", $CURLOPT_WRITEDATA, "int", 0)
    DllCall($hLibCurl, "int:cdecl", "curl_easy_setopt", "ptr", $hThread, "int", $CURLOPT_FOLLOWLOCATION, "int", 1)
    DllCall($hLibCurl, "int:cdecl", "curl_easy_setopt", "ptr", $hThread, "int", $CURLOPT_VERBOSE, "int", 1)

    DllCall($hLibCurl, "int:cdecl", "curl_multi_add_handle", "ptr", $hCurlMultiHandle, "ptr", $hThread)
Next

$tActive = DllStructCreate("int")
Do
    $iPerform = DllCall($hLibCurl, "int:cdecl", "curl_multi_perform", "ptr", $hCurlMultiHandle, "ptr", DllStructGetPtr($tActive))[0]
Until DllStructGetData($tActive, 1) > 0

if ($iPerform = $CURLM_OK) Then
    $tMsgsLeft = DllStructCreate("int")
    $pMsg = DllCall($hLibCurl, "ptr:cdecl", "curl_multi_info_read", "ptr", $hCurlMultiHandle, "ptr", DllStructGetPtr($tMsgsLeft))[0]
    ; $pMsg = 0  ?????????????
EndIf

for $i = 0 to UBound($aThreads) - 1
    DllCall($hLibCurl, "int:cdecl", "curl_multi_remove_handle", "ptr", $hCurlMultiHandle, "ptr", $aThreads[$i])
    DllCall($hLibCurl, "none:cdecl", "curl_easy_cleanup", "ptr", $aThreads[$i])
Next

DllCall($hLibCurl, "int:cdecl", "curl_multi_cleanup", "ptr", $hCurlMultiHandle)
DllClose($hLibCurl)
DllCallbackFree ('_my_fwrite')
ConsoleWrite($bCallbackBuffer)

Func _my_fwrite($buffer, $size, $nmemb, $stream)
    Local $vData = DllStructCreate("byte[" & $size * $nmemb & "]", $buffer)
    $bCallbackBuffer &= DllStructGetData($vData, 1)
    Return $size * $nmemb
EndFunc   ;==>_my_fwrite

PS. Tested on AutoIt v3.3.9.22

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×