Need help with WiFi/HTTP code

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Gary13579
Posts: 93
Joined: Mon Aug 15, 2005 7:43 am

Need help with WiFi/HTTP code

Post by Gary13579 »

I'm using the following code to download files from an HTTP web server. This is using code Shine posted a while back in IRC.

Code: Select all

int capacity = 1024000;
int len = 0;
u8* buffer = (u8*) malloc(capacity);
while (1) {
	// read data
	int count = sceNetInetRecv(sockListen, (u8*) &buffer[len], capacity - len, 0);

	// in blocking mode it has to return something, otherwise it is closed
	// (which is the default for HTTP/1.0 connections)
	if (count == 0) break;
	
	if &#40;count < 0&#41; &#123;
		pspDebugScreenPrintf&#40;"read error&#58; %i\n", err&#41;;
		break;
	&#125;

	// adjust buffer, if needed
	len += count;
	if &#40;len + 1024000 > capacity&#41; &#123;
		capacity *= 2;
		buffer = realloc&#40;buffer, capacity&#41;;
		if &#40;!buffer&#41; break;
	&#125;
&#125;

// now "len" bytes data are in buffer, add a 0 at end for printing and search for header end
if &#40;buffer&#41; &#123;
	buffer&#91;len&#93; = 0;
	u8* page = strstr&#40;&#40;char*&#41; buffer, "\r\n\r\n"&#41;;
	if &#40;page&#41; &#123;
		page += 4;
		pspDebugScreenPrintf&#40;"\n\n%s", page&#41;;
		SceUID file;
		file = sceIoOpen&#40;"ms0&#58;/PSP/MUSIC/test.mp3", PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777&#41;;
		sceIoWrite&#40;file, page, len&#41;;
		sceIoClose&#40;file&#41;;
	&#125;
	free&#40;buffer&#41;;
&#125;
Now this code works GREAT while downloading an MP3 that is under a certain size (from testing, it seems around 6-7mb).
If the MP3 is, say, 8mb, then it will start downloading it and just eventually stop. It slows way down and the WiFi light doesn't blink as much as while it is downloading.

Anyone have any clue why?
planetusa
Posts: 21
Joined: Tue Dec 06, 2005 3:03 pm
Location: Asheville
Contact:

Post by planetusa »

I myself had similar issue....I think it's the PSP, and not your code, but I haven't determined for certain.

Right now, I suggest using the ported libcurl

(Bottom of page)

http://forums.ps2dev.org/viewtopic.php?t=4275&start=0
planetusa
Posts: 21
Joined: Tue Dec 06, 2005 3:03 pm
Location: Asheville
Contact:

Post by planetusa »

As to a why.....I actually notice something, something from when I was working on something else.....the more full buffer gets, the slower the psp is, and is in writing to buffer, if you could write to the file as the data comes in, you may have a solution.

Michael
Gary13579
Posts: 93
Joined: Mon Aug 15, 2005 7:43 am

Post by Gary13579 »

planetusa wrote:As to a why.....I actually notice something, something from when I was working on something else.....the more full buffer gets, the slower the psp is, and is in writing to buffer, if you could write to the file as the data comes in, you may have a solution.

Michael
In the final code though, I don't want to have to write the file to the memory stick, that is just my way of testing to see if it works.

Now, sorry for showing my noob, but what is libcurl? And how would it solve my problem?

Edit:
curl is a command line tool for transferring files with URL syntax, supporting FTP, FTPS, TFTP, HTTP, HTTPS, TELNET, DICT, FILE and LDAP.

I really need to start googling more, heh.
Again, it seems like this will save it to the memory stick, which I really do not want to do. I want to be able to play the mp3 without having to write it to a file, sort of like WiFi Jukebox, but instead of having to use a custom visual basic program, you can use an HTTP server which will run on any OS.
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

llibcurl has a callback function which you can do what you want with the output.

(Post: http://forums.ps2dev.org/viewtopic.php?p=32816#32816 )
If you have a look at the example I included with the ported libcurl, the function that is being called back is
write_data(void *ptr, size_t size, size_t nmemb, void *stream)

You get sent (size*nmemb) bytes of data pointed to by ptr, and you return the number of bytes from it you have processed.

So you could pass this data straight onto an mp3 decoder or whatever ;)
dbeyer3069
Posts: 81
Joined: Mon Dec 19, 2005 4:09 pm

Post by dbeyer3069 »

danzel wrote:llibcurl has a callback function which you can do what you want with the output.

(Post: http://forums.ps2dev.org/viewtopic.php?p=32816#32816 )
If you have a look at the example I included with the ported libcurl, the function that is being called back is
write_data(void *ptr, size_t size, size_t nmemb, void *stream)

You get sent (size*nmemb) bytes of data pointed to by ptr, and you return the number of bytes from it you have processed.

So you could pass this data straight onto an mp3 decoder or whatever ;)
By the way, the libcurl progress callback works really well too. You need to pass a NULL for the first field (widget object) but it otherwise works great. You can abort a download by returning anything other than 0 in the progress callback. Just some free info for other aspiring libcurl people.

David Beyer
Post Reply