TCP/TCPListener.h
author Jens Alfke <jens@mooseyard.com>
Sat May 24 13:26:02 2008 -0700 (2008-05-24)
changeset 1 8267d5c429c4
child 2 9fdd8dba529c
permissions -rwxr-xr-x
Added #imports of utility headers, so source files will compile without requiring a custom prefix (MYUtilities.pch.)
     1 //
     2 //  TCPListener.m
     3 //  MYNetwork
     4 //
     5 //  Created by Jens Alfke on 5/10/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 
     8 #import "TCPEndpoint.h"
     9 @class TCPConnection, IPAddress;
    10 @protocol TCPListenerDelegate;
    11 
    12 
    13 /** Generic TCP-based server that listens for incoming connections on a port.
    14     For each incoming connection, it creates an instance of (a subclass of) the generic TCP
    15     client class TCPClient. The -connectionClass property lets you customize which subclass
    16     to use.
    17     TCPListener supports Bonjour advertisements for the service, and automatic port renumbering
    18     if there are conflicts. */
    19 @interface TCPListener : TCPEndpoint 
    20 {
    21     @private
    22     uint16_t _port;
    23     BOOL _pickAvailablePort;
    24     BOOL _useIPv6;
    25     CFSocketRef _ipv4socket;
    26     CFSocketRef _ipv6socket;
    27     
    28     NSString *_bonjourServiceType, *_bonjourServiceName;
    29     NSNetService *_netService;
    30     NSDictionary *_bonjourTXTRecord;
    31     BOOL _bonjourPublished;
    32     NSInteger /*NSNetServicesError*/ _bonjourError;
    33 
    34     Class _connectionClass;
    35 }
    36 
    37 /** Initializes a new TCPListener that will listen on the given port when opened. */
    38 - (id) initWithPort: (UInt16)port;
    39 
    40 /** The subclass of TCPConnection that will be instantiated. */
    41 @property Class connectionClass;
    42 
    43 @property (assign) id<TCPListenerDelegate> delegate;
    44 
    45 /** Should the server listen for IPv6 connections (on the same port number)? Defaults to NO. */
    46 @property BOOL useIPv6;
    47 
    48 /** The port number to listen on.
    49     If the pickAvailablePort property is enabled, this value may be updated after the server opens
    50     to reflect the actual port number being used. */
    51 @property uint16_t port;
    52 
    53 /** Should the server pick a higher port number if the desired port is already in use?
    54     Defaults to NO. If enabled, the port number will be incremented until a free port is found. */
    55 @property BOOL pickAvailablePort;
    56 
    57 /** Opens the server. You must call this after configuring all desired properties (property
    58     changes are ignored while the server is open.) */
    59 - (BOOL) open: (NSError **)error;
    60 
    61 - (BOOL) open;
    62 
    63 /** Closes the server. */
    64 - (void) close;
    65 
    66 /** Is the server currently open? */
    67 @property (readonly) BOOL isOpen;
    68 
    69 
    70 #pragma mark BONJOUR:
    71 
    72 /** The Bonjour service type to advertise. Defaults to nil; setting it implicitly enables Bonjour.
    73     The value should look like e.g. "_http._tcp."; for details, see the NSNetService documentation. */
    74 @property (copy) NSString *bonjourServiceType;
    75 
    76 /** The Bonjour service name to advertise. Defaults to nil, meaning that a default name will be
    77     automatically generated if Bonjour is enabled (by setting -bonjourServiceType). */
    78 @property (copy) NSString *bonjourServiceName;
    79 
    80 /** The dictionary form of the Bonjour TXT record: metadata about the service that can be browsed
    81     by peers. Changes to this dictionary will be pushed in near-real-time to interested peers. */
    82 @property (copy) NSDictionary *bonjourTXTRecord;
    83 
    84 /** Is this service currently published/advertised via Bonjour? */
    85 @property (readonly) BOOL bonjourPublished;
    86 
    87 /** Current error status of Bonjour service advertising. See NSNetServicesError for error codes. */
    88 @property (readonly) NSInteger /*NSNetServicesError*/ bonjourError;
    89 
    90 
    91 @end
    92 
    93 
    94 
    95 #pragma mark -
    96 
    97 /** The delegate messages sent by TCPListener. */
    98 @protocol TCPListenerDelegate <NSObject>
    99 
   100 - (void) listener: (TCPListener*)listener didAcceptConnection: (TCPConnection*)connection;
   101 
   102 @optional
   103 - (void) listenerDidOpen: (TCPListener*)listener;
   104 - (void) listener: (TCPListener*)listener failedToOpen: (NSError*)error;
   105 - (void) listenerDidClose: (TCPListener*)listener;
   106 - (BOOL) listener: (TCPListener*)listener shouldAcceptConnectionFrom: (IPAddress*)address;
   107 @end