Reputation: 4519
I'm trying to write something like wrapper for NSStream
to make my life easier. I just want to know when is connection established, when is closed, write some data a get received data. So I'm thinking about something like this:
Header:
@interface StreamWrapper : NSObject
- (id)initWithDelegate:(id <StreamWrapperDelegate>)delegate;
- (void)writeData:(NSData *)data;
@end
@protocol StreamWrapperDelegate <NSObject>
@required
- (void)streamWrapper:(StreamWrapper *)streamWrapper didReceiveData:(NSData *)data;
- (void)streamWrapperDidConnect:(StreamWrapper *)streamWrapper;
- (void)streamWrapperDidCloseConnection:(StreamWrapper *)streamWrapper;
@end
Class:
@interface StreamWrapper () <NSStreamDelegate>
...
@property (nonatomic, weak) id <StreamWrapperDelegate> delegate;
@property (nonatomic, strong) NSInputStream *inputStream;
@property (nonatomic, strong) NSOutputStream *outputStream;
- (void)closeStreams;
- (void)setAndOpenStreams;
@end
@implementation StreamWrapper
#pragma mark - NSStreamDelegate
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
switch (eventCode) {
case NSStreamEventEndEncountered:
// Should I here close both streams (even if this is called only for one (in or out)) and delegate closed connection?
break;
case NSStreamEventErrorOccurred:
// Should I here close both streams (even if this is called only for one (in or out)) and delegate closed connection?
break;
case NSStreamEventHasBytesAvailable:
// My reading algorithm...
[self.delegate streamWrapper:self didReceiveData:data];
break;
case NSStreamEventHasSpaceAvailable:
// Is this useful for me?
break;
case NSStreamEventNone:
// Is this useful for me?
break;
case NSStreamEventOpenCompleted:
// Should I here delegate successful connection? Should I wait to receive this for both streams? How?
break;
default:
break;
}
}
...
@end
So... how to implement that I will always know when is connection established and I'm able to send and receive data and when is connection broken (even only one way) and I should close it and try for brand new establishment? Or what is proper way to do something like this?
Upvotes: 3
Views: 798
Reputation: 433
My previous experience with NSStream is to capture the first NSStreamEventHasSpaceAvailable event as the signal for connection establishment.
I also nil out the input and output streams when Error or Completed message arise - so I only need to check if these streams are nil in the writeData: method.
Upvotes: 0
Reputation: 1483
According To Apple's Developer Website
NSStreamEventNone
No event has occurred.
Available in OS X v10.3 and later.
Declared in NSStream.h.
NSStreamEventOpenCompleted
The open has completed successfully.
Available in OS X v10.3 and later.
Declared in NSStream.h.
NSStreamEventHasBytesAvailable
The stream has bytes to be read.
Available in OS X v10.3 and later.
Declared in NSStream.h.
NSStreamEventHasSpaceAvailable
The stream can accept bytes for writing.
Available in OS X v10.3 and later.
Declared in NSStream.h.
NSStreamEventErrorOccurred
An error has occurred on the stream.
Available in OS X v10.3 and later.
Declared in NSStream.h.
NSStreamEventEndEncountered
The end of the stream has been reached.
Available in OS X v10.3 and later.
Declared in NSStream.h.
Article : Byte-available event
Article : space-available event
Upvotes: 2