|
Atrinik Client 2.5
|
#include <global.h>Go to the source code of this file.
Functions | |
| static size_t | curl_callback (void *ptr, size_t size, size_t nmemb, void *data) |
| int | curl_connect (void *c_data) |
| curl_data * | curl_data_new (const char *url) |
| curl_data * | curl_download_start (const char *url) |
| sint8 | curl_download_finished (curl_data *data) |
| void | curl_data_free (curl_data *data) |
| static void | curl_share_lock (CURL *handle, curl_lock_data data, curl_lock_access lock_access, void *userptr) |
| static void | curl_share_unlock (CURL *handle, curl_lock_data data, void *userptr) |
| void | curl_init () |
| void | curl_deinit () |
Variables | |
| static CURLSH * | handle_share = NULL |
| static SDL_mutex * | handle_share_mutex = NULL |
cURL module for downloading data from URLs.
The module uses SDL threads to download data in the background. This makes the API slightly more complicated, but does not freeze the client GUI while the download is in progress.
Common API usage is something like this:
static curl_data *dl_data = NULL; // This would be your function where the user triggers something, for // example, clicks "Download" button or similar. void action_do() { // This is only necessary if you want to give the user a chance to retry, // using for example, a "Retry" button. It may also be necessary if you // do not properly cleanup in your GUI exiting code. if (dl_data) { curl_data_free(dl_data); } // Start downloading; now dl_data will not be NULL anymore, and a new // thread will be created, which will start downloading the data from // the provided URL. dl_data = curl_download_start("http://www.atrinik.org/") } // Here would be your GUI drawing code, as an example. void draw_gui() { // The trigger action to start download could be something like: // if button_pressed(xxx) then action_do() // Here you check for the dl_data; if non-NULL, there is something // being downloaded (or perhaps an error occurred that you can check // for. if (dl_data) { sint8 ret; // This checks the state of the download. ret = curl_download_finished(dl_data); // -1 return value means that some kind of an error occurred; // 404 error, connection timed out, etc. if (ret == -1) { // Here you can either cleanup using curl_data_free(dl_data); dl_data = NULL; // and exit, or show the user that something has gone wrong, and (optionally) // show them a button to retry. } // 0 means the download is still in progress. else if (ret == 0) { // Here you can show the user that download is still in progress with // some text, for example. } // 1 means the download finished. else if (ret == 1) { // What you do here depends on what type of GUI you are creating. For // example, in the case of the metaserver, the downloaded data is parsed // and added to the servers list, and then dl_data is freed and NULLed. // However, cleaning up the dl_data pointer can be left up to exit_gui(), // and here you can just show the raw data to the user, by accessing // dl_data->memory (this can also be used to split the data or whatever). // dl_data->size will contain the number of bytes in dl_data->memory. // Note that dl_data->memory may be NULL, in case no data was downloaded // (empty page). // dl_data->memory is always NUL-terminated (unless, of course, there is // no data, as previously mentioned). } } } // Cleaning up should be done after exiting the GUI, to make sure // downloading process is stopped (if it's running) and cleanup the // structure. void exit_gui() { if (dl_data) { curl_data_free(dl_data); dl_data = NULL; } }
Definition in file curl.c.
| static size_t curl_callback | ( | void * | ptr, |
| size_t | size, | ||
| size_t | nmemb, | ||
| void * | data | ||
| ) | [static] |
Function to call when receiving data from cURL.
| ptr | Pointer to data to process. |
| size | The size of each piece of data. |
| nmemb | Number of data elements. |
| data | User supplied data pointer - points to curl_data that holds the data returned from the url. |
| int curl_connect | ( | void * | c_data | ) |
| void curl_data_free | ( | curl_data * | data | ) |
| curl_data* curl_data_new | ( | const char * | url | ) |
Check if cURL has finished downloading the previously supplied url.
| data | cURL data structure that was returned by a previous curl_download_start() call. |
State of the data:
| curl_data* curl_download_start | ( | const char * | url | ) |
Start downloading an url.
| url | What to download. |
| static void curl_share_lock | ( | CURL * | handle, |
| curl_lock_data | data, | ||
| curl_lock_access | lock_access, | ||
| void * | userptr | ||
| ) | [static] |
| static void curl_share_unlock | ( | CURL * | handle, |
| curl_lock_data | data, | ||
| void * | userptr | ||
| ) | [static] |
CURLSH* handle_share = NULL [static] |
SDL_mutex* handle_share_mutex = NULL [static] |
1.7.4