Bennu Game Development

English Forums => Extensions => Topic started by: josebita on November 25, 2011, 12:15:39 AM

Title: libcurl bindings [File downloading, server-client communications]
Post by: josebita on November 25, 2011, 12:15:39 AM
I have created a very small libcurl binding that should be useful for those wanting to access online resources from BennuGD.
Right now it only supports one download at a time but that limitation will be lifted.


Here you can see a small video of the library downloading the main BennuGD forum code:
http://www.youtube.com/watch?v=2pm_sPxkqsc (http://www.youtube.com/watch?v=2pm_sPxkqsc)
The API is very simple right now:
[Edit] I've changed the API to closely mimic curl's easy API. I'll update the function description here ASAP. In the meanwhile, please use the provided example.

List of possible errors after doing curl_perform():
http://code.google.com/p/bennugd-monolithic/source/browse/trunk/3rdparty/curl/include/curl/curl.h#393

Hope it's useful. I'll upload binaries for other systems as I compile them.



[Edit] Obviously the idea is to get this into the iOS and Android ports, too.
Title: Re: libcurl bindings
Post by: josebita on December 25, 2011, 10:45:13 PM
win32 binaries added to the first post. Hope they're useful!
Title: Re: libcurl bindings [File downloading, server-client communications]
Post by: josebita on January 03, 2012, 03:07:39 AM
OSX binary updated. Also, one no longer needs to use curl_fetch to download to a string, you just use:
curl_setopt(curl, CURLOPT_WRITEDATA, &output);
Where output is the string where the download will be saved to, obviously.
Title: Re: libcurl bindings [File downloading, server-client communications]
Post by: josebita on January 24, 2012, 10:34:50 PM
Updated the win32 and OSX binaries and re-linked them, this time from Google Code hoping that the bad guys won't remove the files again.
The new version now changes the value of status (when used as curl_perform(curl, &status); ) in case of error to reflect the internal error emitted by libcurl. Possible errors are listed here:
http://code.google.com/p/bennugd-monolithic/source/browse/trunk/3rdparty/curl/include/curl/curl.h#393

Some example code for you to use:
import "mod_video"
import "mod_curl"
import "mod_say"
import "mod_mouse"
import "mod_text"
import "mod_map"
import "mod_file"

Global
int width = 1024;
int height = 768;
int quit=0;
end;

Process bouncer()
Private
    int vx=3, vy=3;

Begin
    graph = load_png("Icon.png");
    // Position the graphic onscreen
    x = 10+graphic_info(0, GRAPH, G_WIDTH);
    y = 10+graphic_info(0, GRAPH, G_HEIGHT);
    while(quit == 0)
        if(x + vx >= width || x+vx < 0)
            vx = -vx;
        end
        if(y+vy >= height || y+vy < 0)
            vy = -vy;
        end
        x += vx; y += vy;
        FRAME;
    End;
End;

Process main()
Private
    int i=0, status=0, curl=0;
    int tostring=0;     // Switch to 1 to download to a string
    string output;

Begin
    set_mode(width, height, 16);
   
    // Remove Google logo, if it exists already
    if(file_exists("classicplus.png"))
        fremove("classicplus.png");
        say("Removed existing logo");
    end;
   
    bouncer();
   
    say("Starting download");
   
    // Start libcurl, set options, perform transfer
    curl = curl_init();
    if(curl == -1)
        say("Curl initialisation failed, quitting");
        quit = 1;
        return;
    end;
   
    curl_setopt(curl, CURLOPT_NOPROGRESS,    1);
    if(tostring == 0)
        // Use this to write to a file
        curl_setopt(curl, CURLOPT_WRITEDATA, "classicplus.png");
        curl_setopt(curl, CURLOPT_URL, "http://www.google.es/logos/classicplus.png");
    else
        // Use this to download to a string
        curl_setopt(curl, CURLOPT_WRITEDATA, &output);
        curl_setopt(curl, CURLOPT_URL, "http://www.google.es/");
    end
   
    curl_perform(curl, &status);
   
    // Wait for the transfer to finish
    while(status < 0)
        FRAME;
    end;
   
    if(status > 0)
        say("Transfer failed with status "+status+", quitting");
        quit=1;
        return;
    end
   
    if(tostring == 0)
        // Replace the bouncer image by the google logo we just downloaded
        unload_map(0, son.graph);
        son.graph = load_png("classicplus.png");
    else
        write(0, 0, 0, 0, output);
    end
   
    curl_cleanup(curl);
   
    say("Download done!");

    while(! mouse.right)
        FRAME;
    end;
    quit=1;
End;