TCP/TCPStream.h
author Jens Alfke <jens@mooseyard.com>
Fri Jul 24 14:06:28 2009 -0700 (2009-07-24)
changeset 63 5e4855a592ee
parent 7 5936db2c1987
permissions -rw-r--r--
* The BLIPConnection receivedRequest: delegate method now returns BOOL. If the method returns NO (or if the method isn't implemented in the delegate), that means it didn't handle the message at all; an error will be returned to the sender.
* If the connection closes unexpectedly due to an error, then the auto-generated responses to pending requests will contain that error. This makes it easier to display a meaningful error message in the handler for the request.
     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, IPAddress;
    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 IP address this stream is connected to. */
    24 @property (readonly) IPAddress *peerAddress;
    25 
    26 /** The connection's security level as reported by the underlying CFStream. */
    27 @property (readonly) NSString *securityLevel;
    28 
    29 /** The SSL property dictionary for the CFStream. */
    30 @property (copy) NSDictionary* SSLProperties;
    31 
    32 /** The SSL certificate(s) of the peer, if any. */
    33 @property (readonly) NSArray *peerSSLCerts;
    34 
    35 /** Opens the stream. */
    36 - (void) open;
    37 
    38 /** Disconnects abruptly. */
    39 - (void) disconnect;
    40 
    41 /** Closes the stream politely, waiting until there's no data pending. */
    42 - (BOOL) close;
    43 
    44 /** Is the stream open? */
    45 @property (readonly) BOOL isOpen;
    46 
    47 /** Does the stream have pending data to read or write, that prevents it from closing? */
    48 @property (readonly) BOOL isBusy;
    49 
    50 /** Returns NO if the stream is ready to close (-close has been called and -isBusy is NO.) */
    51 @property (readonly) BOOL isActive;
    52 
    53 /** Generic accessor for CFStream/NSStream properties. */
    54 - (id) propertyForKey: (CFStringRef)cfStreamProperty;
    55 
    56 /** Generic accessor for CFStream/NSStream properties. */
    57 - (void) setProperty: (id)value forKey: (CFStringRef)cfStreamProperty;
    58 
    59 @end
    60 
    61 
    62 /** Input stream for a TCPConnection. */
    63 @interface TCPReader : TCPStream
    64 
    65 /** The connection's TCPWriter. */
    66 @property (readonly) TCPWriter *writer;
    67 
    68 /** Reads bytes from the stream, like the corresponding method of NSInputStream.
    69     The number of bytes actually read is returned, or zero if no data is available.
    70     If an error occurs, it will call its -_gotError method, and return a negative number. */
    71 - (NSInteger) read: (void*)dst maxLength: (NSUInteger)maxLength;
    72 
    73 @end
    74 
    75 
    76 
    77 @interface TCPStream (Protected)
    78 /** Called when the stream opens. */
    79 - (void) _opened;
    80 
    81 /** Called when the stream has bytes available to read. */
    82 - (void) _canRead;
    83 
    84 /** Called when the stream has space available in its output buffer to write to. */
    85 - (void) _canWrite;
    86  
    87 /** Called when the underlying stream closes due to the socket closing. */
    88 - (void) _gotEOF;
    89 
    90 /** Call this if a read/write call returns -1 to report an error;
    91     it will look up the error from the NSStream and call gotError: with it.
    92     This method always returns NO, so you can "return [self _gotError]". */
    93 - (BOOL) _gotError;
    94 
    95 /** Signals a fatal error to the TCPConnection.
    96     This method always returns NO, so you can "return [self _gotError: e]". */
    97 - (BOOL) _gotError: (NSError*)error;
    98 @end