Hier mal ein kleines Sample, was man alles machen muss, damit man mit der vlf Library eine Datei downloaden kann ;-)
// Erstmal ein paar grundlegende Funktionen, die wir brauchen
/**
* Inits the network library
*/
void OnNetInit()
{
sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
sceUtilityLoadNetModule(PSP_NET_MODULE_PARSEURI);
sceUtilityLoadNetModule(PSP_NET_MODULE_PARSEHTTP);
sceUtilityLoadNetModule(PSP_NET_MODULE_HTTP);
sceNetInit(0x20000, 0x20, 0x1000, 0x20, 0x1000);
sceNetInetInit();
sceNetApctlInit(0x8000, 48);
sceNetResolverInit();
sceHttpInit(0x25800);
}
/**
* Terminates the network library
*/
void OnNetTerm()
{
sceNetTerm();
sceHttpEnd();
sceNetApctlTerm();
sceNetResolverTerm();
sceNetInetTerm();
sceUtilityUnloadNetModule(PSP_NET_MODULE_HTTP);
sceUtilityUnloadNetModule(PSP_NET_MODULE_PARSEHTTP);
sceUtilityUnloadNetModule(PSP_NET_MODULE_PARSEURI);
sceUtilityUnloadNetModule(PSP_NET_MODULE_INET);
sceUtilityUnloadNetModule(PSP_NET_MODULE_COMMON);
}
/**
* Checks if the PSP is connected to the internet
* @return 1 if connected, 0 if not
*/
int IsConnected()
{
int state;
sceNetApctlGetState(&state);
if (state == 4) { return 1; }
else { return 0; }
}
/**
* Lets the user connect to the internet by showing the dialog
* @return 1 if connected, 0 if not
*/
int DoConnection()
{
/* show the dialog */
vlfGuiNetConfDialog();
/* check connection */
if(IsConnected()) { return 1; } /* connected */
else { return 0; } /* not connected */
}
/**
* Downloads a file from the internet
* @param url - The direct link to the file to get to
* @param saveAs - Path including filename to write data to
*/
int wget(char*url,char*saveAs)
{
int tpl, cnx, req, ret;
u32 written;
if((tpl=sceHttpCreateTemplate("PSP-InternetRadio/1.0", 1, 1))<0)return tpl;
if((cnx=sceHttpCreateConnectionWithURL(tpl, url, 0))<0)return cnx;
if((req=sceHttpCreateRequestWithURL(cnx, PSP_HTTP_METHOD_GET, url, 0))<0)return req;
if((ret=sceHttpSendRequest(req, 0, 0))<0)return ret;
if(saveAs)
{
SceUID fd=sceIoOpen(saveAs, PSP_O_WRONLY | PSP_O_CREAT, 0777);
// Log
//debug_log("Downloading %s\n", url);
//debug_log("Filesize: %d\n", fsize);
while((ret=sceHttpReadData(req,buf,sizeof(buf)))>0)
{
written = sceIoWrite(fd,buf,ret);
}
ret=sceIoClose(fd);
}
else
{//store in ram
ret=sceHttpReadData(req,buf,sizeof(buf));
}
sceHttpDeleteRequest(req);
sceHttpDeleteConnection(cnx);
sceHttpDeleteTemplate(tpl);
return ret;
}
// Hier jetzt die Main-Funktion
int main()
{
/* Hier musst du erstmal dein Setupkram machen
* also Menü, Hintergrundfarbe, Titlebar etc
*/
// Network Module laden
OnNetInit();
// Hier wird der Status gespeichert, ob der Benutzer
// verbunden ist oder nicht
int connected = 0;
// Network-Dialog aufrufen
connected = DoConnection();
// Überprüfen ob der Benutzer verbunden ist
if(connected == 1)
{
// Verbunden, also kann jetzt gedownloadet werden
wget("www.myhost.com/myfile.rar", "ms0:/myfile.rar");
// Network Module terminieren
OnNetTerm();
}
return 0;
}