TCP/TCPStream.h
author Jens Alfke <jens@mooseyard.com>
Sat May 24 17:25:06 2008 -0700 (2008-05-24)
changeset 2 9fdd8dba529c
parent 0 9d67172bb323
child 7 5936db2c1987
permissions -rw-r--r--
* Added more documentation.
* Minor API changes.
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@0
    10
@class TCPConnection, TCPWriter;
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@0
    23
/** The connection's security level as reported by the underlying CFStream. */
jens@0
    24
@property (readonly) NSString *securityLevel;
jens@0
    25
jens@0
    26
/** The SSL property dictionary for the CFStream. */
jens@0
    27
@property (copy) NSDictionary* SSLProperties;
jens@0
    28
jens@0
    29
/** The SSL certificate(s) of the peer, if any. */
jens@0
    30
@property (readonly) NSArray *peerSSLCerts;
jens@0
    31
jens@0
    32
/** Opens the stream. */
jens@0
    33
- (void) open;
jens@0
    34
jens@0
    35
/** Disconnects abruptly. */
jens@0
    36
- (void) disconnect;
jens@0
    37
jens@0
    38
/** Closes the stream politely, waiting until there's no data pending. */
jens@0
    39
- (BOOL) close;
jens@0
    40
jens@0
    41
/** Is the stream open? */
jens@0
    42
@property (readonly) BOOL isOpen;
jens@0
    43
jens@0
    44
/** Does the stream have pending data to read or write, that prevents it from closing? */
jens@0
    45
@property (readonly) BOOL isBusy;
jens@0
    46
jens@0
    47
/** Generic accessor for CFStream/NSStream properties. */
jens@0
    48
- (id) propertyForKey: (CFStringRef)cfStreamProperty;
jens@0
    49
jens@0
    50
/** Generic accessor for CFStream/NSStream properties. */
jens@0
    51
- (void) setProperty: (id)value forKey: (CFStringRef)cfStreamProperty;
jens@0
    52
jens@0
    53
@end
jens@0
    54
jens@0
    55
jens@0
    56
/** Input stream for a TCPConnection. */
jens@0
    57
@interface TCPReader : TCPStream
jens@2
    58
jens@0
    59
/** The connection's TCPWriter. */
jens@0
    60
@property (readonly) TCPWriter *writer;
jens@2
    61
jens@2
    62
/** Reads bytes from the stream, like the corresponding method of NSInputStream.
jens@2
    63
    The number of bytes actually read is returned, or zero if no data is available.
jens@2
    64
    If an error occurs, it will call its -_gotError method, and return a negative number. */
jens@2
    65
- (NSInteger) read: (void*)dst maxLength: (NSUInteger)maxLength;
jens@2
    66
jens@0
    67
@end
jens@0
    68
jens@0
    69
jens@0
    70
jens@0
    71
@interface TCPStream (Protected)
jens@0
    72
/** Called when the stream opens. */
jens@0
    73
- (void) _opened;
jens@0
    74
jens@0
    75
/** Called when the stream has bytes available to read. */
jens@0
    76
- (void) _canRead;
jens@0
    77
jens@0
    78
/** Called when the stream has space available in its output buffer to write to. */
jens@0
    79
- (void) _canWrite;
jens@0
    80
 
jens@0
    81
/** Called when the underlying stream closes due to the socket closing. */
jens@0
    82
- (void) _gotEOF;
jens@0
    83
jens@0
    84
/** Call this if a read/write call returns -1 to report an error;
jens@0
    85
    it will look up the error from the NSStream and call gotError: with it.
jens@0
    86
    This method always returns NO, so you can "return [self _gotError]". */
jens@0
    87
- (BOOL) _gotError;
jens@0
    88
jens@0
    89
/** Signals a fatal error to the TCPConnection.
jens@0
    90
    This method always returns NO, so you can "return [self _gotError: e]". */
jens@0
    91
- (BOOL) _gotError: (NSError*)error;
jens@0
    92
@end