From 853dee67639d73178f036c192ea5d98d74fb39e3 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 21 Dec 2012 02:51:35 +0000 Subject: Add Broadcaster::Transfer for scoped recursion-safe reply bundling. git-svn-id: http://svn.drobilla.net/lad/trunk/ingen@4868 a436a847-0d15-0410-975c-d299462d15a1 --- src/server/Broadcaster.hpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/server/Broadcaster.hpp') diff --git a/src/server/Broadcaster.hpp b/src/server/Broadcaster.hpp index 3af4e5b7..621b6b61 100644 --- a/src/server/Broadcaster.hpp +++ b/src/server/Broadcaster.hpp @@ -42,9 +42,31 @@ namespace Server { class Broadcaster : public Interface { public: + Broadcaster() : _bundle_depth(0) {} + void register_client(const Raul::URI& uri, SharedPtr client); bool unregister_client(const Raul::URI& uri); + /** A handle that represents a transfer of possibly several changes. + * + * This object going out of scope signifies the transfer is completed. + * This makes doing the right thing in recursive functions that send + * updates simple (e.g. Event::post_process()). + */ + struct Transfer : public Raul::Noncopyable { + Transfer(Broadcaster& b) : broadcaster(b) { + if (++broadcaster._bundle_depth == 1) { + broadcaster.bundle_begin(); + } + } + ~Transfer() { + if (--broadcaster._bundle_depth == 0) { + broadcaster.bundle_end(); + } + } + Broadcaster& broadcaster; + }; + SharedPtr client(const Raul::URI& uri); void send_plugins(const BlockFactory::Plugins& plugin_list); @@ -109,10 +131,13 @@ public: void error(const std::string& msg) { BROADCAST(error, msg); } private: + friend class Transfer; + typedef std::map< Raul::URI, SharedPtr > Clients; Glib::Mutex _clients_mutex; Clients _clients; + unsigned _bundle_depth; }; } // namespace Server -- cgit v1.2.1