Author Topic: Does NetSend() use a cache to not run the callback?  (Read 1285 times)

HunterGreen

  • Guest
Does NetSend() use a cache to not run the callback?
« on: March 15, 2014, 08:35:33 am »
I'm developing an app that will pull information from a web site in the form of a bunch of text flat-files (the server in question isn't smart enough to do better), and one image, by firing off a string of six NetSend() calls with corresponding callbacks that will stash the data received -- in the case of the image, it writes it as an image file, and in the others, it will parse them into SQLite databases, though only one of the five has been written to do that so far; the rest just printf() that they got called and exit for now.

Yesterday I was going nuts trying to test this code because it seemed like the NetSend() callback never fired. I put tons of printf()s all around to be sure that NetSend itself was being executed (it is, all six of them), and to verify if the callbacks fire, but they wouldn't. I know they have in the past; for instance, that image I'd be pulling down to display, it displays correctly. But I couldn't get any of the code in the callbacks to ever execute. Frustrated, I packed it in for the night.

This morning, I tried again, and one of the flat-file capture callbacks fired -- once. I saw a bug in it, fixed it, tried to run it again... and it won't fire again. It occurs to me that the file in question got updated at 6:30am this morning by an automated process, so maybe that's why it fired once today... but the image file hasn't been updated in weeks, so maybe it won't fire until that image gets updated again. If NetSend is implementing some internal caching, or relying on something in iOS that does, that could be very useful in production, but is there any way to override it in the simulator, or otherwise ensure that the NetSend and callback will actually happen, at least during development?

Then again maybe it's a coincidence and has nothing to do with whether the data is fresh. Any other ideas for something else to check?

For reference, this is what the block that calls the NetSend()s looks like, though I've changed the names to protect the innocent:

Code: [Select]
NetSend("http://some/path/images/some-image.png", ReceivedThumbnail);
NetSend("http://some/path/Downloads/file1.txt", ReceivedFile1);
NetSend("http://some/path/Downloads/file2.txt", ReceivedFile2);
NetSend("http://some/path/Downloads/file3.txt", ReceivedFile3);
NetSend("http://some/path/Downloads/file4.txt", ReceivedFile4);
NetSend("http://some/path/Downloads/file5.txt", ReceivedFile5);
And here's an example of one of the callbacks:

Code: [Select]
int ReceivedThumbnail(int event, char *response, int length) {
  printf("ReceivedThumbnail\n");
  if (event == 0) return 0;
  FileDelete("Thumbnail.png");
  int ThumbnailFile = FileCreate("Thumbnail.png");
  FileWrite(ThumbnailFile, response, length);
  FileClose(ThumbnailFile);
  return 0;
}

HunterGreen

  • Guest
Re: Does NetSend() use a cache to not run the callback?
« Reply #1 on: March 15, 2014, 04:12:03 pm »
I tried writing some code that makes a fakely unique URL by adding ?t=<stuff> to the end (I use a date/timestamp to make sure it's unique) to see if that would make it work, but the callbacks still aren't firing. I confirmed that these fake URLs do give me the right results in a browser. Does that mean it has nothing to do with caching, or that the caching isn't fooled by me adding this string to the end?

HunterGreen

  • Guest
Re: Does NetSend() use a cache to not run the callback?
« Reply #2 on: March 16, 2014, 08:02:30 am »
The callbacks won't run today, so it seems to be a coincidence about the timing, not a caching issue. I also tried making a brand new app with nothing but this:

Code: [Select]
//====================================================
// App.cpp
//====================================================
#include "DragonFireSDK.h"

int ReceivedFile(int event, char *response, int length) {
printf("Received File(%d,...,%d)\n",event,length);
return 0;
}

void AppMain() // the program begins here
{
printf("Calling NetSend\n");
NetSend("http://some/path/files/Downloads/file.txt", ReceivedFile);
}

void AppExit()
{
}

void OnTimer()
{
}

Still no call of the callback. Guess I'll try support.
« Last Edit: March 16, 2014, 08:06:32 am by HunterGreen »

HunterGreen

  • Guest
Re: Does NetSend() use a cache to not run the callback?
« Reply #3 on: March 17, 2014, 09:40:40 am »
Support got me the answer I needed (thank you!).

The problem is that, despite the use of a callback creating a separate thread, NetSend() can't be run concurrently; each NetSend() call aborts any previous ones. So the only way to get a bunch of separate NetSend()s to work is with a "daisy chain". NetSend() #1's callback does whatever it has to do, then calls another NetSend() #2, whose callback calls NetSend() #3, etc. until the final NetSend()'s callback then calls a main function (not AppMain of course since that's what started the daisy chain). Makes the code a little harder to read, but it seems to work. (A Visual Studio crash made me lose a lot of what I've done in this program, so I have more work to do to say that this cures all ills -- all my callbacks that I wrote are gone and have to be rewritten -- but it seems to be calling them now, at least).

DragonFire Support agreed this should probably be mentioned in NetSend()'s page in the API, so hopefully in the next version.