Skip to content

Commit

Permalink
Merge branch 'TheReverend403-patch-1'
Browse files Browse the repository at this point in the history
closes #33
  • Loading branch information
hrkfdn committed Apr 30, 2016
2 parents 824a6b4 + c8cd969 commit cd6fadf
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion audioscrobbler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#define APIKEY "a0ed2629d3d28606f67d7214c916788d"
#define SECRET "295f31c5d28215215b1503fb0327cc01"
#define CURL_MAX_RETRIES 3
#define CURL_RETRY_DELAY 3 // Seconds

CAudioScrobbler* AudioScrobbler = 0;

Expand Down Expand Up @@ -44,6 +46,7 @@ void CAudioScrobbler::OpenURL(std::string url, const char* postfields = 0, char*
curl_easy_setopt(_handle, CURLOPT_DNS_CACHE_TIMEOUT, 0);
curl_easy_setopt(_handle, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(_handle, CURLOPT_WRITEFUNCTION, writecb);
curl_easy_setopt(_handle, CURLOPT_TIMEOUT, 10);

if(postfields) {
curl_easy_setopt(_handle, CURLOPT_POST, 1);
Expand All @@ -55,7 +58,27 @@ void CAudioScrobbler::OpenURL(std::string url, const char* postfields = 0, char*
curl_easy_setopt(_handle, CURLOPT_ERRORBUFFER, errbuf);

curl_easy_setopt(_handle, CURLOPT_URL, url.c_str());
curl_easy_perform(_handle);
CURLcode res = curl_easy_perform(_handle);

// Sometimes last.fm likes to just timeout for no reason, leaving us hanging.
// If this happens, retry a few times with a small delay.
if (res != CURLE_OK) {
eprintf("libcurl: (%d)", res);
eprintf("%s", curl_easy_strerror(res));
eprintf("Will retry %d times with a %d second delay.", CURL_MAX_RETRIES, CURL_RETRY_DELAY);

int retries = 0;
do {
sleep(CURL_RETRY_DELAY);
retries++;
eprintf("Retry %d/%d", retries, CURL_MAX_RETRIES);

res = curl_easy_perform(_handle);
if (res != CURLE_OK) {
eprintf("Failed: %s", curl_easy_strerror(res));
}
} while (res != CURLE_OK && retries < CURL_MAX_RETRIES);
}
}


Expand Down

0 comments on commit cd6fadf

Please sign in to comment.