Permalink
Browse files

[WIN] speed up detection of a missing/stopped Bonjour service

  • Loading branch information...
1 parent a95ff30 commit 2933c975a160c4003465c2cd5d4e68ea35612e41 @CrystalP committed Jan 5, 2012
Showing with 43 additions and 9 deletions.
  1. +35 −8 xbmc/network/windows/ZeroconfWIN.cpp
  2. +8 −1 xbmc/network/windows/ZeroconfWIN.h
@@ -29,6 +29,12 @@
#pragma comment(lib, "dnssd.lib")
+#include <winsock2.h>
+#pragma comment(lib, "ws2_32.lib")
+
+#define MDNS_TCP_SERVERADDR "127.0.0.1"
+#define MDNS_TCP_SERVERPORT 5354
+
CZeroconfWIN::CZeroconfWIN()
{
}
@@ -38,6 +44,24 @@ CZeroconfWIN::~CZeroconfWIN()
doStop();
}
+DNSServiceStatus CZeroconfWIN::IsmDNSResponderUp()
+{
+ SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
+
+ if (s == INVALID_SOCKET)
+ return DNSServiceStatus_Unknown;
+
+ sockaddr_in saddr;
+ saddr.sin_family = AF_INET;
+ saddr.sin_addr.s_addr = inet_addr(MDNS_TCP_SERVERADDR);
+ saddr.sin_port = htons(MDNS_TCP_SERVERPORT);
+
+ int err = connect(s, (struct sockaddr *) &saddr, sizeof(saddr));
+ closesocket(s);
+
+ return (err == 0) ? DNSServiceStatus_Up : DNSServiceStatus_Down;
+ // alternative detection method: the service creates a window of class name "Bonjour Hidden Window %d", with %d the process id
+}
//methods to implement for concrete implementations
bool CZeroconfWIN::doPublishService(const std::string& fcr_identifier,
@@ -63,26 +87,29 @@ bool CZeroconfWIN::doPublishService(const std::string& fcr_identifier,
}
}
- DNSServiceErrorType err = DNSServiceRegister(&netService, 0, 0, fcr_name.c_str(), fcr_type.c_str(), NULL, NULL, htons(f_port), TXTRecordGetLength(&txtRecord), TXTRecordGetBytesPtr(&txtRecord), registerCallback, NULL);
-
- if(err != kDNSServiceErr_ServiceNotRunning)
- DNSServiceProcessResult(netService);
+ DNSServiceErrorType err = kDNSServiceErr_ServiceNotRunning;
- if (err != kDNSServiceErr_NoError)
+ if (IsmDNSResponderUp() == DNSServiceStatus_Down
+ || (err = DNSServiceRegister(&netService, 0, 0, fcr_name.c_str(), fcr_type.c_str(), NULL, NULL, htons(f_port), TXTRecordGetLength(&txtRecord), TXTRecordGetBytesPtr(&txtRecord), registerCallback, NULL)) != kDNSServiceErr_NoError)
{
// Something went wrong so lets clean up.
if (netService)
DNSServiceRefDeallocate(netService);
- CLog::Log(LOGERROR, "CZeroconfWIN::doPublishService CFNetServiceRegister returned (error = %ld)\n", (int) err);
+ CLog::Log(LOGERROR, __FUNCTION__ " CFNetServiceRegister returned (error = %ld)\n", (int) err);
if(err == kDNSServiceErr_ServiceNotRunning)
{
- CLog::Log(LOGERROR, "CZeroconfWIN: Zeroconf can't be started probably because Apple's Bonjour Service isn't installed. You can get it by either installing Itunes or Apple's Bonjour Print Service for Windows (http://support.apple.com/kb/DL999)");
+ CLog::Log(LOGERROR, "ZeroconfWIN: Zeroconf can't be started probably because Apple's Bonjour Service isn't installed. You can get it by either installing Itunes or Apple's Bonjour Print Service for Windows (http://support.apple.com/kb/DL999)");
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Error, "Failed to start zeroconf", "Is Apple's Bonjour Service installed? See log for more info.", 10000, true);
}
- }
+ }
else
{
+ err = DNSServiceProcessResult(netService);
+
+ if (err != kDNSServiceErr_NoError)
+ CLog::Log(LOGERROR, __FUNCTION__ " DNSServiceProcessResult returned (error = %ld)\n", (int) err);
+
CSingleLock lock(m_data_guard);
m_services.insert(make_pair(fcr_identifier, netService));
}
@@ -26,6 +26,13 @@
#include "threads/CriticalSection.h"
#include <dns_sd.h>
+typedef enum
+{
+ DNSServiceStatus_Unknown,
+ DNSServiceStatus_Down,
+ DNSServiceStatus_Up
+} DNSServiceStatus;
+
class CZeroconfWIN : public CZeroconf
{
public:
@@ -46,7 +53,7 @@ class CZeroconfWIN : public CZeroconf
private:
static void DNSSD_API registerCallback(DNSServiceRef sdref, const DNSServiceFlags flags, DNSServiceErrorType errorCode, const char *name, const char *regtype, const char *domain, void *context);
-
+ static DNSServiceStatus IsmDNSResponderUp();
//lock + data (accessed from runloop(main thread) + the rest)
CCriticalSection m_data_guard;

0 comments on commit 2933c97

Please sign in to comment.