diff --git a/client/controller.cpp b/client/controller.cpp index 1abf22aa..af6c1dc0 100644 --- a/client/controller.cpp +++ b/client/controller.cpp @@ -34,7 +34,15 @@ using namespace std; -Controller::Controller() : MessageReceiver(), active_(false), latency_(0), stream_(nullptr), decoder_(nullptr), player_(nullptr), serverSettings_(nullptr), asyncException_(false) +Controller::Controller(size_t instance) : MessageReceiver(), + instance_(instance), + active_(false), + latency_(0), + stream_(nullptr), + decoder_(nullptr), + player_(nullptr), + serverSettings_(nullptr), + asyncException_(false) { } @@ -174,7 +182,7 @@ void Controller::worker() { clientConnection_->start(); - msg::Hello hello(clientConnection_->getMacAddress()); + msg::Hello hello(clientConnection_->getMacAddress(), instance_); clientConnection_->send(&hello); msg::Time timeReq; diff --git a/client/controller.h b/client/controller.h index 1b3cf101..24287dcc 100644 --- a/client/controller.h +++ b/client/controller.h @@ -45,7 +45,7 @@ class Controller : public MessageReceiver { public: - Controller(); + Controller(size_t instance); void start(const PcmDevice& pcmDevice, const std::string& host, size_t port, int latency); void stop(); @@ -60,6 +60,7 @@ public: private: void worker(); bool sendTimeSyncMessage(long after = 1000); + size_t instance_; std::atomic active_; std::thread controllerThread_; SampleFormat sampleFormat_; diff --git a/client/snapClient.cpp b/client/snapClient.cpp index c9b003f2..003ad94f 100644 --- a/client/snapClient.cpp +++ b/client/snapClient.cpp @@ -76,6 +76,7 @@ int main (int argc, char **argv) string host(""); size_t port(1704); int latency(0); + size_t instance(1); int processPriority(-3); Switch helpSwitch("", "help", "produce help message"); @@ -86,6 +87,7 @@ int main (int argc, char **argv) Value soundcardValue("s", "soundcard", "index or name of the soundcard", "default", &soundcard); Implicit daemonOption("d", "daemon", "daemonize, optional process priority [-20..19]", -3, &processPriority); Value latencyValue("", "latency", "latency of the soundcard", 0, &latency); + Value instanceValue("i", "instance", "instance id", 1, &instance); OptionParser op("Allowed options"); op.add(helpSwitch) @@ -99,7 +101,8 @@ int main (int argc, char **argv) #ifdef HAS_DAEMON .add(daemonOption) #endif - .add(latencyValue); + .add(latencyValue) + .add(instanceValue); try { @@ -151,7 +154,7 @@ int main (int argc, char **argv) if (daemonOption.isSet()) { #ifdef HAS_DAEMON - daemonize("snapcast", "audio", "/var/run/snapclient/pid"); + daemonize("snapcast", "audio", "/var/run/snapclient/pid." + cpt::to_string(instance)); if (processPriority < -20) processPriority = -20; else if (processPriority > 19) @@ -197,7 +200,7 @@ int main (int argc, char **argv) #endif } - std::unique_ptr controller(new Controller()); + std::unique_ptr controller(new Controller(instance)); if (!g_terminated) { logO << "Latency: " << latency << "\n"; diff --git a/common/daemon.h b/common/daemon.h index e8ed6c63..866285b5 100644 --- a/common/daemon.h +++ b/common/daemon.h @@ -84,13 +84,14 @@ void daemonize(const std::string& user, const std::string& group, const std::str if (user_gid != (gid_t)-1 && user_gid != getgid() && setgid(user_gid) == -1) throw SnapException("Failed to set group " + cpt::to_string((int)user_gid)); -#ifdef FREEBSD +//#if defined(FREEBSD) && !defined(MACOS) +//#ifdef FREEBSD /// init supplementary groups /// (must be done before we change our uid) /// no need to set the new user's supplementary groups if we are already this user - if (!had_group && user_uid != getuid() && initgroups(user_name, user_gid) == -1) - throw SnapException("Failed to set supplementary groups of user \"" + user + "\""); -#endif +// if (!had_group && user_uid != getuid() && initgroups(user_name, user_gid) == -1) +// throw SnapException("Failed to set supplementary groups of user \"" + user + "\""); +//#endif /// set uid if (user_uid != (uid_t)-1 && user_uid != getuid() && setuid(user_uid) == -1) throw SnapException("Failed to set user " + user); diff --git a/message/hello.h b/message/hello.h index 44756678..ae6c71a1 100644 --- a/message/hello.h +++ b/message/hello.h @@ -34,7 +34,7 @@ public: { } - Hello(const std::string& macAddress) : JsonMessage(message_type::kHello) + Hello(const std::string& macAddress, size_t instance) : JsonMessage(message_type::kHello) { msg["MAC"] = macAddress; msg["HostName"] = ::getHostName(); @@ -42,6 +42,7 @@ public: msg["ClientName"] = "Snapclient"; msg["OS"] = ::getOS(); msg["Arch"] = ::getArch(); + msg["Instance"] = instance; msg["SnapStreamProtocolVersion"] = 2; } @@ -79,6 +80,11 @@ public: return msg["Arch"]; } + int getInstance() + { + return get("Instance", 1); + } + int getProtocolVersion() { return get("SnapStreamProtocolVersion", 1);