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

Commit

Permalink
Merge pull request #487 from gezihuzi/dev_bonjour_extent
Browse files Browse the repository at this point in the history
Bonjour service support custom txt data
  • Loading branch information
swisspol committed Aug 17, 2020
2 parents 0da9ee6 + f4cf591 commit c6d118f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions GCDWebServer/Core/GCDWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*
Expand Down
24 changes: 24 additions & 0 deletions GCDWebServer/Core/GCDWebServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -590,6 +591,29 @@ - (BOOL)_start:(NSError**)error {
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);
Expand Down

0 comments on commit c6d118f

Please sign in to comment.