mirror of
https://github.com/badaix/snapcast.git
synced 2025-07-18 08:57:42 +02:00
improved error handling
This commit is contained in:
parent
5cc5aa7f04
commit
635daabd8c
3 changed files with 58 additions and 43 deletions
|
@ -24,6 +24,7 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "common/snapException.h"
|
||||||
#include "common/log.h"
|
#include "common/log.h"
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,15 +37,24 @@ BrowseAvahi::BrowseAvahi() : client_(NULL), sb_(NULL)
|
||||||
|
|
||||||
|
|
||||||
BrowseAvahi::~BrowseAvahi()
|
BrowseAvahi::~BrowseAvahi()
|
||||||
|
{
|
||||||
|
cleanUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void BrowseAvahi::cleanUp()
|
||||||
{
|
{
|
||||||
if (sb_)
|
if (sb_)
|
||||||
avahi_service_browser_free(sb_);
|
avahi_service_browser_free(sb_);
|
||||||
|
sb_ = NULL;
|
||||||
|
|
||||||
if (client_)
|
if (client_)
|
||||||
avahi_client_free(client_);
|
avahi_client_free(client_);
|
||||||
|
client_ = NULL;
|
||||||
|
|
||||||
if (simple_poll)
|
if (simple_poll)
|
||||||
avahi_simple_poll_free(simple_poll);
|
avahi_simple_poll_free(simple_poll);
|
||||||
|
simple_poll = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -114,7 +124,8 @@ void BrowseAvahi::browse_callback(
|
||||||
const char *type,
|
const char *type,
|
||||||
const char *domain,
|
const char *domain,
|
||||||
AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
|
AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
|
||||||
void* userdata) {
|
void* userdata)
|
||||||
|
{
|
||||||
|
|
||||||
// AvahiClient* client = (AvahiClient*)userdata;
|
// AvahiClient* client = (AvahiClient*)userdata;
|
||||||
BrowseAvahi* browseAvahi = static_cast<BrowseAvahi*>(userdata);
|
BrowseAvahi* browseAvahi = static_cast<BrowseAvahi*>(userdata);
|
||||||
|
@ -154,7 +165,8 @@ void BrowseAvahi::browse_callback(
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void BrowseAvahi::client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
|
void BrowseAvahi::client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata)
|
||||||
|
{
|
||||||
assert(c);
|
assert(c);
|
||||||
|
|
||||||
/* Called whenever the client or server state changes */
|
/* Called whenever the client or server state changes */
|
||||||
|
@ -170,31 +182,20 @@ void BrowseAvahi::client_callback(AvahiClient *c, AvahiClientState state, AVAHI_
|
||||||
|
|
||||||
bool BrowseAvahi::browse(const std::string& serviceName, int proto, AvahiResult& result, int timeout)
|
bool BrowseAvahi::browse(const std::string& serviceName, int proto, AvahiResult& result, int timeout)
|
||||||
{
|
{
|
||||||
int error;
|
try
|
||||||
|
{
|
||||||
/* Allocate main loop object */
|
/* Allocate main loop object */
|
||||||
if (!(simple_poll = avahi_simple_poll_new()))
|
if (!(simple_poll = avahi_simple_poll_new()))
|
||||||
{
|
throw SnapException("BrowseAvahi - Failed to create simple poll object");
|
||||||
logE << "Failed to create simple poll object.\n";
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Allocate a new client */
|
/* Allocate a new client */
|
||||||
client_ = avahi_client_new(avahi_simple_poll_get(simple_poll), (AvahiClientFlags)0, client_callback, this, &error);
|
int error;
|
||||||
|
if (!(client_ = avahi_client_new(avahi_simple_poll_get(simple_poll), (AvahiClientFlags)0, client_callback, this, &error)))
|
||||||
/* Check wether creating the client object succeeded */
|
throw SnapException("BrowseAvahi - Failed to create client: " + std::string(avahi_strerror(error)));
|
||||||
if (!client_)
|
|
||||||
{
|
|
||||||
logE << "Failed to create client: " << avahi_strerror(error) << "\n";
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Create the service browser */
|
/* Create the service browser */
|
||||||
if (!(sb_ = avahi_service_browser_new(client_, AVAHI_IF_UNSPEC, proto, serviceName.c_str(), NULL, (AvahiLookupFlags)0, browse_callback, this)))
|
if (!(sb_ = avahi_service_browser_new(client_, AVAHI_IF_UNSPEC, proto, serviceName.c_str(), NULL, (AvahiLookupFlags)0, browse_callback, this)))
|
||||||
{
|
throw SnapException("BrowseAvahi - Failed to create service browser: " + std::string(avahi_strerror(avahi_client_errno(client_))));
|
||||||
logE << "Failed to create service browser: " << avahi_strerror(avahi_client_errno(client_)) << "\n";
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
result_.valid_ = false;
|
result_.valid_ = false;
|
||||||
while (timeout > 0)
|
while (timeout > 0)
|
||||||
|
@ -204,14 +205,19 @@ bool BrowseAvahi::browse(const std::string& serviceName, int proto, AvahiResult&
|
||||||
if (result_.valid_)
|
if (result_.valid_)
|
||||||
{
|
{
|
||||||
result = result_;
|
result = result_;
|
||||||
|
cleanUp();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fail:
|
cleanUp();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
cleanUp();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,7 @@ public:
|
||||||
bool browse(const std::string& serviceName, int proto, AvahiResult& result, int timeout);
|
bool browse(const std::string& serviceName, int proto, AvahiResult& result, int timeout);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void cleanUp();
|
||||||
static void resolve_callback(AvahiServiceResolver *r, AVAHI_GCC_UNUSED AvahiIfIndex interface, AVAHI_GCC_UNUSED AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, const AvahiAddress *address, uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, AVAHI_GCC_UNUSED void* userdata);
|
static void resolve_callback(AvahiServiceResolver *r, AVAHI_GCC_UNUSED AvahiIfIndex interface, AVAHI_GCC_UNUSED AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, const AvahiAddress *address, uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, AVAHI_GCC_UNUSED void* userdata);
|
||||||
static void browse_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, void* userdata);
|
static void browse_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, void* userdata);
|
||||||
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata);
|
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata);
|
||||||
|
|
|
@ -144,15 +144,23 @@ int main (int argc, char *argv[])
|
||||||
BrowseAvahi browseAvahi;
|
BrowseAvahi browseAvahi;
|
||||||
AvahiResult avahiResult;
|
AvahiResult avahiResult;
|
||||||
while (!g_terminated)
|
while (!g_terminated)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (browseAvahi.browse("_snapcast._tcp", AVAHI_PROTO_INET, avahiResult, 5000))
|
if (browseAvahi.browse("_snapcast._tcp", AVAHI_PROTO_INET, avahiResult, 5000))
|
||||||
{
|
{
|
||||||
ip = avahiResult.ip_;
|
ip = avahiResult.ip_;
|
||||||
port = avahiResult.port_;
|
port = avahiResult.port_;
|
||||||
std::cout << ip << ":" << port << "\n";
|
logO << "Found server " << ip << ":" << port << "\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
logS(kLogErr) << "Exception: " << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
usleep(500*1000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Controller> controller(new Controller());
|
std::unique_ptr<Controller> controller(new Controller());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue