lib/upnp: use C++ exceptions instead of class Error
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user