TCP/TCPListener.h
changeset 1 8267d5c429c4
child 2 9fdd8dba529c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/TCP/TCPListener.h	Sat May 24 13:26:02 2008 -0700
     1.3 @@ -0,0 +1,107 @@
     1.4 +//
     1.5 +//  TCPListener.m
     1.6 +//  MYNetwork
     1.7 +//
     1.8 +//  Created by Jens Alfke on 5/10/08.
     1.9 +//  Copyright 2008 Jens Alfke. All rights reserved.
    1.10 +
    1.11 +#import "TCPEndpoint.h"
    1.12 +@class TCPConnection, IPAddress;
    1.13 +@protocol TCPListenerDelegate;
    1.14 +
    1.15 +
    1.16 +/** Generic TCP-based server that listens for incoming connections on a port.
    1.17 +    For each incoming connection, it creates an instance of (a subclass of) the generic TCP
    1.18 +    client class TCPClient. The -connectionClass property lets you customize which subclass
    1.19 +    to use.
    1.20 +    TCPListener supports Bonjour advertisements for the service, and automatic port renumbering
    1.21 +    if there are conflicts. */
    1.22 +@interface TCPListener : TCPEndpoint 
    1.23 +{
    1.24 +    @private
    1.25 +    uint16_t _port;
    1.26 +    BOOL _pickAvailablePort;
    1.27 +    BOOL _useIPv6;
    1.28 +    CFSocketRef _ipv4socket;
    1.29 +    CFSocketRef _ipv6socket;
    1.30 +    
    1.31 +    NSString *_bonjourServiceType, *_bonjourServiceName;
    1.32 +    NSNetService *_netService;
    1.33 +    NSDictionary *_bonjourTXTRecord;
    1.34 +    BOOL _bonjourPublished;
    1.35 +    NSInteger /*NSNetServicesError*/ _bonjourError;
    1.36 +
    1.37 +    Class _connectionClass;
    1.38 +}
    1.39 +
    1.40 +/** Initializes a new TCPListener that will listen on the given port when opened. */
    1.41 +- (id) initWithPort: (UInt16)port;
    1.42 +
    1.43 +/** The subclass of TCPConnection that will be instantiated. */
    1.44 +@property Class connectionClass;
    1.45 +
    1.46 +@property (assign) id<TCPListenerDelegate> delegate;
    1.47 +
    1.48 +/** Should the server listen for IPv6 connections (on the same port number)? Defaults to NO. */
    1.49 +@property BOOL useIPv6;
    1.50 +
    1.51 +/** The port number to listen on.
    1.52 +    If the pickAvailablePort property is enabled, this value may be updated after the server opens
    1.53 +    to reflect the actual port number being used. */
    1.54 +@property uint16_t port;
    1.55 +
    1.56 +/** Should the server pick a higher port number if the desired port is already in use?
    1.57 +    Defaults to NO. If enabled, the port number will be incremented until a free port is found. */
    1.58 +@property BOOL pickAvailablePort;
    1.59 +
    1.60 +/** Opens the server. You must call this after configuring all desired properties (property
    1.61 +    changes are ignored while the server is open.) */
    1.62 +- (BOOL) open: (NSError **)error;
    1.63 +
    1.64 +- (BOOL) open;
    1.65 +
    1.66 +/** Closes the server. */
    1.67 +- (void) close;
    1.68 +
    1.69 +/** Is the server currently open? */
    1.70 +@property (readonly) BOOL isOpen;
    1.71 +
    1.72 +
    1.73 +#pragma mark BONJOUR:
    1.74 +
    1.75 +/** The Bonjour service type to advertise. Defaults to nil; setting it implicitly enables Bonjour.
    1.76 +    The value should look like e.g. "_http._tcp."; for details, see the NSNetService documentation. */
    1.77 +@property (copy) NSString *bonjourServiceType;
    1.78 +
    1.79 +/** The Bonjour service name to advertise. Defaults to nil, meaning that a default name will be
    1.80 +    automatically generated if Bonjour is enabled (by setting -bonjourServiceType). */
    1.81 +@property (copy) NSString *bonjourServiceName;
    1.82 +
    1.83 +/** The dictionary form of the Bonjour TXT record: metadata about the service that can be browsed
    1.84 +    by peers. Changes to this dictionary will be pushed in near-real-time to interested peers. */
    1.85 +@property (copy) NSDictionary *bonjourTXTRecord;
    1.86 +
    1.87 +/** Is this service currently published/advertised via Bonjour? */
    1.88 +@property (readonly) BOOL bonjourPublished;
    1.89 +
    1.90 +/** Current error status of Bonjour service advertising. See NSNetServicesError for error codes. */
    1.91 +@property (readonly) NSInteger /*NSNetServicesError*/ bonjourError;
    1.92 +
    1.93 +
    1.94 +@end
    1.95 +
    1.96 +
    1.97 +
    1.98 +#pragma mark -
    1.99 +
   1.100 +/** The delegate messages sent by TCPListener. */
   1.101 +@protocol TCPListenerDelegate <NSObject>
   1.102 +
   1.103 +- (void) listener: (TCPListener*)listener didAcceptConnection: (TCPConnection*)connection;
   1.104 +
   1.105 +@optional
   1.106 +- (void) listenerDidOpen: (TCPListener*)listener;
   1.107 +- (void) listener: (TCPListener*)listener failedToOpen: (NSError*)error;
   1.108 +- (void) listenerDidClose: (TCPListener*)listener;
   1.109 +- (BOOL) listener: (TCPListener*)listener shouldAcceptConnectionFrom: (IPAddress*)address;
   1.110 +@end