jens@0: // jens@0: // TCPStream.h jens@0: // MYNetwork jens@0: // jens@0: // Created by Jens Alfke on 5/10/08. jens@0: // Copyright 2008 Jens Alfke. All rights reserved. jens@0: // jens@0: jens@0: #import jens@7: @class TCPConnection, TCPWriter, IPAddress; jens@0: jens@0: jens@2: /** Abstract superclass for data streams, used by TCPConnection. */ jens@0: @interface TCPStream : NSObject jens@0: { jens@0: TCPConnection *_conn; jens@0: NSStream *_stream; jens@0: BOOL _shouldClose; jens@0: } jens@0: jens@0: - (id) initWithConnection: (TCPConnection*)conn stream: (NSStream*)stream; jens@0: jens@7: /** The IP address this stream is connected to. */ jens@7: @property (readonly) IPAddress *peerAddress; jens@7: jens@0: /** The connection's security level as reported by the underlying CFStream. */ jens@0: @property (readonly) NSString *securityLevel; jens@0: jens@0: /** The SSL property dictionary for the CFStream. */ jens@0: @property (copy) NSDictionary* SSLProperties; jens@0: jens@0: /** The SSL certificate(s) of the peer, if any. */ jens@0: @property (readonly) NSArray *peerSSLCerts; jens@0: jens@0: /** Opens the stream. */ jens@0: - (void) open; jens@0: jens@0: /** Disconnects abruptly. */ jens@0: - (void) disconnect; jens@0: jens@0: /** Closes the stream politely, waiting until there's no data pending. */ jens@0: - (BOOL) close; jens@0: jens@0: /** Is the stream open? */ jens@0: @property (readonly) BOOL isOpen; jens@0: jens@0: /** Does the stream have pending data to read or write, that prevents it from closing? */ jens@0: @property (readonly) BOOL isBusy; jens@0: jens@0: /** Generic accessor for CFStream/NSStream properties. */ jens@0: - (id) propertyForKey: (CFStringRef)cfStreamProperty; jens@0: jens@0: /** Generic accessor for CFStream/NSStream properties. */ jens@0: - (void) setProperty: (id)value forKey: (CFStringRef)cfStreamProperty; jens@0: jens@0: @end jens@0: jens@0: jens@0: /** Input stream for a TCPConnection. */ jens@0: @interface TCPReader : TCPStream jens@2: jens@0: /** The connection's TCPWriter. */ jens@0: @property (readonly) TCPWriter *writer; jens@2: jens@2: /** Reads bytes from the stream, like the corresponding method of NSInputStream. jens@2: The number of bytes actually read is returned, or zero if no data is available. jens@2: If an error occurs, it will call its -_gotError method, and return a negative number. */ jens@2: - (NSInteger) read: (void*)dst maxLength: (NSUInteger)maxLength; jens@2: jens@0: @end jens@0: jens@0: jens@0: jens@0: @interface TCPStream (Protected) jens@0: /** Called when the stream opens. */ jens@0: - (void) _opened; jens@0: jens@0: /** Called when the stream has bytes available to read. */ jens@0: - (void) _canRead; jens@0: jens@0: /** Called when the stream has space available in its output buffer to write to. */ jens@0: - (void) _canWrite; jens@0: jens@0: /** Called when the underlying stream closes due to the socket closing. */ jens@0: - (void) _gotEOF; jens@0: jens@0: /** Call this if a read/write call returns -1 to report an error; jens@0: it will look up the error from the NSStream and call gotError: with it. jens@0: This method always returns NO, so you can "return [self _gotError]". */ jens@0: - (BOOL) _gotError; jens@0: jens@0: /** Signals a fatal error to the TCPConnection. jens@0: This method always returns NO, so you can "return [self _gotError: e]". */ jens@0: - (BOOL) _gotError: (NSError*)error; jens@0: @end