From b9f02f22c43ae8f86c8b1568cdccffd8793d087f Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Sat, 18 Jan 2014 12:56:35 +0100
Subject: [PATCH] db/upnp: don't use WorkQueue for _ADVERTISEMENT_BYEBYE

Remove the ContentDirectoryDescriptor right away.  Reduces bloat.
---
 src/db/upnp/Discovery.cxx | 61 ++++++++++++++++++---------------------
 src/db/upnp/Discovery.hxx |  5 ++--
 2 files changed, 30 insertions(+), 36 deletions(-)

diff --git a/src/db/upnp/Discovery.cxx b/src/db/upnp/Discovery.cxx
index d7982f670..ad63530db 100644
--- a/src/db/upnp/Discovery.cxx
+++ b/src/db/upnp/Discovery.cxx
@@ -62,40 +62,33 @@ UPnPDeviceDirectory::discoExplorer()
 		}
 
 		const ScopeLock protect(mutex);
-		if (!tsk->alive) {
-			// Device signals it is going off.
-			auto it = directories.find(tsk->deviceId);
-			if (it != directories.end()) {
-				directories.erase(it);
-			}
-		} else {
-			// Device signals its existence and well-being. Perform the
-			// UPnP "description" phase by downloading and decoding the
-			// description document.
-			char *buf;
-			// LINE_SIZE is defined by libupnp's upnp.h...
-			char contentType[LINE_SIZE];
-			int code = UpnpDownloadUrlItem(tsk->url.c_str(), &buf, contentType);
-			if (code != UPNP_E_SUCCESS) {
-				continue;
-			}
-			std::string sdesc(buf);
+		// Device signals its existence and well-being. Perform the
+		// UPnP "description" phase by downloading and decoding the
+		// description document.
+		char *buf;
+		// LINE_SIZE is defined by libupnp's upnp.h...
+		char contentType[LINE_SIZE];
+		int code = UpnpDownloadUrlItem(tsk->url.c_str(), &buf, contentType);
+		if (code != UPNP_E_SUCCESS) {
+			continue;
+		}
+		std::string sdesc(buf);
 
-			// Update or insert the device
-			ContentDirectoryDescriptor d(tsk->url, sdesc,
-						     time(0), tsk->expires);
-			if (!d.device.ok) {
-				continue;
-			}
+		// Update or insert the device
+		ContentDirectoryDescriptor d(tsk->url, sdesc,
+					     time(0), tsk->expires);
+		if (!d.device.ok) {
+			continue;
+		}
 
 #if defined(__clang__) || GCC_CHECK_VERSION(4,8)
-			auto e = directories.emplace(tsk->deviceId, d);
+		auto e = directories.emplace(tsk->deviceId, d);
 #else
-			auto e = directories.insert(std::make_pair(tsk->deviceId, d));
+		auto e = directories.insert(std::make_pair(tsk->deviceId, d));
 #endif
-			if (!e.second)
-				e.first->second = d;
-		}
+		if (!e.second)
+			e.first->second = d;
+
 		delete tsk;
 	}
 }
@@ -113,7 +106,7 @@ UPnPDeviceDirectory::OnAlive(Upnp_Discovery *disco)
 {
 	if (isMSDevice(disco->DeviceType) ||
 	    isCDService(disco->ServiceType)) {
-		DiscoveredTask *tp = new DiscoveredTask(1, disco);
+		DiscoveredTask *tp = new DiscoveredTask(disco);
 		if (discoveredQueue.put(tp))
 			return UPNP_E_FINISH;
 	}
@@ -127,9 +120,11 @@ UPnPDeviceDirectory::OnByeBye(Upnp_Discovery *disco)
 
 	if (isMSDevice(disco->DeviceType) ||
 	    isCDService(disco->ServiceType)) {
-		DiscoveredTask *tp = new DiscoveredTask(0, disco);
-		if (discoveredQueue.put(tp))
-			return UPNP_E_FINISH;
+		// Device signals it is going off.
+		const ScopeLock protect(mutex);
+		auto it = directories.find(disco->DeviceId);
+		if (it != directories.end())
+			directories.erase(it);
 	}
 
 	return UPNP_E_SUCCESS;
diff --git a/src/db/upnp/Discovery.hxx b/src/db/upnp/Discovery.hxx
index c4704dc7e..9e362c96f 100644
--- a/src/db/upnp/Discovery.hxx
+++ b/src/db/upnp/Discovery.hxx
@@ -49,13 +49,12 @@ class UPnPDeviceDirectory {
 	 * discovery thread.
 	 */
 	struct DiscoveredTask {
-		bool alive;
 		std::string url;
 		std::string deviceId;
 		int expires; // Seconds valid
 
-		DiscoveredTask(bool _alive, const Upnp_Discovery *disco)
-			: alive(_alive), url(disco->Location),
+		DiscoveredTask(const Upnp_Discovery *disco)
+			:url(disco->Location),
 			  deviceId(disco->DeviceId),
 			  expires(disco->Expires) {}
 	};