Cancel Httprequest

Hi,

I am sending an Httprequest using CCHttpClient::getInstance()->send(pRequest);
And I want to wait for certain time, if response didn’t come(ie. internet is slow), I’d like to cancel that request.
Is it possible to cancel the request which is already sent ?

Another option for me is to set the timeout of cchttpclient,but my application is crashing when I try that.Did anyone try that ? Please share the code

Thanks

Sorry, I don’t know if you can cancel the request once it’s been sent. You could tag the request and on the response ignore it if it has that tag.

Hi,
Thanks for your reply…

Using tag is a good idea,
But my application is crashing before it comes to download completed handler.

Can you provide the error, or some of your code? CCHttpClient is tricky to work with, so I want to see how you are making requests.

Hi
Sorry for my late reply…

Let me explain what am doing:
I have a Reachability class ,which will check for internet connection status.
I am calling its function from Appdelegate.cpp .
the function looks like :

void Reachability::getNetworkConnectionStatus()
{

*pRequest = new cocos2d::extension::CCHttpRequest;
string link1 = TEST_LINK;
link1.replace;
*pRequest~~>setUrl);
_pRequest~~>setRequestType(CCHttpRequest::kHttpGet);
_pRequest->setResponseCallback(this,callfuncND_selector(Reachability::onRequestCompleted));

CCHttpClient::getInstance()>send;
m_pScheduler
>scheduleSelector(schedule_selector(Reachability::checkForResponse), this, MAX_RESPONSE_TIME, 0, 0,false);
}

I have an onRequestCompleted() handler and a scheduled funtion checkForResponse().

if onRequestCompleted() is fired first, I am taking the connection as proper.
if in onRequestCompleted() , if response is null,it is no connection.
And if onRequestCompleted() is not fired within time interval MAX_RESPONSE_TIME , the sceduled function will be fired first and this case I am taking as slow connection

When it is proper connection, I am starting other downloads.

But my app crashes occasionally even if it returns proper connection.

is it the default timeout making problem here ?

We’ve totally rewritten CCHttpClient for our game, to include support for canceling pending requests (no support to cancel an ongoing request), configurable timeouts and retries. Unfortunately it’s no longer compatible with the original class (as we switched from strings to integers for tags (easier to handle in a switch statement :)).
We had to rebuild libcurl with support for SSL and added libssl, libcrypto and libcares as well. I would suggest to include them in standard cocos2d-x, as most probably all developers will need secure connections for communicating with their servers.

I will try to make a pull request for it.

Hi Vani,

You need to actually set NULL to all the callbacks in the Scene before you actually exit the scene. So that there is no bad memory pointers. In your case,

_pRequest->setResponseCallback(NULL);

Need not to say, if you want to handle the callbacks of current scene in the new scene then you need to set the reference of new scene in the present scene’s callbacks. :slight_smile:

PS: There error situtation you are facing is called as dangling pointer in C++.

Hi
Can anybody help me with almost the same problem. In my case I have a lot of images to download from different sources. But when user switches screen I need to cancel all downloads (because new images will be only in a queue and I need them to be downloaded immediately). How can I do this?

have you made the pull request? I really need that cancel transfer feature in my game. I don’t really need ssl, so maybe I’ll try to re-adjust the code