diff --git a/include/upnp/upnp_context.h b/include/upnp/upnp_context.h index a1014cc717296d7784cf2bd8566bc54ec43be2dd..6fe02351257799904e1c3229ccd42aa372665e4b 100644 --- a/include/upnp/upnp_context.h +++ b/include/upnp/upnp_context.h @@ -22,6 +22,7 @@ #include <opendht/rng.h> #include <opendht/logger.h> +#include <asio/dispatch.hpp> #include <asio/steady_timer.hpp> #include <asio/system_timer.hpp> @@ -125,7 +126,7 @@ public: template <typename T> inline void dispatch(T&& f) { - stateCtx->dispatch(std::move(f)); + asio::dispatch(*stateCtx, std::forward<T>(f)); } void restart() diff --git a/src/upnp/protocol/natpmp/nat_pmp.cpp b/src/upnp/protocol/natpmp/nat_pmp.cpp index 01ebc9536ac46ad808dd2e09e552dae912b7d424..83a93ce104f1f26f698ea46e6509d24439f5dda0 100644 --- a/src/upnp/protocol/natpmp/nat_pmp.cpp +++ b/src/upnp/protocol/natpmp/nat_pmp.cpp @@ -25,6 +25,8 @@ #include <poll.h> #endif +#include <asio/post.hpp> + #ifdef _WIN32 #define _poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) #else @@ -38,7 +40,7 @@ NatPmp::NatPmp(const std::shared_ptr<asio::io_context>& ctx, const std::shared_p : UPnPProtocol(logger), ioContext(ctx), searchForIgdTimer_(*ctx) { // JAMI_DBG("NAT-PMP: Instance [%p] created", this); - ioContext->dispatch([this] { + asio::dispatch(*ioContext, [this] { igd_ = std::make_shared<PMPIGD>(); }); } @@ -143,7 +145,7 @@ NatPmp::terminate() { std::condition_variable cv {}; - ioContext->dispatch([&] { + asio::dispatch(*ioContext, [&] { terminate(cv); }); @@ -264,7 +266,7 @@ NatPmp::requestMappingAdd(const Mapping& mapping) { // libnatpmp isn't thread-safe, so we use Asio here to make // sure that all requests are sent from the same thread. - ioContext->post([w = weak(), mapping] { + asio::post(*ioContext, [w = weak(), mapping] { auto sthis = w.lock(); if (!sthis) return; @@ -301,7 +303,7 @@ NatPmp::requestMappingRenew(const Mapping& mapping) { // libnatpmp isn't thread-safe, so we use Asio here to make // sure that all requests are sent from the same thread. - ioContext->post([w = weak(), mapping] { + asio::post(*ioContext, [w = weak(), mapping] { auto sthis = w.lock(); if (!sthis) return; @@ -505,7 +507,7 @@ NatPmp::addPortMapping(Mapping& mapping) void NatPmp::requestMappingRemove(const Mapping& mapping) { - ioContext->dispatch([w = weak(), mapping] { + asio::dispatch(*ioContext, [w = weak(), mapping] { if (auto pmpThis = w.lock()) { Mapping map {mapping}; pmpThis->removePortMapping(map); diff --git a/src/upnp/protocol/pupnp/pupnp.cpp b/src/upnp/protocol/pupnp/pupnp.cpp index c702d4047f2f5bd742e8471be92dd76c2f55ce05..c6b0993b9ca08d0d68067733c8f798f39a5aea68 100644 --- a/src/upnp/protocol/pupnp/pupnp.cpp +++ b/src/upnp/protocol/pupnp/pupnp.cpp @@ -558,7 +558,7 @@ PUPnP::validateIgd(const std::string& location, IXML_Document* doc_container_ptr void PUPnP::requestMappingAdd(const Mapping& mapping) { - ioContext->post([w = weak(), mapping] { + asio::post(*ioContext, [w = weak(), mapping] { if (auto upnpThis = w.lock()) { if (not upnpThis->isRunning()) return; @@ -582,7 +582,7 @@ PUPnP::requestMappingAdd(const Mapping& mapping) void PUPnP::requestMappingRenew(const Mapping& mapping) { - ioContext->post([w = weak(), mapping] { + asio::post(*ioContext, [w = weak(), mapping] { if (auto upnpThis = w.lock()) { if (not upnpThis->isRunning()) return; @@ -615,7 +615,7 @@ void PUPnP::requestMappingRemove(const Mapping& mapping) { // Send remove request using the matching IGD - ioContext->post([w = weak(), mapping] { + asio::post(*ioContext, [w = weak(), mapping] { if (auto upnpThis = w.lock()) { // Abort if we are shutting down. if (not upnpThis->isRunning()) @@ -838,7 +838,7 @@ PUPnP::downLoadIgdDescription(const std::string& locationUrl) UpnpGetErrorMessage(upnp_err)); } else { if(logger_) logger_->debug("PUPnP: Succeeded to download device XML document from {}", locationUrl); - ioContext->post([w = weak(), url = locationUrl, doc_container_ptr] { + asio::post(*ioContext, [w = weak(), url = locationUrl, doc_container_ptr] { if (auto upnpThis = w.lock()) { upnpThis->validateIgd(url, doc_container_ptr); } @@ -922,7 +922,7 @@ PUPnP::handleCtrlPtUPnPEvents(Upnp_EventType event_type, const void* event) std::string deviceId {UpnpDiscovery_get_DeviceID_cstr(d_event)}; std::string location {UpnpDiscovery_get_Location_cstr(d_event)}; IpAddr dstAddr(*(const pj_sockaddr*) (UpnpDiscovery_get_DestAddr(d_event))); - ioContext->post([w = weak(), + asio::post(*ioContext, [w = weak(), deviceId = std::move(deviceId), location = std::move(location), dstAddr = std::move(dstAddr)] { @@ -938,7 +938,7 @@ PUPnP::handleCtrlPtUPnPEvents(Upnp_EventType event_type, const void* event) std::string deviceId(UpnpDiscovery_get_DeviceID_cstr(d_event)); // Process the response on the main thread. - ioContext->post([w = weak(), deviceId = std::move(deviceId)] { + asio::post(*ioContext, [w = weak(), deviceId = std::move(deviceId)] { if (auto upnpThis = w.lock()) { upnpThis->processDiscoveryAdvertisementByebye(deviceId); } @@ -968,7 +968,7 @@ PUPnP::handleCtrlPtUPnPEvents(Upnp_EventType event_type, const void* event) std::string publisherUrl(UpnpEventSubscribe_get_PublisherUrl_cstr(es_event)); // Process the response on the main thread. - ioContext->post([w = weak(), event_type, publisherUrl = std::move(publisherUrl)] { + asio::post(*ioContext, [w = weak(), event_type, publisherUrl = std::move(publisherUrl)] { if (auto upnpThis = w.lock()) { upnpThis->processDiscoverySubscriptionExpired(event_type, publisherUrl); } @@ -1477,7 +1477,7 @@ PUPnP::deleteMappingsByDescription(const std::shared_ptr<IGD>& igd, const std::s igd->toString(), description); - ioContext->post([w=weak(), igd, description]{ + asio::post(*ioContext, [w=weak(), igd, description]{ if (auto sthis = w.lock()) { auto mapList = sthis->getMappingsListByDescr(igd, description); for (auto const& [_, map] : mapList) { diff --git a/src/upnp/upnp_context.cpp b/src/upnp/upnp_context.cpp index 756ec1ecafdcc7b4aedab8e0b5b71834d71b8439..5304a4dbf666db9fd10192e365842c3e74fbf7b5 100644 --- a/src/upnp/upnp_context.cpp +++ b/src/upnp/upnp_context.cpp @@ -61,7 +61,7 @@ UPnPContext::UPnPContext(const std::shared_ptr<asio::io_context>& ioContext, con portRange_.emplace(PortType::TCP, std::make_pair(UPNP_TCP_PORT_MIN, UPNP_TCP_PORT_MAX)); portRange_.emplace(PortType::UDP, std::make_pair(UPNP_UDP_PORT_MIN, UPNP_UDP_PORT_MAX)); - stateCtx->post([this] { init(); }); + asio::post(*stateCtx, [this] { init(); }); } std::shared_ptr<asio::io_context> @@ -116,7 +116,7 @@ UPnPContext::shutdown() std::unique_lock lk(mappingMutex_); std::condition_variable cv; - stateCtx->post([&, this] { shutdown(cv); }); + asio::post(*stateCtx, [&, this] { shutdown(cv); }); if (logger_) logger_->debug("Waiting for shutdown…"); @@ -177,7 +177,7 @@ UPnPContext::startUpnp() // Request a new IGD search. for (auto const& [_, protocol] : protocolList_) { - ioCtx->dispatch([p=protocol] { p->searchForIgd(); }); + asio::dispatch(*ioCtx, [p=protocol] { p->searchForIgd(); }); } started_ = true; @@ -225,7 +225,7 @@ UPnPContext::stopUpnp(bool forceRelease) // Clear all current IGDs. for (auto const& [_, protocol] : protocolList_) { - ioCtx->dispatch([p=protocol]{ p->clearIgds(); }); + asio::dispatch(*ioCtx, [p=protocol]{ p->clearIgds(); }); } started_ = false; @@ -393,7 +393,7 @@ UPnPContext::reserveMapping(Mapping& requestedMap) void UPnPContext::releaseMapping(const Mapping& map) { - stateCtx->dispatch([this, map] { + asio::dispatch(*stateCtx, [this, map] { if (shutdownComplete_) return; auto mapPtr = getMappingWithKey(map.getMapKey()); @@ -975,7 +975,7 @@ void UPnPContext::onIgdUpdated(const std::shared_ptr<IGD>& igd, UpnpIgdEvent event) { if (!stateCtx->get_executor().running_in_this_thread()) { - stateCtx->post([this, igd, event = std::move(event)] { onIgdUpdated(igd, event); }); + asio::post(*stateCtx, [this, igd, event = std::move(event)] { onIgdUpdated(igd, event); }); return; } assert(igd); @@ -1041,7 +1041,7 @@ void UPnPContext::onMappingAdded(const std::shared_ptr<IGD>& igd, const Mapping& mapRes) { if (!stateCtx->get_executor().running_in_this_thread()) { - stateCtx->post([this, igd, mapRes] { onMappingAdded(igd, mapRes); }); + asio::post(*stateCtx, [this, igd, mapRes] { onMappingAdded(igd, mapRes); }); return; } @@ -1082,7 +1082,7 @@ void UPnPContext::onMappingRenewed(const std::shared_ptr<IGD>& igd, const Mapping& map) { if (!stateCtx->get_executor().running_in_this_thread()) { - stateCtx->post([this, igd, map] { onMappingRenewed(igd, map); }); + asio::post(*stateCtx, [this, igd, map] { onMappingRenewed(igd, map); }); return; } @@ -1125,7 +1125,7 @@ void UPnPContext::onMappingRemoved(const std::shared_ptr<IGD>& igd, const Mapping& mapRes) { if (!stateCtx->get_executor().running_in_this_thread()) { - stateCtx->post([this, igd, mapRes] { onMappingRemoved(igd, mapRes); }); + asio::post(*stateCtx, [this, igd, mapRes] { onMappingRemoved(igd, mapRes); }); return; } @@ -1141,7 +1141,7 @@ void UPnPContext::onIgdDiscoveryStarted() { if (!stateCtx->get_executor().running_in_this_thread()) { - stateCtx->post([this] { onIgdDiscoveryStarted(); }); + asio::post(*stateCtx, [this] { onIgdDiscoveryStarted(); }); return; } std::lock_guard lock(igdDiscoveryMutex_); @@ -1294,7 +1294,7 @@ void UPnPContext::onMappingRequestFailed(const Mapping& mapRes) { if (!stateCtx->get_executor().running_in_this_thread()) { - stateCtx->post([this, mapRes] { onMappingRequestFailed(mapRes); }); + asio::post(*stateCtx, [this, mapRes] { onMappingRequestFailed(mapRes); }); return; }