wxHTTP nonblocking without using threads?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
lollisoft
Earned some good credits
Earned some good credits
Posts: 115
Joined: Sat Jul 23, 2005 3:52 pm
Location: Germany
Contact:

wxHTTP nonblocking without using threads?

Post by lollisoft » Sat Jan 24, 2015 9:45 am

Hi,

I am using wxHTTP to check for updates. Using the synchronous calls, all is fine as long as I am online and the server can be reached (ignoring some time to get a file from it).

When it is offline, I'll experience nonreactive GUI and thus I switched to use nonblocking and events. Here I get trouble:

The event wxSOCKET_CONNECTION is fired and I'll start to use the wxHTTP object to check for wxPROTO_NOERR.

I'll like to get this code, but I get 193 and the download doesn't start.

Is wxHTTP usable in async mode without using threads and if so, how to use it correctly?

Thanks, Lothar

Code: Select all

lbErrCodes LB_STDCALL UpdateCheckerHandler::RunUpdateCheck() {

	if (!wxApp::IsMainLoopRunning()) return ERR_NONE; // should return true
		
	if (isJustRunning) return ERR_NONE;
	
	isJustRunning = true;	

	get_poll.SetHeader(_T("Content-type"), _T("text/html; charset=utf-8"));
	get_poll.SetTimeout(3); // 10 seconds of timeout instead of 10 minutes ...
	
	address.Hostname(_T("www.lollisoft.de"));
	address.Service(80);
	
	Connect(SOCKET_ID, wxEVT_SOCKET, (wxObjectEventFunction) &UpdateCheckerHandler::OnSocketEvent );

	get_poll.SetNotify(wxSOCKET_CONNECTION_FLAG |
				   wxSOCKET_INPUT_FLAG |
				   wxSOCKET_LOST_FLAG);
	
	get_poll.SetEventHandler(*this, SOCKET_ID);
	get_poll.Notify(true);
	get_poll.Connect(address, false);
	
	// Get the stream to trigger opening the connection (httpStream to be used in GotConnection)
	httpStream = get_poll.GetInputStream(_T("My URI HERE"));

	// Assume httpStream gets valid any time later

	return ERR_NONE;
}

void UpdateCheckerHandler::OnSocketEvent(wxSocketEvent& event) {
	switch(event.GetSocketEvent())
    {
        case wxSOCKET_CONNECTION:
			GotConnection();
			break;
		case wxSOCKET_LOST:
			isJustRunning = false;
			break;
	}
}

void UpdateCheckerHandler::GotConnection() {

	UAP_REQUEST(getModuleInstance(), lb_I_MetaApplication, meta)

	UAP(lb_I_Parameter, UpdateSettings)
	
	wxProtocolError pErr = get_poll.GetError();

// Here I get code 193?	

	if (pErr == wxPROTO_NOERR)
	{
		wxString res;
		wxStringOutputStream out_stream(&res);

		httpStream->Read(out_stream);
			
		wxJSONReader reader;

// Do my stuff here
	}

	wxDELETE(httpStream);
	get.Close();	
	isJustRunning = false;
}

OS: Windows 7, Mac OS X (Panther/Leopard/Snow Leopard), SuSE Linux, Debian (PPC), OpenMoko FreeRunner
Compiler: OpenWatcom, GCC
wxWidgets 2.8.x
IDE Makefile based.
RAD My own brewed, Code generation with XSLT and DialogBlocks

User avatar
doublemax
Moderator
Moderator
Posts: 14635
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxHTTP nonblocking without using threads?

Post by doublemax » Sat Jan 24, 2015 11:35 am

Is wxHTTP usable in async mode without using threads and if so, how to use it correctly?
I don't know, but wouldn't it be easier to just use a thread?
Use the source, Luke!

lollisoft
Earned some good credits
Earned some good credits
Posts: 115
Joined: Sat Jul 23, 2005 3:52 pm
Location: Germany
Contact:

Re: wxHTTP nonblocking without using threads?

Post by lollisoft » Sat Jan 24, 2015 12:05 pm

I am considering this, but I liked to avoid threads. But it seems that the wxHTTP class is not really working with nonblocking sockets.

I know that wxSockets provide this kind of programming, but then I'll have to reimplement a HTTP client. Isn't there any simple mechanism
to ask for a job to be done in a thread so that I do not have to implement a wxThread derived class?

Thanks, Lothar
OS: Windows 7, Mac OS X (Panther/Leopard/Snow Leopard), SuSE Linux, Debian (PPC), OpenMoko FreeRunner
Compiler: OpenWatcom, GCC
wxWidgets 2.8.x
IDE Makefile based.
RAD My own brewed, Code generation with XSLT and DialogBlocks

User avatar
doublemax
Moderator
Moderator
Posts: 14635
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxHTTP nonblocking without using threads?

Post by doublemax » Sat Jan 24, 2015 12:32 pm

Isn't there any simple mechanism to ask for a job to be done in a thread so that I do not have to implement a wxThread derived class?
Probably.

Unfortunately sockets is one of the few areas in wxWidgets that i know almost nothing about, so i can't help with this.
Use the source, Luke!

User avatar
T-Rex
Moderator
Moderator
Posts: 1190
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: wxHTTP nonblocking without using threads?

Post by T-Rex » Sun Jan 25, 2015 4:29 pm

You can put the HTTP request to the separate thread, that's not complex. You can use wxThreadHelper for this and subclass your frame from it.
Another option is wxCURL or libevent (non-wx lib but is also quite good).

Post Reply