sceHttpSendRequest in exit_callback doesn't work

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

Moderators: cheriff, TyRaNiD

Post Reply
vista200
Posts: 13
Joined: Wed Mar 19, 2008 7:55 am

sceHttpSendRequest in exit_callback doesn't work

Post by vista200 »

Hi there!

I ran into some trouble again when I tried to do something trivial: I try to port a XML-based chat to the PSP. Therefore I need to download the responses at least into the RAM. Everything works great, but when I'm going to "kill" the program with the HOME-Buttons and I answer the question for quitting with yes, the PSP comes to the XMB normally, without having downloaded the given file as set in the exit_callback.

Some debug info gave me the impression of sceHttpSendRequest has failed with an error code of 80431076. All this happens in the exit_callback, even if I put it in a new function or starting a new thread with the given "logout"-function.

Code: Select all

int exit_callback() {

	logout();
	
	if (message_thread >= 0) {
		logfile_write("Deleting message_thread\n");
		sceKernelSuspendThread(message_thread);
		sceKernelTerminateDeleteThread(message_thread);
	}
	
	if (netinit) { 
		logfile_write("Terminating Net\n");
		netTerm(); 
	}
	
	if (modules) {
		logfile_write("Unloading Modules\n");
		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);
	}
	
	logfile_write("Exiting (callback)...\n");
	sceKernelExitGame();
	return 0;
}
Everything works fine here, except the failing download() in the logout():

Code: Select all

int logout() {
	timestring();
	sprintf(tempbuffer, "http://www.pspfreak.de/forum/chat/dologout.php?id=%s", id);
	sprintf(file, "ms0:/PSP/GAME/flashchat/%s-logout.xml", timebuffer);
	sceKernelDelayThread(50000);
	sceKernelSuspendThread(message_thread);
	sceKernelTerminateDeleteThread(message_thread);
	sceKernelDelayThread(50000);
	download(tempbuffer, file);
	return 0;
}
timestring() gives the date/time in a format like yyyy-MM-dd-hh-mm-ss and saves it into timebuffer. The message_thread checks every five seconds for a file on a server and downloads it.

Code: Select all

int download(char *the_url, char *save_as) {
	u64 filesize;
	int nbytes;
	int templ;
	int connection;
	int request;
	int status;
	
	int err = 0;

	if &#40;&#40;err = sceHttpInit&#40;20000&#41;&#41; < 0&#41;&#123;
		sceHttpEnd&#40;&#41;;
		return err;
	&#125; else &#123;
		templ = sceHttpCreateTemplate&#40;"FCatPSP", 1, 0&#41;;

		if &#40;templ < 0&#41;&#123;
			sceHttpDeleteTemplate&#40;templ&#41;;
			return templ;
		&#125; else &#123;
	
			if &#40;&#40;err = sceHttpSetResolveTimeOut&#40;templ, 3000000&#41;&#41; < 0&#41;&#123;
				return err;
			&#125; else &#123;
		
				if &#40;&#40;err = sceHttpSetRecvTimeOut&#40;templ, 60000000&#41;&#41; < 0&#41;&#123;
					return err;
				&#125; else &#123;
			
					if &#40;&#40;err = sceHttpSetSendTimeOut&#40;templ, 60000000&#41;&#41; < 0&#41;&#123;
						return err;
					&#125; else &#123;
				
						connection = sceHttpCreateConnectionWithURL&#40;templ, the_url, 0&#41;;
						if &#40;connection < 0&#41;&#123;
							sceHttpDeleteConnection&#40;connection&#41;;
							return connection;
						&#125; else &#123;
					
							request = sceHttpCreateRequestWithURL&#40;connection,PSP_HTTP_METHOD_GET, the_url, 0&#41;;
							if &#40;request < 0&#41;&#123;
								sceHttpDeleteRequest&#40;request&#41;;
								return request;
							&#125; else &#123;
								if &#40;&#40;err = sceHttpSendRequest&#40;request, NULL, 0&#41;&#41; < 0&#41;&#123;
									return err;
								&#125; else &#123;
							
									sceHttpGetStatusCode&#40;request, &status&#41;;
									if &#40;status != 200&#41;&#123;
										return status;
									&#125; else &#123;

										sceHttpGetContentLength&#40;request,&filesize&#41;;
								
										int file;
										if&#40;&#40;file = sceIoOpen&#40;save_as, PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC, 0777&#41;&#41;&#41; &#123;
								

											while &#40;&#40;nbytes = sceHttpReadData&#40;request, filebuffer, sizeof&#40;filebuffer&#41;&#41;&#41; > 0&#41;
											&#123;
												sceIoWrite&#40;file, filebuffer, nbytes&#41;;
											&#125;
											sceIoClose&#40;file&#41;;
											sceHttpEnd&#40;&#41;;
											sceHttpDeleteTemplate&#40;templ&#41;;
											sceHttpDeleteConnection&#40;connection&#41;;
											sceHttpDeleteRequest&#40;request&#41;;
											return 0;
										&#125;
									&#125;
								&#125;
							&#125;
						&#125;
					&#125;
				&#125;
			&#125;
		&#125;
	&#125;
	return 0;
&#125;
The download-code... working fine, except in the exit_callback.

I appreciate your help!

Yours sincerely,
vista200
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

It starts shutting down stuff for kernel reboot when you select Yes in the exit screen. You're only meant to sceKernelExitGame();

I had a similar problem in the past with writing a large file that took a few seconds to complete.

Alternatively, you could not register the exit callback in the beginning. Detect the Home key from a kernel module, and write the file first. Then register the exit callback (If the writing completes fast enough and the user is still holding the Home key, the exit screen will show as soon as the callback is registered, otherwise you'll have to invoke it manually. I do this in Lockdown In-Game, except I find and unregister the game's callback.).
vista200
Posts: 13
Joined: Wed Mar 19, 2008 7:55 am

Post by vista200 »

Just a quick thought:

Wouldn't it be possible to register no callbacks at all and if home is pressed to ShowMessageDialog(). This is the same, isn't it? If the user selects "Yes" the PSP will do the logout, terminate the net and unload the modules.

Another question: What about the error? Can anyone reproduce the error? What type of error is it?

Thanks for the help! I'll try ASAP

vista200
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

vista200 wrote:Just a quick thought:

Wouldn't it be possible to register no callbacks at all and if home is pressed to ShowMessageDialog(). This is the same, isn't it? If the user selects "Yes" the PSP will do the logout, terminate the net and unload the modules.
Yes you can do that. It didn't strike me, because all official games use the exit callback and I had to use that in Lockdown :)

I guess it would be more efficient to run a kernel module that exports a kernel-user sceCtrlReadBufferPositive and call that from your main module, instead of having a separate thread running in the kernel module just to check the home button.
Post Reply