Atrinik Client 2.5
Functions | Variables
client/curl.c File Reference
#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_datacurl_data_new (const char *url)
curl_datacurl_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

Detailed Description

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;
    }
}
Author:
Alex Tokar

Definition in file curl.c.


Function Documentation

static size_t curl_callback ( void *  ptr,
size_t  size,
size_t  nmemb,
void *  data 
) [static]

Function to call when receiving data from cURL.

Parameters:
ptrPointer to data to process.
sizeThe size of each piece of data.
nmembNumber of data elements.
dataUser supplied data pointer - points to curl_data that holds the data returned from the url.
Returns:
Number of bytes processed.

Definition at line 135 of file curl.c.

int curl_connect ( void *  c_data)

Use cURL to download the url we specified in curl_data structure (c_data).

Parameters:
c_datacurl_data structure that will receive the data (and has url of what to download).
Returns:
-1 on failure, 1 on success.

Definition at line 161 of file curl.c.

void curl_data_free ( curl_data data)

Frees previously created curl_data structure.

Parameters:
dataWhat to free.

Definition at line 305 of file curl.c.

curl_data* curl_data_new ( const char *  url)

Initialize new curl_data structure.

Parameters:
urlUrl to connect to.
Returns:
The new structure.

Definition at line 248 of file curl.c.

void curl_deinit ( )

Deinitialize cURL module.

Definition at line 358 of file curl.c.

sint8 curl_download_finished ( curl_data data)

Check if cURL has finished downloading the previously supplied url.

Parameters:
datacURL data structure that was returned by a previous curl_download_start() call.
Returns:

State of the data:

  • 0: still trying to get data (connecting to server, getting data, etc). While this is the state, no members of this structure should be accessed from the outside (at the very least not without mutex locking).
  • -1: An error occurred trying to get the data.
  • 1: cURL thread finished and the data is ready to be used.

Definition at line 291 of file curl.c.

curl_data* curl_download_start ( const char *  url)

Start downloading an url.

Parameters:
urlWhat to download.
Returns:
cURL data structure. You should store this somehow, and the next tick see if it has finished by using curl_download_finished(). If so, you can access its members such as curl_data::memory. Do not forget to clean up with curl_data_free() at some point (even if an error occurred).

Definition at line 271 of file curl.c.

void curl_init ( )

Initialize cURL module.

Definition at line 344 of file curl.c.

static void curl_share_lock ( CURL *  handle,
curl_lock_data  data,
curl_lock_access  lock_access,
void *  userptr 
) [static]

Lock the share handle.

Definition at line 325 of file curl.c.

static void curl_share_unlock ( CURL *  handle,
curl_lock_data  data,
void *  userptr 
) [static]

Unlock the share handle.

Definition at line 335 of file curl.c.


Variable Documentation

CURLSH* handle_share = NULL [static]

Shared handle.

Definition at line 123 of file curl.c.

SDL_mutex* handle_share_mutex = NULL [static]

Mutex to protect the shared handle.

Definition at line 125 of file curl.c.