wxHTTP nonblocking without using threads?
Posted: 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
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;
}