Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit c6d118f

Browse files
authored
Merge pull request #487 from gezihuzi/dev_bonjour_extent
Bonjour service support custom txt data
2 parents 0da9ee6 + f4cf591 commit c6d118f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

GCDWebServer/Core/GCDWebServer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ extern NSString* const GCDWebServerOption_Port;
9292
*/
9393
extern NSString* const GCDWebServerOption_BonjourName;
9494

95+
/**
96+
* The Bonjour TXT Data used by the GCDWebServer (NSDictionary<NSString, NSString>).
97+
*
98+
* The default value is nil.
99+
*/
100+
extern NSString* const GCDWebServerOption_BonjourTXTData;
101+
95102
/**
96103
* The Bonjour service type used by the GCDWebServer (NSString).
97104
*

GCDWebServer/Core/GCDWebServer.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
NSString* const GCDWebServerOption_Port = @"Port";
5454
NSString* const GCDWebServerOption_BonjourName = @"BonjourName";
5555
NSString* const GCDWebServerOption_BonjourType = @"BonjourType";
56+
NSString* const GCDWebServerOption_BonjourTXTData = @"BonjourTXTData";
5657
NSString* const GCDWebServerOption_RequestNATPortMapping = @"RequestNATPortMapping";
5758
NSString* const GCDWebServerOption_BindToLocalhost = @"BindToLocalhost";
5859
NSString* const GCDWebServerOption_MaxPendingConnections = @"MaxPendingConnections";
@@ -590,6 +591,29 @@ - (BOOL)_start:(NSError**)error {
590591
CFNetServiceSetClient(_registrationService, _NetServiceRegisterCallBack, &context);
591592
CFNetServiceScheduleWithRunLoop(_registrationService, CFRunLoopGetMain(), kCFRunLoopCommonModes);
592593
CFStreamError streamError = {0};
594+
595+
NSDictionary* txtDataDictionary = _GetOption(_options, GCDWebServerOption_BonjourTXTData, nil);
596+
if (txtDataDictionary != nil) {
597+
NSUInteger count = txtDataDictionary.count;
598+
CFStringRef keys[count];
599+
CFStringRef values[count];
600+
NSUInteger index = 0;
601+
for (NSString *key in txtDataDictionary) {
602+
NSString *value = txtDataDictionary[key];
603+
keys[index] = (__bridge CFStringRef)(key);
604+
values[index] = (__bridge CFStringRef)(value);
605+
index ++;
606+
}
607+
CFDictionaryRef txtDictionary = CFDictionaryCreate(CFAllocatorGetDefault(), (void *)keys, (void *)values, count, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
608+
if (txtDictionary != NULL) {
609+
CFDataRef txtData = CFNetServiceCreateTXTDataWithDictionary(nil, txtDictionary);
610+
Boolean setTXTDataResult = CFNetServiceSetTXTData(_registrationService, txtData);
611+
if (!setTXTDataResult) {
612+
GWS_LOG_ERROR(@"Failed setting TXTData");
613+
}
614+
}
615+
}
616+
593617
CFNetServiceRegisterWithOptions(_registrationService, 0, &streamError);
594618

595619
_resolutionService = CFNetServiceCreateCopy(kCFAllocatorDefault, _registrationService);

0 commit comments

Comments
 (0)