diff options
author | Bogdan Drutu <bdrutu@google.com> | 2015-12-11 19:26:52 -0800 |
---|---|---|
committer | Bogdan Drutu <bdrutu@google.com> | 2015-12-11 19:29:07 -0800 |
commit | 38d2ad6e96b5fc2f7ee3ae7e82b81983afe50874 (patch) | |
tree | 3f88f5978afa548dde9a50a6e869f8b7a5dfc723 | |
parent | 3a977a49f642a087637ddb5cb72245b0485ac665 (diff) |
Add a hook for the default constructor of ClientContext.
-rw-r--r-- | include/grpc++/client_context.h | 10 | ||||
-rw-r--r-- | src/cpp/client/client_context.cc | 19 |
2 files changed, 28 insertions, 1 deletions
diff --git a/include/grpc++/client_context.h b/include/grpc++/client_context.h index 1b6769dee1..d3f23fe047 100644 --- a/include/grpc++/client_context.h +++ b/include/grpc++/client_context.h @@ -280,6 +280,16 @@ 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. + class GlobalCallbacks { + public: + virtual void DefaultConstructor(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..f33c1e3a9f 100644 --- a/src/cpp/client/client_context.cc +++ b/src/cpp/client/client_context.cc @@ -45,12 +45,22 @@ namespace grpc { +class DefaultGlobalCallbacks GRPC_FINAL : public ClientContext::GlobalCallbacks { + public: + void DefaultConstructor(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_) { @@ -124,4 +134,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 |