TCP/TCPStream.h
author Dan Preston <danpreston@codechemistry.com>
Tue May 05 15:40:36 2009 -0700 (2009-05-05)
changeset 41 0fa1fcf07d87
parent 7 5936db2c1987
permissions -rw-r--r--
Fixed a small leak of an NSString object.
jens@0
     1
//
jens@0
     2
//  TCPStream.h
jens@0
     3
//  MYNetwork
jens@0
     4
//
jens@0
     5
//  Created by Jens Alfke on 5/10/08.
jens@0
     6
//  Copyright 2008 Jens Alfke. All rights reserved.
jens@0
     7
//
jens@0
     8
jens@0
     9
#import <Foundation/Foundation.h>
jens@7
    10
@class TCPConnection, TCPWriter, IPAddress;
jens@0
    11
jens@0
    12
jens@2
    13
/** Abstract superclass for data streams, used by TCPConnection. */
jens@0
    14
@interface TCPStream : NSObject 
jens@0
    15
{
jens@0
    16
    TCPConnection *_conn;
jens@0
    17
    NSStream *_stream;
jens@0
    18
    BOOL _shouldClose;
jens@0
    19
}
jens@0
    20
jens@0
    21
- (id) initWithConnection: (TCPConnection*)conn stream: (NSStream*)stream;
jens@0
    22
jens@7
    23
/** The IP address this stream is connected to. */
jens@7
    24
@property (readonly) IPAddress *peerAddress;
jens@7
    25
jens@0
    26
/** The connection's security level as reported by the underlying CFStream. */
jens@0
    27
@property (readonly) NSString *securityLevel;
jens@0
    28
jens@0
    29
/** The SSL property dictionary for the CFStream. */
jens@0
    30
@property (copy) NSDictionary* SSLProperties;
jens@0
    31
jens@0
    32
/** The SSL certificate(s) of the peer, if any. */
jens@0
    33
@property (readonly) NSArray *peerSSLCerts;
jens@0
    34
jens@0
    35
/** Opens the stream. */
jens@0
    36
- (void) open;
jens@0
    37
jens@0
    38
/** Disconnects abruptly. */
jens@0
    39
- (void) disconnect;
jens@0
    40
jens@0
    41
/** Closes the stream politely, waiting until there's no data pending. */
jens@0
    42
- (BOOL) close;
jens@0
    43
jens@0
    44
/** Is the stream open? */
jens@0
    45
@property (readonly) BOOL isOpen;
jens@0
    46
jens@0
    47
/** Does the stream have pending data to read or write, that prevents it from closing? */
jens@0
    48
@property (readonly) BOOL isBusy;
jens@0
    49
jens@18
    50
/** Returns NO if the stream is ready to close (-close has been called and -isBusy is NO.) */
jens@18
    51
@property (readonly) BOOL isActive;
jens@18
    52
jens@0
    53
/** Generic accessor for CFStream/NSStream properties. */
jens@0
    54
- (id) propertyForKey: (CFStringRef)cfStreamProperty;
jens@0
    55
jens@0
    56
/** Generic accessor for CFStream/NSStream properties. */
jens@0
    57
- (void) setProperty: (id)value forKey: (CFStringRef)cfStreamProperty;
jens@0
    58
jens@0
    59
@end
jens@0
    60
jens@0
    61
jens@0
    62
/** Input stream for a TCPConnection. */
jens@0
    63
@interface TCPReader : TCPStream
jens@2
    64
jens@0
    65
/** The connection's TCPWriter. */
jens@0
    66
@property (readonly) TCPWriter *writer;
jens@2
    67
jens@2
    68
/** Reads bytes from the stream, like the corresponding method of NSInputStream.
jens@2
    69
    The number of bytes actually read is returned, or zero if no data is available.
jens@2
    70
    If an error occurs, it will call its -_gotError method, and return a negative number. */
jens@2
    71
- (NSInteger) read: (void*)dst maxLength: (NSUInteger)maxLength;
jens@2
    72
jens@0
    73
@end
jens@0
    74
jens@0
    75
jens@0
    76
jens@0
    77
@interface TCPStream (Protected)
jens@0
    78
/** Called when the stream opens. */
jens@0
    79
- (void) _opened;
jens@0
    80
jens@0
    81
/** Called when the stream has bytes available to read. */
jens@0
    82
- (void) _canRead;
jens@0
    83
jens@0
    84
/** Called when the stream has space available in its output buffer to write to. */
jens@0
    85
- (void) _canWrite;
jens@0
    86
 
jens@0
    87
/** Called when the underlying stream closes due to the socket closing. */
jens@0
    88
- (void) _gotEOF;
jens@0
    89
jens@0
    90
/** Call this if a read/write call returns -1 to report an error;
jens@0
    91
    it will look up the error from the NSStream and call gotError: with it.
jens@0
    92
    This method always returns NO, so you can "return [self _gotError]". */
jens@0
    93
- (BOOL) _gotError;
jens@0
    94
jens@0
    95
/** Signals a fatal error to the TCPConnection.
jens@0
    96
    This method always returns NO, so you can "return [self _gotError: e]". */
jens@0
    97
- (BOOL) _gotError: (NSError*)error;
jens@0
    98
@end