Rolled handle infrastructure into rpcc.
[invirt/third/libt4.git] / rpc / rpc.h
index 58e9381..f1eb3bc 100644 (file)
--- a/rpc/rpc.h
+++ b/rpc/rpc.h
@@ -10,6 +10,7 @@
 #include "marshall.h"
 #include "marshall_wrap.h"
 #include "connection.h"
+#include "threaded_log.h"
 
 using std::chrono::milliseconds;
 
@@ -75,6 +76,7 @@ class rpcc : private connection_delegate {
 
         std::mutex m_; // protect insert/delete to calls[]
         std::mutex chan_m_;
+        std::mutex bind_m_; // protect bind operations
 
         bool destroy_wait_ = false;
         cond destroy_wait_c_;
@@ -122,9 +124,17 @@ class rpcc : private connection_delegate {
 
         int bind(milliseconds to = rpc::to_max);
 
+        // Manages a cache of RPC connections.  Usage:
+        //     if (auto cl = rpcc::bind_cached(dst))
+        //         ret = cl->call(...);
+        // where the string dst has the form "host:port".  Because bind_cached()
+        // may block, callers should probably not hold mutexes.
+        static shared_ptr<rpcc> bind_cached(const string & destination);
+        static void unbind_cached(const string & destination);
+
         void set_reachable(bool r) { reachable_ = r; }
 
-        void cancel();
+        void cancel(lock & m_lock);
 
         template<class P, class R, typename ...Args>
         inline int call(proc_t<P> proc, R & r, const Args & ... args) {