Rewrote the Bonjour classes, using the low-level <dns_sd.h> API. They're now subclasses of MYDNSService.
5 // Created by Jens Alfke on 4/23/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
9 #import "MYDNSService.h"
10 #import "CollectionUtils.h"
13 #import "ExceptionUtils.h"
18 static void serviceCallback(CFSocketRef s,
19 CFSocketCallBackType type,
22 void *clientCallBackInfo);
25 @implementation MYDNSService
30 Log(@"DEALLOC %@ %p", self.class,self);
44 - (DNSServiceErrorType) error {
48 - (void) setError: (DNSServiceErrorType)error {
50 Warn(@"%@ error := %i", self,error);
55 @synthesize continuous=_continuous, serviceRef=_serviceRef;
58 - (DNSServiceRef) createServiceRef {
59 AssertAbstractMethod();
66 return YES; // already started
71 // Ask the subclass to create a DNSServiceRef:
72 _serviceRef = [self createServiceRef];
75 // Wrap a CFSocket around the service's socket:
76 CFSocketContext ctxt = { 0, self, CFRetain, CFRelease, NULL };
77 _socket = CFSocketCreateWithNative(NULL,
78 DNSServiceRefSockFD(_serviceRef),
79 kCFSocketReadCallBack,
80 &serviceCallback, &ctxt);
82 CFSocketSetSocketFlags(_socket, CFSocketGetSocketFlags(_socket) & ~kCFSocketCloseOnInvalidate);
83 // Attach the socket to the runloop so the serviceCallback will be invoked:
84 _socketSource = CFSocketCreateRunLoopSource(NULL, _socket, 0);
86 CFRunLoopAddSource(CFRunLoopGetCurrent(), _socketSource, kCFRunLoopCommonModes);
87 LogTo(DNS,@"Opening %@ -- service=%p",self,_serviceRef);
88 return YES; // success
93 self.error = kDNSServiceErr_Unknown;
94 LogTo(DNS,@"Failed to open %@ -- err=%i",self,_error);
102 [self retain]; // Prevents _socket's dealloc from releasing & deallocing me!
103 if( _socketSource ) {
104 CFRunLoopSourceInvalidate(_socketSource);
105 CFRelease(_socketSource);
106 _socketSource = NULL;
109 CFSocketInvalidate(_socket);
114 LogTo(DNS,@"Stopped %@",self);
115 DNSServiceRefDeallocate(_serviceRef);
130 - (BOOL) priv_processResult
133 DNSServiceErrorType err = DNSServiceProcessResult(_serviceRef);
135 // An error here means the socket has failed and should be closed.
146 - (BOOL) waitForReply
150 LogTo(DNS,@"Waiting for %@ ...", self);
151 BOOL ok = [self priv_processResult];
152 LogTo(DNS,@" ...done waiting");
157 /** CFSocket callback, informing us that _socket has data available, which means
158 that the DNS service has an incoming result to be processed. This will end up invoking
159 the service's specific callback. */
160 static void serviceCallback(CFSocketRef s,
161 CFSocketCallBackType type,
162 CFDataRef address, const void *data, void *clientCallBackInfo)
164 NSAutoreleasePool *pool = [NSAutoreleasePool new];
166 [(MYDNSService*)clientCallBackInfo priv_processResult];
167 }catchAndReport(@"PortMapper serviceCallback");
176 Copyright (c) 2008-2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
178 Redistribution and use in source and binary forms, with or without modification, are permitted
179 provided that the following conditions are met:
181 * Redistributions of source code must retain the above copyright notice, this list of conditions
182 and the following disclaimer.
183 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
184 and the following disclaimer in the documentation and/or other materials provided with the
187 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
188 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
189 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
190 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
191 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
192 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
193 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
194 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.