upnp: move library initialization to Init.cxx
Allow calling UpnpGlobalInit() multiple times.
This commit is contained in:
@@ -217,6 +217,7 @@ src_mpd_SOURCES = \
|
|||||||
src/Timer.cxx
|
src/Timer.cxx
|
||||||
|
|
||||||
UPNP_SOURCES = \
|
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/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 \
|
||||||
|
73
src/lib/upnp/Init.cxx
Normal file
73
src/lib/upnp/Init.cxx
Normal 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
33
src/lib/upnp/Init.hxx
Normal 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
|
@@ -21,33 +21,24 @@
|
|||||||
#include "upnpplib.hxx"
|
#include "upnpplib.hxx"
|
||||||
#include "Callback.hxx"
|
#include "Callback.hxx"
|
||||||
#include "Domain.hxx"
|
#include "Domain.hxx"
|
||||||
|
#include "Init.hxx"
|
||||||
#include "Log.hxx"
|
#include "Log.hxx"
|
||||||
|
|
||||||
#include <upnp/ixml.h>
|
|
||||||
#include <upnp/upnptools.h>
|
#include <upnp/upnptools.h>
|
||||||
|
|
||||||
LibUPnP::LibUPnP()
|
LibUPnP::LibUPnP()
|
||||||
{
|
{
|
||||||
auto code = UpnpInit(0, 0);
|
if (!UpnpGlobalInit(init_error))
|
||||||
if (code != UPNP_E_SUCCESS) {
|
|
||||||
init_error.Format(upnp_domain, code,
|
|
||||||
"UpnpInit() failed: %s",
|
|
||||||
UpnpGetErrorMessage(code));
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
UpnpSetMaxContentLength(2000*1024);
|
auto code = UpnpRegisterClient(o_callback, nullptr, &m_clh);
|
||||||
|
|
||||||
code = UpnpRegisterClient(o_callback, nullptr, &m_clh);
|
|
||||||
if (code != UPNP_E_SUCCESS) {
|
if (code != UPNP_E_SUCCESS) {
|
||||||
|
UpnpGlobalFinish();
|
||||||
init_error.Format(upnp_domain, code,
|
init_error.Format(upnp_domain, code,
|
||||||
"UpnpRegisterClient() failed: %s",
|
"UpnpRegisterClient() failed: %s",
|
||||||
UpnpGetErrorMessage(code));
|
UpnpGetErrorMessage(code));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Servers sometimes make error (e.g.: minidlna returns bad utf-8)
|
|
||||||
ixmlRelaxParser(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@@ -65,8 +56,5 @@ LibUPnP::o_callback(Upnp_EventType et, void* evp, void* cookie)
|
|||||||
|
|
||||||
LibUPnP::~LibUPnP()
|
LibUPnP::~LibUPnP()
|
||||||
{
|
{
|
||||||
int error = UpnpFinish();
|
UpnpGlobalFinish();
|
||||||
if (error != UPNP_E_SUCCESS)
|
|
||||||
FormatError(upnp_domain, "UpnpFinish() failed: %s",
|
|
||||||
UpnpGetErrorMessage(error));
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user