Skip to content

Commit

Permalink
fix(windows): Fixed a discovery.stop() bug on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyost committed Nov 19, 2023
1 parent e480faa commit 6a6284c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
77 changes: 41 additions & 36 deletions packages/bonsoir_windows/windows/bonsoir_discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,49 +84,54 @@ namespace bonsoir_windows {

void browseCallback(DWORD status, PVOID context, PDNS_RECORD dnsRecord) {
auto discovery = (BonsoirDiscovery *)context;
if (status == ERROR_SUCCESS) {
std::string nameHost = toUtf8(dnsRecord->Data.PTR.pNameHost);
auto parts = split(nameHost, '.');
std::string name = parts[0];
std::string type = parts[1] + "." + parts[2];
if (status == ERROR_CANCELLED) {
if (discovery->isRunning()) {
discovery->BonsoirAction::dispose();
}
return;
}

BonsoirService *service = discovery->findService(name, type);
if (dnsRecord->dwTtl <= 0) {
if (service) {
discovery->onSuccess("discoveryServiceLost", "A Bonsoir service has been lost : " + service->getDescription(), service);
discovery->services.remove(*service);
}
return;
if (status != ERROR_SUCCESS) {
discovery->onError("Bonsoir has encountered an error during discovery : " + std::to_string(status), EncodableValue(std::to_string(status)));
discovery->dispose();
return;
}

std::string nameHost = toUtf8(dnsRecord->Data.PTR.pNameHost);
auto parts = split(nameHost, '.');
std::string name = parts[0];
std::string type = parts[1] + "." + parts[2];

BonsoirService *service = discovery->findService(name, type);
if (dnsRecord->dwTtl <= 0) {
if (service) {
discovery->onSuccess("discoveryServiceLost", "A Bonsoir service has been lost : " + service->getDescription(), service);
discovery->services.remove(*service);
}
if (!service) {
BonsoirService newService =
BonsoirService(name, type, 0, std::optional<std::string>(), std::map<std::string, std::string>());
PDNS_RECORD txtRecord = dnsRecord;
while (txtRecord != nullptr) {
if (txtRecord->wType == DNS_TYPE_TEXT) {
DNS_TXT_DATAW *pData = &txtRecord->Data.TXT;
for (DWORD s = 0; s < pData->dwStringCount; s++) {
std::string record = toUtf8(std::wstring(pData->pStringArray[s]));
int splitIndex = static_cast<int>(record.find("="));
if (splitIndex != std::string::npos) {
newService.attributes.insert(
{record.substr(0, splitIndex), record.substr(splitIndex + 1, record.length())}
);
}
return;
}
if (!service) {
BonsoirService newService = BonsoirService(name, type, 0, std::optional<std::string>(), std::map<std::string, std::string>());
PDNS_RECORD txtRecord = dnsRecord;
while (txtRecord != nullptr) {
if (txtRecord->wType == DNS_TYPE_TEXT) {
DNS_TXT_DATAW *pData = &txtRecord->Data.TXT;
for (DWORD s = 0; s < pData->dwStringCount; s++) {
std::string record = toUtf8(std::wstring(pData->pStringArray[s]));
int splitIndex = static_cast<int>(record.find("="));
if (splitIndex != std::string::npos) {
newService.attributes.insert(
{record.substr(0, splitIndex), record.substr(splitIndex + 1, record.length())}
);
}
}
txtRecord = txtRecord->pNext;
}
discovery->services.push_back(newService);
discovery->onSuccess("discoveryServiceFound", "Bonsoir has found a service : " + newService.getDescription(), &newService);
txtRecord = txtRecord->pNext;
}
DnsRecordListFree(dnsRecord, DnsFreeRecordList);
} else if (status == ERROR_CANCELLED) {
discovery->BonsoirAction::dispose();
} else {
discovery->onError("Bonsoir has encountered an error during discovery : " + std::to_string(status), EncodableValue(std::to_string(status)));
discovery->dispose();
discovery->services.push_back(newService);
discovery->onSuccess("discoveryServiceFound", "Bonsoir has found a service : " + newService.getDescription(), &newService);
}
DnsRecordListFree(dnsRecord, DnsFreeRecordList);
}

void resolveCallback(DWORD status, PVOID context, PDNS_SERVICE_INSTANCE serviceInstance) {
Expand Down
1 change: 1 addition & 0 deletions packages/bonsoir_windows/windows/bonsoir_windows_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace bonsoir_windows {

// Called when a method is called on this plugin's channel from Dart.
void HandleMethodCall(const MethodCall<EncodableValue> &methodCall, std::unique_ptr<MethodResult<EncodableValue>> result);

private:
BinaryMessenger *messenger;
std::map<int, std::unique_ptr<BonsoirBroadcast>> broadcasts;
Expand Down

0 comments on commit 6a6284c

Please sign in to comment.