Browse Source

Merge pull request #487 from gezihuzi/dev_bonjour_extent

Bonjour service support custom txt data
Pierre-Olivier Latour 5 years ago
parent
commit
c6d118f4ec
2 changed files with 31 additions and 0 deletions
  1. 7 0
      GCDWebServer/Core/GCDWebServer.h
  2. 24 0
      GCDWebServer/Core/GCDWebServer.m

+ 7 - 0
GCDWebServer/Core/GCDWebServer.h

@@ -92,6 +92,13 @@ extern NSString* const GCDWebServerOption_Port;
  */
 extern NSString* const GCDWebServerOption_BonjourName;
 
+/**
+*  The Bonjour TXT Data used by the GCDWebServer (NSDictionary<NSString, NSString>).
+*
+*  The default value is nil.
+*/
+extern NSString* const GCDWebServerOption_BonjourTXTData;
+
 /**
  *  The Bonjour service type used by the GCDWebServer (NSString).
  *

+ 24 - 0
GCDWebServer/Core/GCDWebServer.m

@@ -53,6 +53,7 @@
 NSString* const GCDWebServerOption_Port = @"Port";
 NSString* const GCDWebServerOption_BonjourName = @"BonjourName";
 NSString* const GCDWebServerOption_BonjourType = @"BonjourType";
+NSString* const GCDWebServerOption_BonjourTXTData = @"BonjourTXTData";
 NSString* const GCDWebServerOption_RequestNATPortMapping = @"RequestNATPortMapping";
 NSString* const GCDWebServerOption_BindToLocalhost = @"BindToLocalhost";
 NSString* const GCDWebServerOption_MaxPendingConnections = @"MaxPendingConnections";
@@ -590,6 +591,29 @@ static inline NSString* _EncodeBase64(NSString* string) {
       CFNetServiceSetClient(_registrationService, _NetServiceRegisterCallBack, &context);
       CFNetServiceScheduleWithRunLoop(_registrationService, CFRunLoopGetMain(), kCFRunLoopCommonModes);
       CFStreamError streamError = {0};
+      
+      NSDictionary* txtDataDictionary = _GetOption(_options, GCDWebServerOption_BonjourTXTData, nil);
+      if (txtDataDictionary != nil) {
+        NSUInteger count = txtDataDictionary.count;
+        CFStringRef keys[count];
+        CFStringRef values[count];
+        NSUInteger index = 0;
+        for (NSString *key in txtDataDictionary) {
+          NSString *value = txtDataDictionary[key];
+          keys[index] = (__bridge CFStringRef)(key);
+          values[index] = (__bridge CFStringRef)(value);
+          index ++;
+        }
+        CFDictionaryRef txtDictionary = CFDictionaryCreate(CFAllocatorGetDefault(), (void *)keys, (void *)values, count, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
+        if (txtDictionary != NULL) {
+          CFDataRef txtData = CFNetServiceCreateTXTDataWithDictionary(nil, txtDictionary);
+          Boolean setTXTDataResult = CFNetServiceSetTXTData(_registrationService, txtData);
+          if (!setTXTDataResult) {
+            GWS_LOG_ERROR(@"Failed setting TXTData");
+          }
+        }
+      }
+      
       CFNetServiceRegisterWithOptions(_registrationService, 0, &streamError);
 
       _resolutionService = CFNetServiceCreateCopy(kCFAllocatorDefault, _registrationService);