bool CZeroconfBrowserOSX::doRemoveServiceType(const std::string &fcr_service_type) { //search for this browser and remove it from the map CFNetServiceBrowserRef browser = 0; { CSingleLock lock(m_data_guard); tBrowserMap::iterator it = m_service_browsers.find(fcr_service_type); if (it == m_service_browsers.end()) return false; browser = it->second; m_service_browsers.erase(it); } assert(browser); //now kill the browser CFStreamError streamerror; CFNetServiceBrowserStopSearch(browser, &streamerror); CFNetServiceBrowserUnscheduleFromRunLoop(browser, m_runloop, kCFRunLoopCommonModes); CFNetServiceBrowserInvalidate(browser); //remove the services of this browser { CSingleLock lock(m_data_guard); tDiscoveredServicesMap::iterator it = m_discovered_services.find(browser); if (it != m_discovered_services.end()) m_discovered_services.erase(it); } CFRelease(browser); return true; }
bool CZeroconfBrowserOSX::doAddServiceType(const std::string& fcr_service_type) { CFNetServiceClientContext clientContext = { 0, this, NULL, NULL, NULL }; CFStringRef domain = CFSTR(""); CFNetServiceBrowserRef p_browser = CFNetServiceBrowserCreate(kCFAllocatorDefault, CZeroconfBrowserOSX::BrowserCallback, &clientContext); assert(p_browser != NULL); //schedule the browser CFNetServiceBrowserScheduleWithRunLoop(p_browser, m_runloop, kCFRunLoopCommonModes); CFStreamError error; CFStringRef type = CFStringCreateWithCString(NULL, fcr_service_type.c_str(), kCFStringEncodingUTF8); assert(type != NULL); Boolean result = CFNetServiceBrowserSearchForServices(p_browser, domain, type, &error); CFRelease(type); if (result == false) { // Something went wrong so lets clean up. CFNetServiceBrowserUnscheduleFromRunLoop(p_browser, m_runloop, kCFRunLoopCommonModes); CFRelease(p_browser); p_browser = NULL; CLog::Log(LOGERROR, "CFNetServiceBrowserSearchForServices returned" "(domain = %d, error = %" PRId64")", (int)error.domain, (int64_t)error.error); } else { //store the browser CSingleLock lock(m_data_guard); m_service_browsers.insert(std::make_pair(fcr_service_type, p_browser)); } return result; }
void ofxBonjourIp::stopDiscoverService(){ if(netServiceBrowser) { CFNetServiceBrowserInvalidate(netServiceBrowser); CFNetServiceBrowserUnscheduleFromRunLoop(netServiceBrowser, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); CFRelease(netServiceBrowser); //CFNetService(netServiceBrowser); //CFNetServiceSetClient(netService, NULL, NULL); netServiceBrowser = NULL;// /*CFNetServiceUnscheduleFromRunLoop(netService, CFRunLoopGetCurrent(),kCFRunLoopCommonModes); CFNetServiceSetClient(netService, NULL, NULL); CFRelease(netService); netService = NULL;*/ } }
/* static */ Boolean _BrowserBlockUntilComplete(__CFNetServiceBrowser* browser) { // Assume success by default Boolean result = TRUE; CFRunLoopRef rl = CFRunLoopGetCurrent(); // Schedule in the blocking mode. CFNetServiceBrowserScheduleWithRunLoop((CFNetServiceBrowserRef)browser, rl, _kCFNetServiceBrowserBlockingMode); // Lock in order to check for trigger __CFSpinLock(&(browser->_lock)); // Check that search exists. while (browser->_trigger) { // Unlock again so the browser can continue to be processed. __CFSpinUnlock(&(browser->_lock)); // Run the loop in a private mode with it returning whenever a source // has been handled. CFRunLoopRunInMode(_kCFNetServiceBrowserBlockingMode, DBL_MAX, TRUE); // Lock again in preparation for trigger check __CFSpinLock(&(browser->_lock)); } // Fail if there was an error. if (browser->_error.error) result = FALSE; // Unlock the browser again. __CFSpinUnlock(&(browser->_lock)); // Unschedule from the blocking mode CFNetServiceBrowserUnscheduleFromRunLoop((CFNetServiceBrowserRef)browser, rl, _kCFNetServiceBrowserBlockingMode); return result; }