URL progress callback

I always get -1 in the bytesSent variable, the totalBytes seem OK and the context is OK but i can’t get bytesSent to work.

I’m on 10.6.2 and latest tip as always.

Yeah, on the mac I couldn’t find any way to get that information, sorry!

No problem i thought i was missing something, don’t really need that so much.

I found a way:

  • (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite

Here is the diff I tested:

Index: lib/juce/modules/juce_core/native/juce_mac_Network.mm
--- lib/juce/modules/juce_core/native/juce_mac_Network.mm	(revision 259)
+++ lib/juce/modules/juce_core/native/juce_mac_Network.mm	(working copy)
@@ -117,6 +117,8 @@
     int64 contentLength;
     NSDictionary* headers;
     NSLock* dataLock;
+    URL::OpenStreamProgressCallback* _callback;
+    void* _callbackContext;
 - (JuceURLConnection*) initWithRequest: (NSURLRequest*) req withCallback: (URL::OpenStreamProgressCallback*) callback withContext: (void*) context;
@@ -125,6 +127,7 @@
 - (void) connection: (NSURLConnection*) connection didFailWithError: (NSError*) error;
 - (void) connection: (NSURLConnection*) connection didReceiveData: (NSData*) data;
 - (void) connectionDidFinishLoading: (NSURLConnection*) connection;
+- (void) connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;
 - (BOOL) isOpen;
 - (int) read: (char*) dest numBytes: (int) num;
@@ -181,15 +184,14 @@
     hasFinished = false;
     contentLength = -1;
     headers = nil;
+    _callback = callback;
+    _callbackContext = context;
     runLoopThread = new JuceURLConnectionMessageThread (self);
     while (runLoopThread->isThreadRunning() && ! initialised)
-        if (callback != nullptr)
-            callback (context, -1, (int) [[request HTTPBody] length]);
         Thread::sleep (1);
@@ -268,6 +270,12 @@
+- (void) connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
+    if (_callback != nullptr)
+        _callback (_callbackContext, (int)totalBytesWritten, (int)totalBytesExpectedToWrite);
 - (BOOL) isOpen
     return connection != nil && ! hasFailed;
Index: shared/data/RMetaData.cpp

Thanks. The current code bears no resemblance to the old version that you’ve based that on, but I’ll take a look at that callback method.

In the code that you added for this, I get warnings when building for 64bits, that are easily fixed with a cast:

line 284 says:

and the two arguments need to be int, thus

fixes it for 64 bit builds

Hi Jules

I was wondering if this had been fixed ? I still see the 

                callback (context, -1, (int) [[request HTTPBody] length]);

line in the code for the Mac and indeed get -1 instead of the number of sent bytes. Am I missing something ?


This should be fixed in the latest tip!

Seems to work...

Thanx Fabian !