aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/grpc++/client_context.h11
-rw-r--r--src/cpp/client/client_context.cc21
2 files changed, 31 insertions, 1 deletions
diff --git a/include/grpc++/client_context.h b/include/grpc++/client_context.h
index 1b6769dee1..8e7c3579e3 100644
--- a/include/grpc++/client_context.h
+++ b/include/grpc++/client_context.h
@@ -280,6 +280,17 @@ class ClientContext {
/// There is no guarantee the call will be cancelled.
void TryCancel();
+ /// Global Callbacks
+ ///
+ /// Can be set exactly once per application to install hooks whenever
+ /// a client context is constructed and destructed.
+ class GlobalCallbacks {
+ public:
+ virtual void DefaultConstructor(ClientContext* context) = 0;
+ virtual void Destructor(ClientContext* context) = 0;
+ };
+ static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
+
private:
// Disallow copy and assign.
ClientContext(const ClientContext&);
diff --git a/src/cpp/client/client_context.cc b/src/cpp/client/client_context.cc
index 9bb358b233..a4fdfcb0de 100644
--- a/src/cpp/client/client_context.cc
+++ b/src/cpp/client/client_context.cc
@@ -45,17 +45,29 @@
namespace grpc {
+class DefaultGlobalCallbacks GRPC_FINAL : public ClientContext::GlobalCallbacks {
+ public:
+ void DefaultConstructor(ClientContext* context) GRPC_OVERRIDE {}
+ void Destructor(ClientContext* context) GRPC_OVERRIDE {}
+};
+
+static DefaultGlobalCallbacks g_default_callbacks;
+static ClientContext::GlobalCallbacks* g_callbacks = &g_default_callbacks;
+
ClientContext::ClientContext()
: initial_metadata_received_(false),
call_(nullptr),
call_canceled_(false),
deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
- propagate_from_call_(nullptr) {}
+ propagate_from_call_(nullptr) {
+ g_callbacks->DefaultConstructor(this);
+}
ClientContext::~ClientContext() {
if (call_) {
grpc_call_destroy(call_);
}
+ g_callbacks->Destructor(this);
}
std::unique_ptr<ClientContext> ClientContext::FromServerContext(
@@ -124,4 +136,11 @@ grpc::string ClientContext::peer() const {
return peer;
}
+void ClientContext::SetGlobalCallbacks(GlobalCallbacks* callbacks) {
+ GPR_ASSERT(g_callbacks == &g_default_callbacks);
+ GPR_ASSERT(callbacks != NULL);
+ GPR_ASSERT(callbacks != &g_default_callbacks);
+ g_callbacks = callbacks;
+}
+
} // namespace grpc