lib/upnp: use C++ exceptions instead of class Error

This commit is contained in:
Max Kellermann
2016-02-07 00:29:06 +01:00
parent 6e2ad6860f
commit 3ee5093b03
11 changed files with 143 additions and 230 deletions

View File

@@ -23,10 +23,12 @@
#include "Callback.hxx"
#include "Domain.hxx"
#include "thread/Mutex.hxx"
#include "util/Error.hxx"
#include "util/RuntimeError.hxx"
#include <upnp/upnptools.h>
#include <assert.h>
static Mutex upnp_client_init_mutex;
static unsigned upnp_client_ref;
static UpnpClient_Handle upnp_client_handle;
@@ -44,40 +46,32 @@ UpnpClientCallback(Upnp_EventType et, void *evp, void *cookie)
return callback.Invoke(et, evp);
}
static bool
DoInit(Error &error)
static void
DoInit()
{
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;
if (code != UPNP_E_SUCCESS)
throw FormatRuntimeError("UpnpRegisterClient() failed: %s",
UpnpGetErrorMessage(code));
}
bool
UpnpClientGlobalInit(UpnpClient_Handle &handle, Error &error)
void
UpnpClientGlobalInit(UpnpClient_Handle &handle)
{
if (!UpnpGlobalInit(error))
return false;
UpnpGlobalInit();
bool success;
{
try {
const ScopeLock protect(upnp_client_init_mutex);
success = upnp_client_ref > 0 || DoInit(error);
if (upnp_client_ref == 0)
DoInit();
} catch (...) {
UpnpGlobalFinish();
throw;
}
if (success) {
++upnp_client_ref;
handle = upnp_client_handle;
} else
UpnpGlobalFinish();
return success;
++upnp_client_ref;
handle = upnp_client_handle;
}
void