Added #imports of utility headers, so source files will compile without requiring a custom prefix (MYUtilities.pch.)
5 // Created by Jens Alfke on 5/10/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
8 #import "TCPEndpoint.h"
9 @class TCPConnection, IPAddress;
10 @protocol TCPListenerDelegate;
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
17 TCPListener supports Bonjour advertisements for the service, and automatic port renumbering
18 if there are conflicts. */
19 @interface TCPListener : TCPEndpoint
23 BOOL _pickAvailablePort;
25 CFSocketRef _ipv4socket;
26 CFSocketRef _ipv6socket;
28 NSString *_bonjourServiceType, *_bonjourServiceName;
29 NSNetService *_netService;
30 NSDictionary *_bonjourTXTRecord;
31 BOOL _bonjourPublished;
32 NSInteger /*NSNetServicesError*/ _bonjourError;
34 Class _connectionClass;
37 /** Initializes a new TCPListener that will listen on the given port when opened. */
38 - (id) initWithPort: (UInt16)port;
40 /** The subclass of TCPConnection that will be instantiated. */
41 @property Class connectionClass;
43 @property (assign) id<TCPListenerDelegate> delegate;
45 /** Should the server listen for IPv6 connections (on the same port number)? Defaults to NO. */
46 @property BOOL useIPv6;
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;
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;
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;
63 /** Closes the server. */
66 /** Is the server currently open? */
67 @property (readonly) BOOL isOpen;
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;
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;
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;
84 /** Is this service currently published/advertised via Bonjour? */
85 @property (readonly) BOOL bonjourPublished;
87 /** Current error status of Bonjour service advertising. See NSNetServicesError for error codes. */
88 @property (readonly) NSInteger /*NSNetServicesError*/ bonjourError;
97 /** The delegate messages sent by TCPListener. */
98 @protocol TCPListenerDelegate <NSObject>
100 - (void) listener: (TCPListener*)listener didAcceptConnection: (TCPConnection*)connection;
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;