upnp: move library initialization to Init.cxx

Allow calling UpnpGlobalInit() multiple times.
This commit is contained in:
Max Kellermann 2014-01-26 13:53:03 +01:00
parent e57e89b9d5
commit 01f7abfc63
4 changed files with 112 additions and 17 deletions

View File

@ -217,6 +217,7 @@ src_mpd_SOURCES = \
src/Timer.cxx
UPNP_SOURCES = \
src/lib/upnp/Init.cxx src/lib/upnp/Init.hxx \
src/lib/upnp/Device.cxx src/lib/upnp/Device.hxx \
src/lib/upnp/ContentDirectoryService.cxx src/lib/upnp/ContentDirectoryService.hxx \
src/lib/upnp/Discovery.cxx src/lib/upnp/Discovery.hxx \

73
src/lib/upnp/Init.cxx Normal file
View File

@ -0,0 +1,73 @@
/*
* Copyright (C) 2003-2014 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "Init.hxx"
#include "Domain.hxx"
#include "thread/Mutex.hxx"
#include "util/Error.hxx"
#include <upnp/upnp.h>
#include <upnp/upnptools.h>
#include <upnp/ixml.h>
static Mutex upnp_init_mutex;
static unsigned upnp_ref;
static bool
DoInit(Error &error)
{
auto code = UpnpInit(0, 0);
if (code != UPNP_E_SUCCESS) {
error.Format(upnp_domain, code,
"UpnpInit() failed: %s",
UpnpGetErrorMessage(code));
return false;
}
UpnpSetMaxContentLength(2000*1024);
// Servers sometimes make error (e.g.: minidlna returns bad utf-8)
ixmlRelaxParser(1);
return true;
}
bool
UpnpGlobalInit(Error &error)
{
const ScopeLock protect(upnp_init_mutex);
if (upnp_ref == 0 && !DoInit(error))
return false;
++upnp_ref;
return true;
}
void
UpnpGlobalFinish()
{
const ScopeLock protect(upnp_init_mutex);
assert(upnp_ref > 0);
if (--upnp_ref == 0)
UpnpFinish();
}

33
src/lib/upnp/Init.hxx Normal file
View File

@ -0,0 +1,33 @@
/*
* Copyright (C) 2003-2014 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_UPNP_INIT_HXX
#define MPD_UPNP_INIT_HXX
#include "check.h"
class Error;
bool
UpnpGlobalInit(Error &error);
void
UpnpGlobalFinish();
#endif

View File

@ -21,33 +21,24 @@
#include "upnpplib.hxx"
#include "Callback.hxx"
#include "Domain.hxx"
#include "Init.hxx"
#include "Log.hxx"
#include <upnp/ixml.h>
#include <upnp/upnptools.h>
LibUPnP::LibUPnP()
{
auto code = UpnpInit(0, 0);
if (code != UPNP_E_SUCCESS) {
init_error.Format(upnp_domain, code,
"UpnpInit() failed: %s",
UpnpGetErrorMessage(code));
if (!UpnpGlobalInit(init_error))
return;
}
UpnpSetMaxContentLength(2000*1024);
code = UpnpRegisterClient(o_callback, nullptr, &m_clh);
auto code = UpnpRegisterClient(o_callback, nullptr, &m_clh);
if (code != UPNP_E_SUCCESS) {
UpnpGlobalFinish();
init_error.Format(upnp_domain, code,
"UpnpRegisterClient() failed: %s",
UpnpGetErrorMessage(code));
return;
}
// Servers sometimes make error (e.g.: minidlna returns bad utf-8)
ixmlRelaxParser(1);
}
int
@ -65,8 +56,5 @@ LibUPnP::o_callback(Upnp_EventType et, void* evp, void* cookie)
LibUPnP::~LibUPnP()
{
int error = UpnpFinish();
if (error != UPNP_E_SUCCESS)
FormatError(upnp_domain, "UpnpFinish() failed: %s",
UpnpGetErrorMessage(error));
UpnpGlobalFinish();
}