Assert(xw_instance_ == 0);
}
+void Instance::DoAndPostMessage(Instance* that, const std::function<void()>& work,
+ const picojson::value& json) {
+ ScopeLogger();
+ if (nullptr != that) {
+ std::lock_guard<std::mutex> lock{instances_mutex_};
+ if (all_instances_.end() != all_instances_.find(that)) {
+ work();
+ that->PostMessage(json.serialize().c_str());
+ return;
+ }
+ }
+ LoggerE("Trying to post message to non-existing instance: [%p], ignoring", that);
+}
+
void Instance::PostMessage(Instance* that, const char* msg) {
ScopeLogger();
if (nullptr != that) {
auto request = [=]() -> void {
auto response = [this, domain_name, id](const common::PlatformResult result) -> void {
ScopeLogger();
-
picojson::value value{picojson::object{}};
picojson::object& obj = value.get<picojson::object>();
obj["id"] = picojson::value(static_cast<double>(id));
obj["listenerId"] = picojson::value(kNBSCallback + std::to_string(id));
- if (result.IsSuccess()) {
- ReportSuccess(obj);
- this->addDomainListener(domain_name, id);
- } else {
- LogAndReportError(result, &obj);
- }
-
- Instance::PostMessage(this, value.serialize().c_str());
+ Instance::DoAndPostMessage(this,
+ [this, &domain_name, &id, &result, &obj]() -> void {
+ if (result.IsSuccess()) {
+ ReportSuccess(obj);
+ this->addDomainListener(domain_name, id);
+ } else {
+ LogAndReportError(result, &obj);
+ }
+ },
+ value);
};
NetworkBearerSelectionManager::GetInstance()->requestRouteToHost(domain_name, response);
auto release = [=]() -> void {
auto response = [this, domain_name, id](const common::PlatformResult result) -> void {
ScopeLogger();
-
picojson::value value{picojson::object{}};
picojson::object& obj = value.get<picojson::object>();
obj["callbackId"] = picojson::value(id);
- if (result.IsSuccess()) {
- ReportSuccess(obj);
- this->removeDomainListener(domain_name);
- } else {
- LogAndReportError(result, &obj);
- }
-
- Instance::PostMessage(this, value.serialize().c_str());
+ Instance::DoAndPostMessage(this,
+ [this, &domain_name, &result, &obj]() -> void {
+ if (result.IsSuccess()) {
+ ReportSuccess(obj);
+ this->removeDomainListener(domain_name);
+ } else {
+ LogAndReportError(result, &obj);
+ }
+ },
+ value);
};
NetworkBearerSelectionManager::GetInstance()->releaseRouteToHost(domain_name, response);