Re: multithreaded vs ...?
Re: multithreaded vs ...?
- Subject: Re: multithreaded vs ...?
- From: email@hidden
- Date: Mon, 18 Feb 2002 14:51:40 -0800
I use SmallSockets (
http://smallsockets.sourceforge.net), plus i've
subclassed it a lot to do async socket accept and async reading... I
contributed the code back to the author of SmallSockets, but he hasn't
released an integrated version yet.
but you seem to be on the right track... I use
NSFileHandleReadCompletionNotification then the callback has the data
already in it... [[notification userInfo]
objectForKey:NSFileHandleNotificationDataItem]...
then in the callback, I just do [[notification object]
readInBackgroundAndNotifyForModes:[NSArray
arrayWithObjects:NSEventTrackingRunLoopMode, NSModalPanelRunLoopMode,
NSDefaultRunLoopMode, nil]] to get the next packet to be read async.
that you you never have to futz with the raw fd... i'm not sure if
NSFileHandle will complain if you do...
On Monday, February 18, 2002, at 01:24 PM, Jay Kuri wrote:
Thanks Tom,
Am I right in thinking that doing it this way would be something along
these lines:
Create (and connect) my network socket and get a NSFileHandle with
initWithFileDescriptor,
register to recieve notification:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:dataToRead \
name:NSFileHandleDataAvailableNotification object:self];
then:
[clientHandle waitForDataInBackgroundAndNotify];
handle incoming data in dataToRead, reading the data I need (possibly
over several calls to dataToRead). Once I have a full packet, process
it and call app-defined delegate method to decide what to do with the
message?
Is there anything I should be aware of when operating on a file
descriptor directly when I have a NSFileHandle object based on that
same descriptor?
I ask because I'll probably use a nonblocking read directly on the file
descriptor so that I don't get hung up waiting for data that hasn't
arrived yet.
Can anyone point me in the right direction for any documentation or
examples of this type of code? (using waitForDataInBackgroundAndNotify,
etc.)
Thanks again,
Jay
On Monday, February 18, 2002, at 11:01 AM, email@hidden wrote:
On Sunday, February 17, 2002, at 11:32 PM, Jason Kuri wrote:
I'm developing a cocoa framework to interface with a server using a
network messaging protocol (tcp based, variable-length records). The
concept is to provide a connection object which an app can use to
handle all communications with the server. (Basically dealing with
the connection process and complexities of packet arrival and
decoding, encoding, etc.)
My question is, given that the object will have to deal with packets
arriving at random times and that they will need to be dealt with
relatively quickly, what is the best way to incorporate my
network-processing into the applications event-loop?
If I were writing this in C, I would either create a separate thread
for handling network activity, or use nonblocking io and a select()
loop with callback functions. I think I have a reasonable
understanding of NSApplication and multi-threading in cocoa. Based
on what I've read, I have come to the conclusion that the best way to
handle this is to require that my connection-object operate in it's
own thread... but as I am relatively new to cocoa programming, I'd
like to get a second opinion. Is there a better way to handle this?
I'm concerned that forcing it into it's own thread will make using
the framework overly complicated.
Does anyone have any advice or pointers they could share? I'm
guessing my question has a fairly straightforward answer, but I'm
relatively new to Cocoa programming (though I've been programming
Unix-C for years), and I would really appreciate any pointers anyone
can provide.
Assuming that your network packets are going to cause user interface
changes, I would suggest that you do your IO in the main runloop. do
your accepts, reads and writes in non-blocking io via
readInBackgroundAndNotify and similar constructs.
Using threads in Cocoa is an over-complex obfuscated mess. It is the
one place that Java actually is better than Objective-C.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.