From 2d8dc8775d350d5c8b1be502ae8c7ddeb02ce5d9 Mon Sep 17 00:00:00 2001 From: gezihuzi Date: Sat, 14 Dec 2019 23:02:23 +0800 Subject: [PATCH] Support setting txtData --- GCDWebServer/Core/GCDWebServer.h | 7 +++++++ GCDWebServer/Core/GCDWebServer.m | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/GCDWebServer/Core/GCDWebServer.h b/GCDWebServer/Core/GCDWebServer.h index 70cb70c..d9d2879 100644 --- a/GCDWebServer/Core/GCDWebServer.h +++ b/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). +* +* The default value is nil. +*/ +extern NSString* const GCDWebServerOption_BonjourTXTData; + /** * The Bonjour service type used by the GCDWebServer (NSString). * diff --git a/GCDWebServer/Core/GCDWebServer.m b/GCDWebServer/Core/GCDWebServer.m index cb1b295..8df784c 100644 --- a/GCDWebServer/Core/GCDWebServer.m +++ b/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);