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