upnp: initialize the client handle only once
Eliminate class LibUPnP and move the code to ClientInit.cxx. Its initialization function can be called multiple times, but UpnpRegisterClient() is called at most once.
This commit is contained in:
parent
01f7abfc63
commit
d7e78059b0
@ -218,13 +218,13 @@ src_mpd_SOURCES = \
|
|||||||
|
|
||||||
UPNP_SOURCES = \
|
UPNP_SOURCES = \
|
||||||
src/lib/upnp/Init.cxx src/lib/upnp/Init.hxx \
|
src/lib/upnp/Init.cxx src/lib/upnp/Init.hxx \
|
||||||
|
src/lib/upnp/ClientInit.cxx src/lib/upnp/ClientInit.hxx \
|
||||||
src/lib/upnp/Device.cxx src/lib/upnp/Device.hxx \
|
src/lib/upnp/Device.cxx src/lib/upnp/Device.hxx \
|
||||||
src/lib/upnp/ContentDirectoryService.cxx src/lib/upnp/ContentDirectoryService.hxx \
|
src/lib/upnp/ContentDirectoryService.cxx src/lib/upnp/ContentDirectoryService.hxx \
|
||||||
src/lib/upnp/Discovery.cxx src/lib/upnp/Discovery.hxx \
|
src/lib/upnp/Discovery.cxx src/lib/upnp/Discovery.hxx \
|
||||||
src/lib/upnp/Domain.cxx src/lib/upnp/Domain.hxx \
|
src/lib/upnp/Domain.cxx src/lib/upnp/Domain.hxx \
|
||||||
src/lib/upnp/ixmlwrap.cxx src/lib/upnp/ixmlwrap.hxx \
|
src/lib/upnp/ixmlwrap.cxx src/lib/upnp/ixmlwrap.hxx \
|
||||||
src/lib/upnp/Callback.hxx \
|
src/lib/upnp/Callback.hxx \
|
||||||
src/lib/upnp/upnpplib.cxx src/lib/upnp/upnpplib.hxx \
|
|
||||||
src/lib/upnp/Util.cxx src/lib/upnp/Util.hxx \
|
src/lib/upnp/Util.cxx src/lib/upnp/Util.hxx \
|
||||||
src/lib/upnp/WorkQueue.hxx \
|
src/lib/upnp/WorkQueue.hxx \
|
||||||
src/lib/upnp/Action.hxx
|
src/lib/upnp/Action.hxx
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "UpnpDatabasePlugin.hxx"
|
#include "UpnpDatabasePlugin.hxx"
|
||||||
#include "lib/upnp/Domain.hxx"
|
#include "lib/upnp/Domain.hxx"
|
||||||
#include "lib/upnp/upnpplib.hxx"
|
#include "lib/upnp/ClientInit.hxx"
|
||||||
#include "lib/upnp/Discovery.hxx"
|
#include "lib/upnp/Discovery.hxx"
|
||||||
#include "lib/upnp/ContentDirectoryService.hxx"
|
#include "lib/upnp/ContentDirectoryService.hxx"
|
||||||
#include "lib/upnp/Util.hxx"
|
#include "lib/upnp/Util.hxx"
|
||||||
@ -69,7 +69,7 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
class UpnpDatabase : public Database {
|
class UpnpDatabase : public Database {
|
||||||
LibUPnP *lib;
|
UpnpClient_Handle handle;
|
||||||
UPnPDeviceDirectory *discovery;
|
UPnPDeviceDirectory *discovery;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -175,17 +175,13 @@ UpnpDatabase::Configure(const config_param &, Error &)
|
|||||||
bool
|
bool
|
||||||
UpnpDatabase::Open(Error &error)
|
UpnpDatabase::Open(Error &error)
|
||||||
{
|
{
|
||||||
lib = new LibUPnP();
|
if (!UpnpClientGlobalInit(handle, error))
|
||||||
if (!lib->ok()) {
|
|
||||||
error.Set(lib->GetInitError());
|
|
||||||
delete lib;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
discovery = new UPnPDeviceDirectory(lib->getclh());
|
discovery = new UPnPDeviceDirectory(handle);
|
||||||
if (!discovery->Start(error)) {
|
if (!discovery->Start(error)) {
|
||||||
delete discovery;
|
delete discovery;
|
||||||
delete lib;
|
UpnpClientGlobalFinish();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,7 +195,7 @@ void
|
|||||||
UpnpDatabase::Close()
|
UpnpDatabase::Close()
|
||||||
{
|
{
|
||||||
delete discovery;
|
delete discovery;
|
||||||
delete lib;
|
UpnpClientGlobalFinish();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -277,7 +273,7 @@ UpnpDatabase::SearchSongs(const ContentDirectoryService &server,
|
|||||||
return true;
|
return true;
|
||||||
|
|
||||||
std::list<std::string> searchcaps;
|
std::list<std::string> searchcaps;
|
||||||
if (!server.getSearchCapabilities(lib->getclh(), searchcaps, error))
|
if (!server.getSearchCapabilities(handle, searchcaps, error))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (searchcaps.empty())
|
if (searchcaps.empty())
|
||||||
@ -344,7 +340,7 @@ UpnpDatabase::SearchSongs(const ContentDirectoryService &server,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return server.search(lib->getclh(),
|
return server.search(handle,
|
||||||
objid, cond.c_str(), dirbuf,
|
objid, cond.c_str(), dirbuf,
|
||||||
error);
|
error);
|
||||||
}
|
}
|
||||||
@ -431,7 +427,7 @@ UpnpDatabase::ReadNode(const ContentDirectoryService &server,
|
|||||||
Error &error) const
|
Error &error) const
|
||||||
{
|
{
|
||||||
UPnPDirContent dirbuf;
|
UPnPDirContent dirbuf;
|
||||||
if (!server.getMetadata(lib->getclh(), objid, dirbuf, error))
|
if (!server.getMetadata(handle, objid, dirbuf, error))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (dirbuf.objects.size() == 1) {
|
if (dirbuf.objects.size() == 1) {
|
||||||
@ -485,8 +481,6 @@ UpnpDatabase::Namei(const ContentDirectoryService &server,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const UpnpClient_Handle handle = lib->getclh();
|
|
||||||
|
|
||||||
std::string objid(rootid);
|
std::string objid(rootid);
|
||||||
|
|
||||||
// Walk the path elements, read each directory and try to find the next one
|
// Walk the path elements, read each directory and try to find the next one
|
||||||
@ -663,7 +657,7 @@ UpnpDatabase::VisitServer(const ContentDirectoryService &server,
|
|||||||
and loop here, but it's not useful as mpd will only return
|
and loop here, but it's not useful as mpd will only return
|
||||||
data to the client when we're done anyway. */
|
data to the client when we're done anyway. */
|
||||||
UPnPDirContent dirbuf;
|
UPnPDirContent dirbuf;
|
||||||
if (!server.readDir(lib->getclh(), tdirent.m_id.c_str(), dirbuf,
|
if (!server.readDir(handle, tdirent.m_id.c_str(), dirbuf,
|
||||||
error))
|
error))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -18,31 +18,21 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "upnpplib.hxx"
|
#include "ClientInit.hxx"
|
||||||
|
#include "Init.hxx"
|
||||||
#include "Callback.hxx"
|
#include "Callback.hxx"
|
||||||
#include "Domain.hxx"
|
#include "Domain.hxx"
|
||||||
#include "Init.hxx"
|
#include "thread/Mutex.hxx"
|
||||||
#include "Log.hxx"
|
#include "util/Error.hxx"
|
||||||
|
|
||||||
#include <upnp/upnptools.h>
|
#include <upnp/upnptools.h>
|
||||||
|
|
||||||
LibUPnP::LibUPnP()
|
static Mutex upnp_client_init_mutex;
|
||||||
{
|
static unsigned upnp_client_ref;
|
||||||
if (!UpnpGlobalInit(init_error))
|
static UpnpClient_Handle upnp_client_handle;
|
||||||
return;
|
|
||||||
|
|
||||||
auto code = UpnpRegisterClient(o_callback, nullptr, &m_clh);
|
static int
|
||||||
if (code != UPNP_E_SUCCESS) {
|
UpnpClientCallback(Upnp_EventType et, void *evp, void *cookie)
|
||||||
UpnpGlobalFinish();
|
|
||||||
init_error.Format(upnp_domain, code,
|
|
||||||
"UpnpRegisterClient() failed: %s",
|
|
||||||
UpnpGetErrorMessage(code));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
LibUPnP::o_callback(Upnp_EventType et, void* evp, void* cookie)
|
|
||||||
{
|
{
|
||||||
if (cookie == nullptr)
|
if (cookie == nullptr)
|
||||||
/* this is the cookie passed to UpnpRegisterClient();
|
/* this is the cookie passed to UpnpRegisterClient();
|
||||||
@ -54,7 +44,50 @@ LibUPnP::o_callback(Upnp_EventType et, void* evp, void* cookie)
|
|||||||
return callback.Invoke(et, evp);
|
return callback.Invoke(et, evp);
|
||||||
}
|
}
|
||||||
|
|
||||||
LibUPnP::~LibUPnP()
|
static bool
|
||||||
|
DoInit(Error &error)
|
||||||
{
|
{
|
||||||
|
auto code = UpnpRegisterClient(UpnpClientCallback, nullptr,
|
||||||
|
&upnp_client_handle);
|
||||||
|
if (code != UPNP_E_SUCCESS) {
|
||||||
|
error.Format(upnp_domain, code,
|
||||||
|
"UpnpRegisterClient() failed: %s",
|
||||||
|
UpnpGetErrorMessage(code));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
UpnpClientGlobalInit(UpnpClient_Handle &handle, Error &error)
|
||||||
|
{
|
||||||
|
if (!UpnpGlobalInit(error))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
upnp_client_init_mutex.lock();
|
||||||
|
bool success = upnp_client_ref > 0 || DoInit(error);
|
||||||
|
upnp_client_init_mutex.unlock();
|
||||||
|
|
||||||
|
if (success) {
|
||||||
|
++upnp_client_ref;
|
||||||
|
handle = upnp_client_handle;
|
||||||
|
} else
|
||||||
|
UpnpGlobalFinish();
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
UpnpClientGlobalFinish()
|
||||||
|
{
|
||||||
|
upnp_client_init_mutex.lock();
|
||||||
|
|
||||||
|
assert(upnp_client_ref > 0);
|
||||||
|
if (--upnp_client_ref == 0)
|
||||||
|
UpnpUnRegisterClient(upnp_client_handle);
|
||||||
|
|
||||||
|
upnp_client_init_mutex.unlock();
|
||||||
|
|
||||||
UpnpGlobalFinish();
|
UpnpGlobalFinish();
|
||||||
}
|
}
|
@ -17,43 +17,19 @@
|
|||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _LIBUPNP_H_X_INCLUDED_
|
#ifndef MPD_UPNP_CLIENT_INIT_HXX
|
||||||
#define _LIBUPNP_H_X_INCLUDED_
|
#define MPD_UPNP_CLIENT_INIT_HXX
|
||||||
|
|
||||||
#include "util/Error.hxx"
|
#include "check.h"
|
||||||
|
|
||||||
#include <upnp/upnp.h>
|
#include <upnp/upnp.h>
|
||||||
|
|
||||||
/** Our link to libupnp. Initialize and keep the handle around */
|
class Error;
|
||||||
class LibUPnP {
|
|
||||||
Error init_error;
|
|
||||||
UpnpClient_Handle m_clh;
|
|
||||||
|
|
||||||
static int o_callback(Upnp_EventType, void *, void *);
|
bool
|
||||||
|
UpnpClientGlobalInit(UpnpClient_Handle &handle, Error &error);
|
||||||
|
|
||||||
public:
|
void
|
||||||
LibUPnP();
|
UpnpClientGlobalFinish();
|
||||||
|
|
||||||
LibUPnP(const LibUPnP &) = delete;
|
#endif
|
||||||
LibUPnP &operator=(const LibUPnP &) = delete;
|
|
||||||
|
|
||||||
~LibUPnP();
|
|
||||||
|
|
||||||
/** Check state after initialization */
|
|
||||||
bool ok() const
|
|
||||||
{
|
|
||||||
return !init_error.IsDefined();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Retrieve init error if state not ok */
|
|
||||||
const Error &GetInitError() const {
|
|
||||||
return init_error;
|
|
||||||
}
|
|
||||||
|
|
||||||
UpnpClient_Handle getclh()
|
|
||||||
{
|
|
||||||
return m_clh;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif /* _LIBUPNP.H_X_INCLUDED_ */
|
|
Loading…
Reference in New Issue
Block a user