Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyost committed Jul 15, 2024
2 parents 1ea6d58 + 5941f71 commit 6709e95
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class BonsoirServiceBroadcast: BonsoirAction {
var txtRecord = TXTRecordRef()
TXTRecordCreate(&txtRecord, 0, nil)
for (key, value) in service.attributes {
TXTRecordSetValue(&txtRecord, key, UInt8(value.count), value)
guard let valueData = value.data(using: .utf8) else { continue }
TXTRecordSetValue(&txtRecord, key, UInt8(valueData.count), [UInt8](valueData))
}
let error = DNSServiceRegister(&sdRef, 0, 0, service.name, service.type, "local.", service.host, CFSwapInt16HostToBig(UInt16(service.port)), TXTRecordGetLength(&txtRecord), TXTRecordGetBytesPtr(&txtRecord), BonsoirServiceBroadcast.registerCallback as DNSServiceRegisterReply, Unmanaged.passUnretained(self).toOpaque())
if error == kDNSServiceErr_NoError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class BonsoirServiceDiscovery: BonsoirAction {

/// Contains all services we're currently resolving.
private var pendingResolution: [DNSServiceRef] = []

private var pendingDispatchSources: [DispatchSourceRead] = []

/// Initializes this class.
public init(id: Int, printLogs: Bool, onDispose: @escaping () -> Void, messenger: FlutterBinaryMessenger, type: String) {
self.type = type
Expand Down Expand Up @@ -125,8 +126,49 @@ class BonsoirServiceDiscovery: BonsoirAction {
stopResolution(sdRef: sdRef, remove: false)
return false
}

pendingResolution.append(sdRef!)
DNSServiceProcessResult(sdRef)

let socket = DNSServiceRefSockFD(sdRef);
if socket == -1 {
onSuccess(eventId: Generated.discoveryServiceResolveFailed, service: service, parameters: [])
stopResolution(sdRef: sdRef, remove: false)
return false
}

let dispatchSource = DispatchSource.makeReadSource(fileDescriptor: socket, queue: DispatchQueue.global(qos: .userInitiated));
dispatchSource.setEventHandler(handler: {
DNSServiceProcessResult(sdRef)

DispatchQueue.main.async {
let foundIndex = self.pendingDispatchSources.firstIndex(where: {
return $0.isEqual(dispatchSource);
})

if foundIndex != nil {
self.pendingDispatchSources.remove(at: foundIndex!)
}
}
});

dispatchSource.setCancelHandler(handler: {
DispatchQueue.main.async {
let foundIndex = self.pendingDispatchSources.firstIndex(where: {
return $0.isEqual(dispatchSource);
})

if foundIndex != nil {
self.pendingDispatchSources.remove(at: foundIndex!)
}

self.onSuccess(eventId: Generated.discoveryServiceResolveFailed, service: service, parameters: [])
self.stopResolution(sdRef: sdRef, remove: false)
}
});

dispatchSource.activate()

pendingDispatchSources.append(dispatchSource)
return true
}

Expand Down Expand Up @@ -167,15 +209,25 @@ class BonsoirServiceDiscovery: BonsoirAction {
service!.host = String(cString: hosttarget!)
}
service!.port = Int(CFSwapInt16BigToHost(port))
discovery.onSuccess(eventId: Generated.discoveryServiceResolved, service: service)

DispatchQueue.main.async {
discovery.onSuccess(eventId: Generated.discoveryServiceResolved, service: service)
}
} else {
if service == nil {
discovery.onError(message: Generated.discoveryServiceResolveFailed, parameters: ["nil", errorCode])
DispatchQueue.main.async {
discovery.onError(message: Generated.discoveryServiceResolveFailed, parameters: ["nil", errorCode])
}
} else {
discovery.onSuccess(eventId: Generated.discoveryServiceResolveFailed, service: service, parameters: [errorCode])
DispatchQueue.main.async {
discovery.onSuccess(eventId: Generated.discoveryServiceResolveFailed, service: service, parameters: [errorCode])
}
}
}
discovery.stopResolution(sdRef: sdRef, remove: sdRef != nil)

DispatchQueue.main.async {
discovery.stopResolution(sdRef: sdRef, remove: sdRef != nil)
}
}

/// Allows to unescape services FQDN.
Expand Down
2 changes: 1 addition & 1 deletion packages/bonsoir_windows/windows/bonsoir_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ namespace bonsoir_windows {
onEvent(successObjectPtr, successParameters);
}

void onError(EncodableValue details, std::list<std::string> parameters = std::list<std::string>(), std::optional<std::string> message = nullptr) {
void onError(EncodableValue details, std::list<std::string> parameters = std::list<std::string>(), std::optional<std::string> message = std::nullopt) {
std::string errorMessage = format(message.value_or(logMessages.find(action + "Error")->second), parameters);

std::shared_ptr<ErrorObject> errorObjectPtr = std::make_shared<ErrorObject>(errorMessage, details);
Expand Down
3 changes: 3 additions & 0 deletions packages/bonsoir_windows/windows/bonsoir_broadcast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ namespace bonsoir_windows {
if (!(broadcast->isRunning())) {
return;
}
if (instance == nullptr || instance->pszInstanceName == nullptr) {
return;
}
std::string name = std::get<0>(parseBonjourFqdn(toUtf8(instance->pszInstanceName)));
if (name == "") {
return;
Expand Down

0 comments on commit 6709e95

Please sign in to comment.