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