Re: Best way to handle background i/o and notify main UI thread
Re: Best way to handle background i/o and notify main UI thread
- Subject: Re: Best way to handle background i/o and notify main UI thread
- From: Chris Irvine <email@hidden>
- Date: Thu, 10 Jul 2008 17:19:08 -0700
On Jul 1, 2008, at 4:27 PM, Jean-Daniel Dupas wrote:
Le 2 juil. 08 à 01:24, Chris Irvine a écrit :
On Jul 1, 2008, at 3:17 PM, Douglas Davidson wrote:
On Jul 1, 2008, at 3:11 PM, Chris Irvine wrote:
I've been doing a lot of reading about this, but I still don't
feel like I have a concise answer. I'm building a small suite of
applications that all will all share similar interaction with a
UDP based network based peer. It will be necessary to have
various background threaded activities to keep the connection
alive and monitored. I've already built a small proof of concept
using a separate C program that does standard i/o and can be
managed by a the Cocoa front end using NSTask. This is somewhat
limiting and isn't portable to our most favorite mobile device.
If your need is for the receipt of UDP packets, CFSocket provides
a run loop source that can handle that.
And then I suppose you would suggest CFRunLoopTimer to schedule
transmission of keep-alive packets or other periodic connection
handling activities? or, something more cocoa-esk ?
-Chris
CFRunLoopTimer, NSTimer, it's just a matter of tast ;-)
For the archives...
Thanks for the pointers. I'm pretty happy with the working skeleton I
have now.
I'm performing my initial UDP packet exchange using traditional BSD
sockets using the traditional functions like socket(), bind(),
connect(), and select().
After initialization succeeds, I attached the already opened sockets
as an event source to the run loop using CFSocketCreateWithNative(),
CFSocketCreateRunLoopSource(), and CFRunLoopAddSource(). This allows
the communication to stay alive without blocking. When packets are
received the callback function performs necessary handling for my
proprietary UDP protocol. Since this is all in the main thread, it is
clean for me to post actions that should happen in the UI using
NSNotificationQueue and -
enqueueNotification:postingStyle:coalesceMask:forModes:
Finally, link status monitoring is handled by a simple method that I
trigger periodically using NSTimer.
All this without any NSThread or intra-thread communication or mutexes.
So far, everything seems to be working pretty well. The only potential
I see so far relates to the Run Loop Modes, that might block important
packets from being handled. I tried NSRunLoopCommonModes as a catch
all, but that didn't seem to work at all. Also, any long >2s blocking
activities in my main run loop will likely torpedo my link status, but
those same activities would probably make my user experience sucky...
so I'll just stay away from those.
-Chris
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden