PortMapper/MYDNSService.h
author Dan Preston <danpreston@codechemistry.com>
Tue May 05 15:27:20 2009 -0700 (2009-05-05)
changeset 40 423c134d3205
parent 31 1d6924779df7
child 50 63baa74c903f
permissions -rw-r--r--
Tweaked release to be immediate instead of on autorelease pool.
     1 //
     2 //  MYDNSService.h
     3 //  MYNetwork
     4 //
     5 //  Created by Jens Alfke on 4/23/09.
     6 //  Copyright 2009 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 #import <CoreFoundation/CFSocket.h>
    11 @class MYDNSConnection;
    12 
    13 
    14 /** Abstract superclass for services based on DNSServiceRefs, such as MYPortMapper. */
    15 @interface MYDNSService : NSObject
    16 {
    17     @private
    18     BOOL _usePrivateConnection;
    19     MYDNSConnection *_connection;
    20     struct _DNSServiceRef_t *_serviceRef;
    21     CFSocketRef _socket;
    22     CFRunLoopSourceRef _socketSource;
    23     SInt32 _error;
    24     BOOL _continuous, _gotResponse;
    25 }
    26 
    27 /** If NO (the default), the service will stop after it gets a result.
    28     If YES, it will continue to run until stopped. */
    29 @property BOOL continuous;
    30 
    31 /** Starts the service.
    32     Returns immediately; you can find out when the service actually starts (or fails to)
    33     by observing the isOpen and error properties.
    34     It's very unlikely that this call itself will fail (return NO). If it does, it
    35     probably means that the mDNSResponder process isn't working. */
    36 - (BOOL) start;
    37 
    38 /** Stops the service. */
    39 - (void) stop;
    40 
    41 
    42 /** The error status, a DNSServiceErrorType enum; nonzero if something went wrong. 
    43     This property is KV observable. */
    44 @property int32_t error;
    45 
    46 
    47 /** Utility to construct a service's full name. */
    48 + (NSString*) fullNameOfService: (NSString*)serviceName
    49                          ofType: (NSString*)type
    50                        inDomain: (NSString*)domain;
    51 
    52 
    53 /** @name Protected
    54  *  Methods for use only by subclasses
    55  */
    56 //@{
    57 
    58 /** Normally, all DNSService objects use a shared IPC connection to the mDNSResponder daemon.
    59     If an instance wants to use its own connection instead, it can set this property to YES before
    60     it starts. If it does so, it must NOT set the kDNSServiceFlagsShareConnection flag when creating
    61     its underlying DNSService.
    62     This functionality is only provided because MYBonjourRegistration needs it -- there's a bug
    63     that prevents DNSServiceUpdateRecord from working with a shared connection. */
    64 @property BOOL usePrivateConnection;
    65 
    66 /** Subclass must implement this abstract method to create a new DNSServiceRef.
    67     This method is called by -open.
    68     The implementation MUST pass the given sdRefPtr directly to the DNSService function
    69     that creates the new ref, without setting it to NULL first.
    70     It MUST also set the kDNSServiceFlagsShareConnection flag, unless it's already set the
    71     usePrivateConnection property. */
    72 - (int32_t/*DNSServiceErrorType*/) createServiceRef: (struct _DNSServiceRef_t**)sdRefPtr;
    73 
    74 /** Subclass's callback must call this method after doing its own work.
    75     This method will update the error state, and will stop the service if it's not set to be
    76     continuous. */
    77 - (void) gotResponse: (int32_t/*DNSServiceErrorType*/)errorCode;
    78 
    79 /** The underlying DNSServiceRef. This will be NULL except while the service is running. */
    80 @property (readonly) struct _DNSServiceRef_t* serviceRef;
    81 
    82 /** Same as -stop, but does not clear the error property.
    83     (The stop method actually calls this first.) */
    84 - (void) cancel;
    85 
    86 /** Block until a message is received from the daemon.
    87     This will cause the service's callback (defined by the subclass) to be invoked.
    88     @return  YES if a message is received, NO on error (or if the service isn't started) */
    89 - (BOOL) waitForReply;
    90 
    91 //@}
    92 
    93 @end
    94 
    95 
    96 
    97 
    98 @interface MYDNSConnection : NSObject
    99 {
   100     struct _DNSServiceRef_t* _connectionRef;
   101     CFSocketRef _socket;
   102     CFRunLoopSourceRef _runLoopSource;
   103 }
   104 
   105 + (MYDNSConnection*) sharedConnection;
   106 - (id) initWithServiceRef: (struct _DNSServiceRef_t *)serviceRef;
   107 @property (readonly) struct _DNSServiceRef_t* connectionRef;
   108 - (BOOL) processResult;
   109 - (void) close;
   110 
   111 @end