Re: [OSX Core Foundation]How can I asynchronously upload a file though HTTP and get a callback called while sending bytes of the stream?
Re: [OSX Core Foundation]How can I asynchronously upload a file though HTTP and get a callback called while sending bytes of the stream?
- Subject: Re: [OSX Core Foundation]How can I asynchronously upload a file though HTTP and get a callback called while sending bytes of the stream?
- From: Rémy Saissy <email@hidden>
- Date: Mon, 6 Dec 2010 13:49:09 +0100
Hi,
thanks for your advices, now it works :).
Have a great day!
On Thu, Dec 2, 2010 at 9:26 PM, Mark Pauley <email@hidden> wrote:
> Take a look at attaching the read end of a read-write bound pair stream to the CFHTTPMessage object with CFHTTPMessageSetBodyStream. You write to the CFWriteStream when possible (just as you are doing with the CFSocket) and so you'll know how the upload is progressing.
>
> Here is a reference:
> http://developer.apple.com/library/mac/#DOCUMENTATION/CoreFoundation/Reference/CFStreamConstants/Reference/reference.html#//apple_ref/doc/uid/TP40003362-CH5-F17772
>
> As far as writing too few bytes: it's fine to write less than the maximum amount, but you only get one event until you call CFWriteStreamWrite again. So for example: if you don't quite have any bytes to write yet and you decide to ignore the write event, your code will just wait around. CFStream won't tell you again, it has already told you and is waiting for you to write. A workaround for this is to set a flag that you check when you do get bytes to write. Set the flag when you decide to ignore an event and write immediately when the bytes come if the flag is set. Clear the flag every time you write bytes to the stream.
>
>
> _Mark
>
> On Dec 2, 2010, at 11:48 AM, Rémy Saissy wrote:
>
>> Hi Mark,
>> yes, now you are talking about it, I didn't write as many bytes as I
>> could in the WriteStream. Only a few 16kb.
>> Maybe that's why It wasn't called again.
>>
>> I don't use CFReadStreamCreateWithHTTPMessage or
>> CFReadStreamCreateWithHTTPStreamedMessage because I want to be
>> notified of the upload as it is progressing.
>> And indeed, socket are not a good solution because of more advanced
>> HTTP behaviours which will come in the project, that's sure.
>> Regards,
>>
>>
>> On Thu, Dec 2, 2010 at 7:53 PM, Mark Pauley <email@hidden> wrote:
>>>
>>> On Dec 2, 2010, at 4:46 AM, Rémy Saissy wrote:
>>>
>>>> Hi,
>>>> as shown in SampleURLConnection, I created a CFWriteStreamRef using a
>>>> CFSocket previously created and connected to the server and configured
>>>> with
>>>> CFSocketSetSocketFlags(sock, kCFSocketAutomaticallyReenableWriteCallBack);
>>>> CFSocketEnableCallBacks(sock, kCFSocketWriteCallBack);
>>>> event though there is no Write callback defined at socket level.
>>>> Then I bound it to my WriteStream with CFStreamCreatePairWithSocket
>>>> set a delegate with CFWriteStreamSetClient and scheduled the Write
>>>> Stream on the runloop.
>>>>
>>>> It works except that the callback only works for the first event
>>>> (kCFStreamEventOpenCompleted). After, it is like disabled.
>>>> right before I tried with a CFSocket directly and setting the
>>>> kCFSocketAutomaticallyReenableWriteCallback flag fixed this behaviour.
>>>> Should I use something else when at WriteStream level?
>>>> Thank you very much.
>>>
>>> I'm not positive, but it sounds like you may be ignoring a write event. CF*Streams will only send you the event once, the next events are ignored until you write or read to the stream.
>>>
>>> With CFSockets, I believe you are correct: you must either use kCFSocketAutomaticallyReenableWriteCallBack or you must call CFSocketEnableCallBacks after every write callback. That call literally adds the underlying fd to the select set (or the equivalent with whatever the current underlying paradigm is on your system).
>>>
>>> I'm curious: why are you using CFSockets directly when you could use either NSURLConnection or CFReadStreamCreateWithHTTPMessage?
>>>
>>> Your CFSocket implementation will be an uphill climb once you need to support proxies not to mention other, more advanced HTTP behavior.
>>>
>>>>
>>>>
>>>>
>>>>
>>>> On Wed, Dec 1, 2010 at 5:53 PM, Quinn "The Eskimo!" <email@hidden> wrote:
>>>>>
>>>>> On 1 Dec 2010, at 16:46, Rémy Saissy wrote:
>>>>>
>>>>>> I would prefer to avoid using Cocoa since my launch agent is exclusively written using CF.
>>>>>
>>>>> Understood. However, we definitely prefer folks using NSURLConnection where at all possible.
>>>>>
>>>>> S+E
>>>>> --
>>>>> Quinn "The Eskimo!" <http://www.apple.com/developer/>
>>>>> Apple Developer Relations, Developer Technical Support, Core OS/Hardware
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> Do not post admin requests to the list. They will be ignored.
>>>>> Macnetworkprog mailing list (email@hidden)
>>>>> Help/Unsubscribe/Update your Subscription:
>>>>>
>>>>> This email sent to email@hidden
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Rémy Saissy
>>>> Web: http://picasaweb.google.fr/remy.saissy
>>>> "J'aime les belles plantes car cela me donne envie de devenir jardinier."
>>>> _______________________________________________
>>>> Do not post admin requests to the list. They will be ignored.
>>>> Macnetworkprog mailing list (email@hidden)
>>>> Help/Unsubscribe/Update your Subscription:
>>>>
>>>> This email sent to email@hidden
>>>
>>>
>>
>>
>>
>> --
>> Rémy Saissy
>> Web: http://picasaweb.google.fr/remy.saissy
>> "J'aime les belles plantes car cela me donne envie de devenir jardinier."
>
>
--
Rémy Saissy
Web: http://picasaweb.google.fr/remy.saissy
"J'aime les belles plantes car cela me donne envie de devenir jardinier."
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden