In case anyone wants the solution to this puzzle ;-)
while (([inputStream streamStatus] & [outputStream streamStatus]) ==
NSStreamStatusOpening && [[NSRunLoop currentRunLoop]
runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
That line will pump the asynchronous events and test that both streams are
open. It should also include a timeout value as well.
On Feb 2, 2010, at 11:38 AM, Richard Penwell wrote:
> Hello Devs,
>
> I'd like some feedback on my design here as I'm building an API that I hope
> will be of use to the community, and some of the finer nuances of Cocoa still
> escape me.
>
> I'm building a class called TDSConnection which is part of a object graph
> providing connectivity to the Tabular Data Stream format in objective-c.
> (TDS is the Microsoft SQL protocol). What I've got is some interesting
> threading and synchronization issues. The one that I'm trying to check for
> correctness currently is thus:
>
> I have a method -[TDSConnection open] which should open two NSStreams (input
> and output), block until both are open or erred then perform the TDS packets
> for login. The method of doing this for me looks like this:
>
> - (void)open
> {
> [self close];
>
> [NSStream getStreamsToHost:host
> port:port
> inputStream:&inputStream
> outputStream:&outputStream];
>
> [inputStream setDelegate:self];
> [outputStream setDelegate:self];
>
> [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
>
> forMode:NSDefaultRunLoopMode];
> [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
>
> forMode:NSDefaultRunLoopMode];
>
> [inputStreamCondition lock];
> [outputStreamCondition lock];
> [inputStream open];
> [outputStream open];
>
> [inputStreamCondition wait];
> [outputStreamCondition wait];
> }
>
> - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
> {
> if (stream == inputStream) {
> [self handleInputStreamEvent:eventCode];
> } else if (stream == outputStream) {
> [self handleOutputStreamEvent:eventCode];
> } else {
> NSLog(@"Recieved a stream event for instance %@ which was not
> the input or output stream", stream);
> }
> }
>
> - (void)handleInputStreamEvent:(NSStreamEvent)eventCode {
> switch (eventCode) {
> case NSStreamEventOpenCompleted:
> [inputStreamCondition signal];
> break;
> default:
> break;
> }
> }
>
> - (void)handleOutputStreamEvent:(NSStreamEvent)eventCode {
> switch (eventCode) {
> case NSStreamEventOpenCompleted:
> [outputStreamCondition signal];
> break;
> default:
> break;
> }
> }
>
> As can be seen, I am using a NSCondition to block the main thread until I
> receive both NSStreamEventOpenCompleted events.
>
> I have a feeling that this code smells a bit, and was wondering if someone
> who's done some networking programming has recommendations.
>
>
> Grateful as always,
> Richard
> _______________________________________________
>
> Cocoa-dev mailing list ([email protected])
>
> 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:
> http://lists.apple.com/mailman/options/cocoa-dev/almightylinuxgod%40me.com
>
> This email sent to [email protected]
_______________________________________________
Cocoa-dev mailing list ([email protected])
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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]