Skip to content

Commit

Permalink
fix(windows): Fixed a bug occurring when listening to the `eventStrea…
Browse files Browse the repository at this point in the history
…m` AFTER starting the action.
  • Loading branch information
Skyost committed Nov 26, 2023
1 parent 13b7e75 commit a4e788d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 53 deletions.
8 changes: 4 additions & 4 deletions packages/bonsoir_windows/windows/bonsoir_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ namespace bonsoir_windows {
}
}

void BonsoirAction::onEvent(EventObject *event) {
log(event->message);
eventQueue.push(event);
void BonsoirAction::onEvent(std::shared_ptr<EventObject> eventObjectPtr) {
log(eventObjectPtr->message);
eventQueue.push(std::move(eventObjectPtr));
processEventQueue();
}

Expand All @@ -84,7 +84,7 @@ namespace bonsoir_windows {
EventObject::EventObject(std::string _message)
: message(_message) {}

SuccessObject::SuccessObject(std::string _id, std::string _message, BonsoirService *_service)
SuccessObject::SuccessObject(std::string _id, std::string _message, std::shared_ptr<BonsoirService> _service)
: EventObject(_message), id(_id), service(_service) {}

void SuccessObject::process(BonsoirAction *action) {
Expand Down
20 changes: 10 additions & 10 deletions packages/bonsoir_windows/windows/bonsoir_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ namespace bonsoir_windows {
class SuccessObject : public EventObject {
public:
std::string id;
BonsoirService *service;
std::shared_ptr<BonsoirService> service;

SuccessObject(std::string _id, std::string _message)
: SuccessObject(_id, _message, nullptr){};

SuccessObject(std::string _id, std::string _message, BonsoirService *_service);
SuccessObject(std::string _id, std::string _message, std::shared_ptr<BonsoirService> _service);

void process(BonsoirAction *action) override;

Expand Down Expand Up @@ -74,14 +74,14 @@ namespace bonsoir_windows {
onSuccess(_id, _message, nullptr);
}

void onSuccess(std::string _id, std::string _message, BonsoirService *_service) {
SuccessObject successObject = SuccessObject(_id, _message, _service);
onEvent(&successObject);
void onSuccess(std::string _id, std::string _message, std::shared_ptr<BonsoirService> _service) {
std::shared_ptr<EventObject> successObjectPtr = std::make_shared<SuccessObject>(_id, _message, _service);
onEvent(successObjectPtr);
}

void onError(std::string _message, EncodableValue _error) {
ErrorObject errorObject = ErrorObject(_message, _error);
onEvent(&errorObject);
std::shared_ptr<ErrorObject> errorObjectPtr = std::make_shared<ErrorObject>(_message, _error);
onEvent(errorObjectPtr);
}

void log(std::string message);
Expand All @@ -91,12 +91,12 @@ namespace bonsoir_windows {
protected:
DNS_SERVICE_CANCEL cancelHandle{};
std::shared_ptr<EventChannel<EncodableValue>> eventChannel;
std::atomic<int> state = 0;

private:
void onEvent(EventObject *event);
void onEvent(std::shared_ptr<EventObject> eventObjectPtr);

std::mutex mutex;
std::queue<EventObject *> eventQueue;
std::queue<std::shared_ptr<EventObject>> eventQueue;
std::atomic<int> state = 0;
};
} // namespace bonsoir_windows
34 changes: 17 additions & 17 deletions packages/bonsoir_windows/windows/bonsoir_broadcast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace bonsoir_windows {
int _id,
bool _printLogs,
BinaryMessenger *_binaryMessenger,
BonsoirService _service
std::unique_ptr<BonsoirService> _servicePtr
)
: BonsoirAction("broadcast", _id, _printLogs, _binaryMessenger),
service(_service) {}
servicePtr(std::move(_servicePtr)) {}

BonsoirBroadcast::~BonsoirBroadcast() {
dispose();
Expand All @@ -23,7 +23,7 @@ namespace bonsoir_windows {
void BonsoirBroadcast::start() {
std::vector<PCWSTR> propertyKeys;
std::vector<PCWSTR> propertyValues;
for (const auto &[key, value] : service.attributes) {
for (const auto &[key, value] : servicePtr->attributes) {
PCWSTR duplicatedKey = _wcsdup(toUtf16(key).c_str());
PCWSTR duplicatedValue = _wcsdup(toUtf16(value).c_str());
if (duplicatedKey != nullptr && duplicatedValue != nullptr) {
Expand All @@ -35,18 +35,18 @@ namespace bonsoir_windows {
propertyValues.push_back(nullptr);

PIP4_ADDRESS ipAddress = nullptr;
if (service.host.has_value() && isValidIPv4(service.host.value())) {
DWORD ip = std::stoul(service.host.value());
if (servicePtr->host.has_value() && isValidIPv4(servicePtr->host.value())) {
DWORD ip = std::stoul(servicePtr->host.value());
ipAddress = &ip;
}

auto computerHost = (getComputerName() + L".local");
PDNS_SERVICE_INSTANCE serviceInstance = DnsServiceConstructInstance(
toUtf16(service.name + "." + service.type + ".local").c_str(),
toUtf16(servicePtr->name + "." + servicePtr->type + ".local").c_str(),
computerHost.c_str(),
nullptr,
nullptr,
static_cast<WORD>(service.port),
static_cast<WORD>(servicePtr->port),
0,
0,
static_cast<DWORD>(propertyKeys.size() - 1),
Expand All @@ -63,17 +63,17 @@ namespace bonsoir_windows {
auto status = DnsServiceRegister(&registerRequest, &cancelHandle);
if (status == DNS_REQUEST_PENDING) {
BonsoirAction::start();
log("Bonsoir service broadcast initialized : " + service.getDescription());
log("Bonsoir service broadcast initialized : " + servicePtr->getDescription());
} else {
onError("Bonsoir service failed to broadcast : " + service.getDescription() + ", error code : " + std::to_string(status), EncodableValue(std::to_string(status)));
onError("Bonsoir service failed to broadcast : " + servicePtr->getDescription() + ", error code : " + std::to_string(status), EncodableValue(std::to_string(status)));
dispose();
}
}

void BonsoirBroadcast::dispose() {
stop();
if (eventChannel != nullptr) {
onSuccess("broadcastStopped", "Bonsoir service broadcast stopped : " + service.getDescription(), &service);
onSuccess("broadcastStopped", "Bonsoir service broadcast stopped : " + servicePtr->getDescription(), servicePtr);
DnsServiceDeRegister(&registerRequest, nullptr);
DnsServiceRegisterCancel(&cancelHandle);
}
Expand All @@ -89,16 +89,16 @@ namespace bonsoir_windows {
if (name == "") {
return;
}
BonsoirService service = broadcast->service;
if (service.name != name) {
std::string oldName = service.name;
service.name = name;
broadcast->onSuccess("broadcastNameAlreadyExists", "Trying to broadcast a service with a name that already exists : " + service.getDescription() + "(old name was " + oldName + ")", &service);
std::shared_ptr<BonsoirService> servicePtr = broadcast->servicePtr;
if (servicePtr->name != name) {
std::string oldName = servicePtr->name;
servicePtr->name = name;
broadcast->onSuccess("broadcastNameAlreadyExists", "Trying to broadcast a service with a name that already exists : " + servicePtr->getDescription() + "(old name was " + oldName + ")", servicePtr);
}
if (status == ERROR_SUCCESS) {
broadcast->onSuccess("broadcastStarted", "Bonsoir service broadcast started : " + service.getDescription(), &service);
broadcast->onSuccess("broadcastStarted", "Bonsoir service broadcast started : " + servicePtr->getDescription(), servicePtr);
} else {
broadcast->onError("Bonsoir service failed to broadcast : " + service.getDescription(), EncodableValue(std::to_string(status)));
broadcast->onError("Bonsoir service failed to broadcast : " + servicePtr->getDescription(), EncodableValue(std::to_string(status)));
broadcast->dispose();
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/bonsoir_windows/windows/bonsoir_broadcast.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ using namespace flutter;
namespace bonsoir_windows {
class BonsoirBroadcast : public BonsoirAction {
public:
BonsoirService service;
std::shared_ptr<BonsoirService> servicePtr;

BonsoirBroadcast(
int _id,
bool _printLogs,
flutter::BinaryMessenger *_binaryMessenger,
BonsoirService _service
std::unique_ptr<BonsoirService> _servicePtr
);

BonsoirBroadcast(BonsoirBroadcast const &) = delete;
Expand Down
22 changes: 11 additions & 11 deletions packages/bonsoir_windows/windows/bonsoir_discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ namespace bonsoir_windows {
}
}

BonsoirService *BonsoirDiscovery::findService(std::string serviceName, std::string serviceType) {
std::shared_ptr<BonsoirService> BonsoirDiscovery::findService(std::string serviceName, std::string serviceType) {
for (auto &found_service : this->services) {
if (found_service.name == serviceName && found_service.type == serviceType) {
return &found_service;
if (found_service->name == serviceName && found_service->type == serviceType) {
return found_service;
}
}
return nullptr;
}

void BonsoirDiscovery::resolveService(std::string serviceName, std::string serviceType) {
BonsoirService *service = findService(serviceName, serviceType);
std::shared_ptr<BonsoirService> service = findService(serviceName, serviceType);
if (service == nullptr) {
onError("Trying to resolve an undiscovered service : " + serviceName, EncodableValue(serviceName));
return;
Expand Down Expand Up @@ -101,16 +101,16 @@ namespace bonsoir_windows {
std::string name = std::get<0>(serviceData);
std::string type = std::get<1>(serviceData);

BonsoirService *service = discovery->findService(name, type);
std::shared_ptr<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);
discovery->services.remove(service);
}
return;
}
if (!service) {
BonsoirService newService = BonsoirService(name, type, 0, std::optional<std::string>(), std::map<std::string, std::string>());
std::shared_ptr<BonsoirService> newService = std::make_shared<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) {
Expand All @@ -124,22 +124,22 @@ namespace bonsoir_windows {
key = record.substr(0, splitIndex);
value = record.substr(splitIndex + 1, record.length());
}
if (key.rfind("=", 0) == std::string::npos && newService.attributes.find(key) == newService.attributes.end()) {
newService.attributes.insert({key, value});
if (key.rfind("=", 0) == std::string::npos && newService->attributes.find(key) == newService->attributes.end()) {
newService->attributes.insert({key, value});
}
}
}
txtRecord = txtRecord->pNext;
}
discovery->services.push_back(newService);
discovery->onSuccess("discoveryServiceFound", "Bonsoir has found a service : " + newService.getDescription(), &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) {
auto discovery = (BonsoirDiscovery *)context;
BonsoirService *service = nullptr;
std::shared_ptr<BonsoirService> service = nullptr;
std::string name = "";
if (serviceInstance && serviceInstance->pszInstanceName) {
std::string nameHost = toUtf8(serviceInstance->pszInstanceName);
Expand Down
6 changes: 3 additions & 3 deletions packages/bonsoir_windows/windows/bonsoir_discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace bonsoir_windows {
class BonsoirDiscovery : public BonsoirAction {
public:
std::string type;
std::list<BonsoirService> services;
std::map<BonsoirService *, PDNS_SERVICE_CANCEL> resolvingServices = std::map<BonsoirService *, PDNS_SERVICE_CANCEL>{};
std::list<std::shared_ptr<BonsoirService>> services;
std::map<std::shared_ptr<BonsoirService>, PDNS_SERVICE_CANCEL> resolvingServices = std::map<std::shared_ptr<BonsoirService>, PDNS_SERVICE_CANCEL>{};

BonsoirDiscovery(
int _id,
Expand All @@ -20,7 +20,7 @@ namespace bonsoir_windows {

void start() override;

BonsoirService *findService(std::string serviceName, std::string serviceType);
std::shared_ptr<BonsoirService> findService(std::string serviceName, std::string serviceType);

void resolveService(std::string serviceName, std::string serviceType);

Expand Down
1 change: 1 addition & 0 deletions packages/bonsoir_windows/windows/bonsoir_service.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "bonsoir_service.h"
#include <iostream>

namespace bonsoir_windows {
BonsoirService::BonsoirService(
Expand Down
12 changes: 6 additions & 6 deletions packages/bonsoir_windows/windows/bonsoir_windows_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ namespace bonsoir_windows {
}
auto hostValue = arguments->find(EncodableValue("service.host"));
std::optional<std::string> host = hostValue == arguments->end() ? std::optional<std::string>() : std::get<std::string>(hostValue->second);
BonsoirService service = BonsoirService(
std::unique_ptr<BonsoirService> servicePtr = std::make_unique<BonsoirService>(
std::get<std::string>(arguments->find(EncodableValue("service.name"))->second),
std::get<std::string>(arguments->find(EncodableValue("service.type"))->second),
std::get<int>(arguments->find(EncodableValue("service.port"))->second),
host,
attributes
);
broadcasts[id] = std::unique_ptr<BonsoirBroadcast>(new BonsoirBroadcast(
broadcasts[id] = std::make_unique<BonsoirBroadcast>(
id,
std::get<bool>(arguments->find(EncodableValue("printLogs"))->second),
messenger,
service
));
std::move(servicePtr)
);
result->Success(EncodableValue(true));
} else if (method.compare("broadcast.start") == 0) {
auto iterator = broadcasts.find(id);
Expand All @@ -73,12 +73,12 @@ namespace bonsoir_windows {
// iterator->second->dispose();
result->Success(EncodableValue(true));
} else if (method.compare("discovery.initialize") == 0) {
discoveries[id] = std::unique_ptr<BonsoirDiscovery>(new BonsoirDiscovery(
discoveries[id] = std::make_unique<BonsoirDiscovery>(
id,
std::get<bool>(arguments->find(EncodableValue("printLogs"))->second),
messenger,
std::get<std::string>(arguments->find(EncodableValue("type"))->second)
));
);
result->Success(EncodableValue(true));
} else if (method.compare("discovery.start") == 0) {
auto iterator = discoveries.find(id);
Expand Down

0 comments on commit a4e788d

Please sign in to comment.