diff options
author | Max Kellermann <max@duempel.org> | 2014-01-26 13:53:03 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2014-01-26 15:23:26 +0100 |
commit | 01f7abfc6385884251cc57fa615a065f82d274df (patch) | |
tree | 48d5cd64a1dc247f41168885b07b03ce7bf4a9d8 /src/lib | |
parent | e57e89b9d59faed31abcd11453fb33af95c55f40 (diff) |
upnp: move library initialization to Init.cxx
Allow calling UpnpGlobalInit() multiple times.
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/upnp/Init.cxx | 73 | ||||
-rw-r--r-- | src/lib/upnp/Init.hxx | 33 | ||||
-rw-r--r-- | src/lib/upnp/upnpplib.cxx | 22 |
3 files changed, 111 insertions, 17 deletions
diff --git a/src/lib/upnp/Init.cxx b/src/lib/upnp/Init.cxx new file mode 100644 index 000000000..4fc606de9 --- /dev/null +++ b/src/lib/upnp/Init.cxx @@ -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(); +} diff --git a/src/lib/upnp/Init.hxx b/src/lib/upnp/Init.hxx new file mode 100644 index 000000000..b23f8e2ab --- /dev/null +++ b/src/lib/upnp/Init.hxx @@ -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 diff --git a/src/lib/upnp/upnpplib.cxx b/src/lib/upnp/upnpplib.cxx index 452c033d5..d36fc3593 100644 --- a/src/lib/upnp/upnpplib.cxx +++ b/src/lib/upnp/upnpplib.cxx @@ -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(); } |