Hi, I implemented a threaded file downloader and it works well in general. but when it fails to download a file, the urlResponse listener gets called multiple times even after calling ofRemoveURLRequest(id);
Here’s my example code
URLSaver::URLSaver()
{
ofAddListener(ofURLResponseEvent(), this, &URLSaver::urlResponse);
startThread();
}
URLSaver::~URLSaver()
{
commands.close();
waitForThread(true);
ofRemoveListener(ofURLResponseEvent(), this, &URLSaver::urlResponse);
}
void URLSaver::saveURL(const string &url, const string &path)
{
ofURLSaverEntry entry;
entry.url = url;
entry.path = path;
entry.shouldStop = false;
commands.send(entry);
}
void URLSaver::clear()
{
ofURLSaverEntry entry;
entry.shouldStop = true;
commands.send(entry);
}
void URLSaver::threadedFunction(){
ofURLSaverEntry entry;
while (commands.receive(entry)) {
if (!entry.shouldStop)
ofSaveURLAsync(entry.url, entry.path);
else
ofRemoveAllURLRequests();
}
}
void URLSaver::urlResponse(ofHttpResponse &response)
{
if (response.status == 200) {
cout << "FILE DOWNLOAD SUCCESS" << endl;
}
else {
cout << "FILE DOWNLOAD FAILED" << endl;
ofRemoveURLRequest(response.request.getId());
}
}
If the file is successfully downloaded, it prints “FILE DOWNLOAD SUCCESS” once as it should be.
But if it fails to download a file, it prints “FILE DOWNLOAD FAILED” multiple times (like 4~10 times). But I don’t understand why.
Is this normal? Any idea how to fix this? Thanks!