db/upnp/Discovery: use std::list instead of std::map
There will only be very few items, and that does not justify the bloat of std::map.
This commit is contained in:
parent
c9af358e4b
commit
b8e382b011
@ -54,19 +54,32 @@ isMSDevice(const char *st)
|
||||
}
|
||||
|
||||
inline void
|
||||
UPnPDeviceDirectory::LockAdd(std::string &&id, ContentDirectoryDescriptor &&d)
|
||||
UPnPDeviceDirectory::LockAdd(ContentDirectoryDescriptor &&d)
|
||||
{
|
||||
const ScopeLock protect(mutex);
|
||||
directories[std::move(id)] = std::move(d);
|
||||
|
||||
for (auto &i : directories) {
|
||||
if (i.id == d.id) {
|
||||
i = std::move(d);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
directories.emplace_back(std::move(d));
|
||||
}
|
||||
|
||||
inline void
|
||||
UPnPDeviceDirectory::LockRemove(const std::string &id)
|
||||
{
|
||||
const ScopeLock protect(mutex);
|
||||
auto i = directories.find(id);
|
||||
if (i != directories.end())
|
||||
directories.erase(i);
|
||||
|
||||
for (auto i = directories.begin(), end = directories.end();
|
||||
i != end; ++i) {
|
||||
if (i->id == id) {
|
||||
directories.erase(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
@ -91,7 +104,8 @@ UPnPDeviceDirectory::discoExplorer()
|
||||
}
|
||||
|
||||
// Update or insert the device
|
||||
ContentDirectoryDescriptor d(MonotonicClockS(), tsk->expires);
|
||||
ContentDirectoryDescriptor d(std::move(tsk->deviceId),
|
||||
MonotonicClockS(), tsk->expires);
|
||||
|
||||
{
|
||||
Error error2;
|
||||
@ -104,7 +118,7 @@ UPnPDeviceDirectory::discoExplorer()
|
||||
}
|
||||
}
|
||||
|
||||
LockAdd(std::move(tsk->deviceId), std::move(d));
|
||||
LockAdd(std::move(d));
|
||||
delete tsk;
|
||||
}
|
||||
}
|
||||
@ -180,7 +194,7 @@ UPnPDeviceDirectory::expireDevices(Error &error)
|
||||
|
||||
for (auto it = directories.begin();
|
||||
it != directories.end();) {
|
||||
if (now > it->second.expires) {
|
||||
if (now > it->expires) {
|
||||
it = directories.erase(it);
|
||||
didsomething = true;
|
||||
} else {
|
||||
@ -263,9 +277,9 @@ UPnPDeviceDirectory::getDirServices(std::vector<ContentDirectoryService> &out,
|
||||
|
||||
for (auto dit = directories.begin();
|
||||
dit != directories.end(); dit++) {
|
||||
for (const auto &service : dit->second.device.services) {
|
||||
for (const auto &service : dit->device.services) {
|
||||
if (isCDService(service.serviceType.c_str())) {
|
||||
out.emplace_back(dit->second.device, service);
|
||||
out.emplace_back(dit->device, service);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -285,7 +299,7 @@ UPnPDeviceDirectory::getServer(const char *friendlyName,
|
||||
const ScopeLock protect(mutex);
|
||||
|
||||
for (const auto &i : directories) {
|
||||
const auto &device = i.second.device;
|
||||
const auto &device = i.device;
|
||||
|
||||
if (device.friendlyName != friendlyName)
|
||||
continue;
|
||||
|
@ -27,7 +27,7 @@
|
||||
|
||||
#include <upnp/upnp.h>
|
||||
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
@ -63,6 +63,8 @@ class UPnPDeviceDirectory {
|
||||
*/
|
||||
class ContentDirectoryDescriptor {
|
||||
public:
|
||||
std::string id;
|
||||
|
||||
UPnPDevice device;
|
||||
|
||||
/**
|
||||
@ -73,8 +75,9 @@ class UPnPDeviceDirectory {
|
||||
|
||||
ContentDirectoryDescriptor() = default;
|
||||
|
||||
ContentDirectoryDescriptor(unsigned last, int exp)
|
||||
:expires(last + exp + 20) {}
|
||||
ContentDirectoryDescriptor(std::string &&_id,
|
||||
unsigned last, int exp)
|
||||
:id(std::move(_id)), expires(last + exp + 20) {}
|
||||
|
||||
bool Parse(const std::string &url, const char *description,
|
||||
Error &_error) {
|
||||
@ -85,7 +88,7 @@ class UPnPDeviceDirectory {
|
||||
LibUPnP *const lib;
|
||||
|
||||
Mutex mutex;
|
||||
std::map<std::string, ContentDirectoryDescriptor> directories;
|
||||
std::list<ContentDirectoryDescriptor> directories;
|
||||
WorkQueue<DiscoveredTask *> discoveredQueue;
|
||||
|
||||
/**
|
||||
@ -129,7 +132,7 @@ private:
|
||||
*/
|
||||
bool expireDevices(Error &error);
|
||||
|
||||
void LockAdd(std::string &&id, ContentDirectoryDescriptor &&d);
|
||||
void LockAdd(ContentDirectoryDescriptor &&d);
|
||||
void LockRemove(const std::string &id);
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user