Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
grpc::ClientContext Class Reference

#include <client_context.h>

Classes

class  GlobalCallbacks
 

Public Member Functions

void AddMetadata (const std::string &meta_key, const std::string &meta_value)
 
std::shared_ptr< const grpc::AuthContextauth_context () const
 
grpc_callc_call ()
 
struct census_contextcensus_context () const
 Returns the census context that has been set, or nullptr if not set. More...
 
 ClientContext ()
 
grpc_compression_algorithm compression_algorithm () const
 
std::shared_ptr< grpc::CallCredentialscredentials ()
 
std::chrono::system_clock::time_point deadline () const
 Return the deadline for the client call. More...
 
std::string debug_error_string () const
 
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerInitialMetadata () const
 
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerTrailingMetadata () const
 
std::string peer () const
 
gpr_timespec raw_deadline () const
 Return a gpr_timespec representation of the client call's deadline. More...
 
void set_authority (const std::string &authority)
 
void set_census_context (struct census_context *ccp)
 
void set_compression_algorithm (grpc_compression_algorithm algorithm)
 
void set_credentials (const std::shared_ptr< grpc::CallCredentials > &creds)
 
template<typename T >
void set_deadline (const T &deadline)
 
void set_fail_fast (bool fail_fast)
 DEPRECATED: Use set_wait_for_ready() instead. More...
 
void set_initial_metadata_corked (bool corked)
 
void set_wait_for_ready (bool wait_for_ready)
 
void TryCancel ()
 
 ~ClientContext ()
 

Static Public Member Functions

static std::unique_ptr< ClientContextFromCallbackServerContext (const grpc::CallbackServerContext &server_context, PropagationOptions options=PropagationOptions())
 
static std::unique_ptr< ClientContextFromServerContext (const grpc::ServerContextBase &server_context, PropagationOptions options=PropagationOptions())
 
static void SetGlobalCallbacks (GlobalCallbacks *callbacks)
 

Private Member Functions

std::string authority ()
 
grpc_callcall () const
 
 ClientContext (const ClientContext &)
 
uint32_t initial_metadata_flags () const
 
ClientContextoperator= (const ClientContext &)
 
void SendCancelToInterceptors ()
 
void set_call (grpc_call *call, const std::shared_ptr< grpc::Channel > &channel)
 
grpc::experimental::ClientRpcInfoset_client_rpc_info (const char *method, const char *suffix_for_stats, grpc::internal::RpcMethod::RpcType type, grpc::ChannelInterface *channel, const std::vector< std::unique_ptr< grpc::experimental::ClientInterceptorFactoryInterface >> &creators, size_t interceptor_pos)
 
void set_debug_error_string (const std::string &debug_error_string)
 

Static Private Member Functions

static std::unique_ptr< ClientContextFromInternalServerContext (const grpc::ServerContextBase &server_context, PropagationOptions options)
 

Private Attributes

std::shared_ptr< const grpc::AuthContextauth_context_
 
grpc::string authority_
 
grpc_callcall_
 
bool call_canceled_
 
struct census_contextcensus_context_
 
std::shared_ptr< grpc::Channelchannel_
 
grpc_compression_algorithm compression_algorithm_
 
std::shared_ptr< grpc::CallCredentialscreds_
 
gpr_timespec deadline_
 
std::string debug_error_string_
 
bool initial_metadata_corked_
 
bool initial_metadata_received_
 
grpc::internal::Mutex mu_
 
grpc_callpropagate_from_call_
 
PropagationOptions propagation_options_
 
grpc::internal::MetadataMap recv_initial_metadata_
 
grpc::experimental::ClientRpcInfo rpc_info_
 
std::multimap< std::string, std::string > send_initial_metadata_
 
grpc::internal::MetadataMap trailing_metadata_
 
bool wait_for_ready_
 
bool wait_for_ready_explicitly_set_
 

Friends

class grpc::Channel
 
template<class R >
class grpc::ClientAsyncReader
 
template<class W , class R >
class grpc::ClientAsyncReaderWriter
 
template<class R >
class grpc::ClientAsyncResponseReader
 
template<class W >
class grpc::ClientAsyncWriter
 
template<class R >
class grpc::ClientReader
 
template<class W , class R >
class grpc::ClientReaderWriter
 
template<class W >
class grpc::ClientWriter
 
template<class InputMessage , class OutputMessage >
class grpc::internal::BlockingUnaryCallImpl
 
template<class InputMessage , class OutputMessage >
class grpc::internal::CallbackUnaryCallImpl
 
class grpc::internal::CallOpClientRecvStatus
 
class grpc::internal::CallOpRecvInitialMetadata
 
class grpc::internal::ClientAsyncResponseReaderHelper
 
template<class Response >
class grpc::internal::ClientCallbackReaderImpl
 
template<class Request , class Response >
class grpc::internal::ClientCallbackReaderWriterImpl
 
class grpc::internal::ClientCallbackUnaryImpl
 
template<class Request >
class grpc::internal::ClientCallbackWriterImpl
 
class grpc::internal::ClientContextAccessor
 
class grpc::testing::ClientContextTestPeer
 
class grpc::testing::InteropClientContextInspector
 

Detailed Description

A ClientContext allows the person implementing a service client to:

Context settings are only relevant to the call they are invoked with, that is to say, they aren't sticky. Some of these settings, such as the compression options, can be made persistent at channel construction time (see grpc::CreateCustomChannel).

Warning
ClientContext instances should not be reused across rpcs.
The ClientContext instance used for creating an rpc must remain alive and valid for the lifetime of the rpc.

Definition at line 195 of file grpcpp/impl/codegen/client_context.h.

Constructor & Destructor Documentation

◆ ClientContext() [1/2]

grpc::ClientContext::ClientContext ( )

Definition at line 64 of file client_context.cc.

◆ ~ClientContext()

grpc::ClientContext::~ClientContext ( )

Definition at line 79 of file client_context.cc.

◆ ClientContext() [2/2]

grpc::ClientContext::ClientContext ( const ClientContext )
private

Member Function Documentation

◆ AddMetadata()

void grpc::ClientContext::AddMetadata ( const std::string &  meta_key,
const std::string &  meta_value 
)

Add the (meta_key, meta_value) pair to the metadata associated with a client call. These are made available at the server side by the grpc::ServerContext::client_metadata() method.

Warning
This method should only be called before invoking the rpc.
Parameters
meta_keyThe metadata key. If meta_value is binary data, it must end in "-bin".
meta_valueThe metadata value. If its value is binary, the key name must end in "-bin".

Metadata must conform to the following format:

Custom-Metadata -> Binary-Header / ASCII-Header
Binary-Header -> {Header-Name "-bin" } {binary value}
ASCII-Header -> Header-Name ASCII-Value
Header-Name -> 1*( %x30-39 / %x61-7A / "_" / "-" / ".") ; 0-9 a-z _ - .
ASCII-Value -> 1*( %x20-%x7E ) ; space and printable ASCII
Custom-Metadata -> Binary-Header / ASCII-Header

Definition at line 121 of file client_context.cc.

◆ auth_context()

std::shared_ptr<const grpc::AuthContext> grpc::ClientContext::auth_context ( ) const
inline

Return the authentication context for the associated client call. It is only valid to call this during the lifetime of the client call.

See also
grpc::AuthContext.

Definition at line 309 of file grpcpp/impl/codegen/client_context.h.

◆ authority()

std::string grpc::ClientContext::authority ( )
inlineprivate

Definition at line 482 of file grpcpp/impl/codegen/client_context.h.

◆ c_call()

grpc_call* grpc::ClientContext::c_call ( )
inline

Should be used for framework-level extensions only. Applications never need to call this method.

Definition at line 407 of file grpcpp/impl/codegen/client_context.h.

◆ call()

grpc_call* grpc::ClientContext::call ( ) const
inlineprivate

Definition at line 459 of file grpcpp/impl/codegen/client_context.h.

◆ census_context()

struct census_context* grpc::ClientContext::census_context ( ) const
inline

Returns the census context that has been set, or nullptr if not set.

Definition at line 378 of file grpcpp/impl/codegen/client_context.h.

◆ compression_algorithm()

grpc_compression_algorithm grpc::ClientContext::compression_algorithm ( ) const
inline

Return the compression algorithm the client call will request be used. Note that the gRPC runtime may decide to ignore this request, for example, due to resource constraints.

Definition at line 338 of file grpcpp/impl/codegen/client_context.h.

◆ credentials()

std::shared_ptr<grpc::CallCredentials> grpc::ClientContext::credentials ( )
inline

EXPERIMENTAL debugging API

Returns the credentials for the client call. This should be used only in tests and for diagnostic purposes, and should not be used by application logic.

Definition at line 333 of file grpcpp/impl/codegen/client_context.h.

◆ deadline()

std::chrono::system_clock::time_point grpc::ClientContext::deadline ( ) const
inline

Return the deadline for the client call.

Definition at line 294 of file grpcpp/impl/codegen/client_context.h.

◆ debug_error_string()

std::string grpc::ClientContext::debug_error_string ( ) const
inline

EXPERIMENTAL debugging API

if status is not ok() for an RPC, this will return a detailed string of the gRPC Core error that led to the failure. It should not be relied upon for anything other than gaining more debug data in failure cases.

Definition at line 414 of file grpcpp/impl/codegen/client_context.h.

◆ FromCallbackServerContext()

std::unique_ptr< ClientContext > grpc::ClientContext::FromCallbackServerContext ( const grpc::CallbackServerContext server_context,
PropagationOptions  options = PropagationOptions() 
)
static

Definition at line 115 of file client_context.cc.

◆ FromInternalServerContext()

std::unique_ptr< ClientContext > grpc::ClientContext::FromInternalServerContext ( const grpc::ServerContextBase server_context,
PropagationOptions  options 
)
staticprivate

Definition at line 102 of file client_context.cc.

◆ FromServerContext()

std::unique_ptr< ClientContext > grpc::ClientContext::FromServerContext ( const grpc::ServerContextBase server_context,
PropagationOptions  options = PropagationOptions() 
)
static

Create a new ClientContext as a child of an incoming server call, according to options (

See also
PropagationOptions).
Parameters
server_contextThe source server context to use as the basis for constructing the client context.
optionsThe options controlling what to copy from the server_context.
Returns
A newly constructed ClientContext instance based on server_context, with traits propagated (copied) according to options.

Definition at line 110 of file client_context.cc.

◆ GetServerInitialMetadata()

const std::multimap<grpc::string_ref, grpc::string_ref>& grpc::ClientContext::GetServerInitialMetadata ( ) const
inline

Return a collection of initial metadata key-value pairs. Note that keys may happen more than once (ie, a std::multimap is returned).

Warning
This method should only be called after initial metadata has been received. For streaming calls, see ClientReaderInterface::WaitForInitialMetadata().
Returns
A multimap of initial metadata key-value pairs from the server.

Definition at line 250 of file grpcpp/impl/codegen/client_context.h.

◆ GetServerTrailingMetadata()

const std::multimap<grpc::string_ref, grpc::string_ref>& grpc::ClientContext::GetServerTrailingMetadata ( ) const
inline

Return a collection of trailing metadata key-value pairs. Note that keys may happen more than once (ie, a std::multimap is returned).

Warning
This method is only callable once the stream has finished.
Returns
A multimap of metadata trailing key-value pairs from the server.

Definition at line 262 of file grpcpp/impl/codegen/client_context.h.

◆ initial_metadata_flags()

uint32_t grpc::ClientContext::initial_metadata_flags ( ) const
inlineprivate

Definition at line 474 of file grpcpp/impl/codegen/client_context.h.

◆ operator=()

ClientContext& grpc::ClientContext::operator= ( const ClientContext )
private

◆ peer()

std::string grpc::ClientContext::peer ( ) const

Return the peer uri in a string. It is only valid to call this during the lifetime of the client call.

Warning
This value is never authenticated or subject to any security related code. It must not be used for any authentication related functionality. Instead, use auth_context.
Returns
The call's peer URI.

Definition at line 174 of file client_context.cc.

◆ raw_deadline()

gpr_timespec grpc::ClientContext::raw_deadline ( ) const
inline

Return a gpr_timespec representation of the client call's deadline.

Definition at line 299 of file grpcpp/impl/codegen/client_context.h.

◆ SendCancelToInterceptors()

void grpc::ClientContext::SendCancelToInterceptors ( )
private

Definition at line 167 of file client_context.cc.

◆ set_authority()

void grpc::ClientContext::set_authority ( const std::string &  authority)
inline

Set the per call authority header (see https://tools.ietf.org/html/rfc7540#section-8.1.2.3).

Definition at line 303 of file grpcpp/impl/codegen/client_context.h.

◆ set_call()

void grpc::ClientContext::set_call ( grpc_call call,
const std::shared_ptr< grpc::Channel > &  channel 
)
private

Definition at line 126 of file client_context.cc.

◆ set_census_context()

void grpc::ClientContext::set_census_context ( struct census_context ccp)
inline

Sets the census context. It is only valid to call this before the client call is created. A common place of setting census context is from within the DefaultConstructor method of GlobalCallbacks.

Definition at line 375 of file grpcpp/impl/codegen/client_context.h.

◆ set_client_rpc_info()

grpc::experimental::ClientRpcInfo* grpc::ClientContext::set_client_rpc_info ( const char *  method,
const char *  suffix_for_stats,
grpc::internal::RpcMethod::RpcType  type,
grpc::ChannelInterface channel,
const std::vector< std::unique_ptr< grpc::experimental::ClientInterceptorFactoryInterface >> &  creators,
size_t  interceptor_pos 
)
inlineprivate

Definition at line 462 of file grpcpp/impl/codegen/client_context.h.

◆ set_compression_algorithm()

void grpc::ClientContext::set_compression_algorithm ( grpc_compression_algorithm  algorithm)

Set algorithm to be the compression algorithm used for the client call.

Parameters
algorithmThe compression algorithm used for the client call.

Definition at line 144 of file client_context.cc.

◆ set_credentials()

void grpc::ClientContext::set_credentials ( const std::shared_ptr< grpc::CallCredentials > &  creds)

Set credentials for the client call.

A credentials object encapsulates all the state needed by a client to authenticate with a server and make various assertions, e.g., about the client’s identity, role, or whether it is authorized to make a particular call.

It is legal to call this only before initial metadata is sent.

See also
https://grpc.io/docs/guides/auth.html

Definition at line 86 of file client_context.cc.

◆ set_deadline()

template<typename T >
void grpc::ClientContext::set_deadline ( const T deadline)
inline

Set the deadline for the client call.

Warning
This method should only be called before invoking the rpc.
Parameters
deadlinethe deadline for the client call. Units are determined by the type used. The deadline is an absolute (not relative) time.

Definition at line 274 of file grpcpp/impl/codegen/client_context.h.

◆ set_debug_error_string()

void grpc::ClientContext::set_debug_error_string ( const std::string &  debug_error_string)
inlineprivate

Definition at line 455 of file grpcpp/impl/codegen/client_context.h.

◆ set_fail_fast()

void grpc::ClientContext::set_fail_fast ( bool  fail_fast)
inline

DEPRECATED: Use set_wait_for_ready() instead.

Definition at line 291 of file grpcpp/impl/codegen/client_context.h.

◆ set_initial_metadata_corked()

void grpc::ClientContext::set_initial_metadata_corked ( bool  corked)
inline

Flag whether the initial metadata should be corked

If corked is true, then the initial metadata will be coalesced with the write of first message in the stream. As a result, any tag set for the initial metadata operation (starting a client-streaming or bidi-streaming RPC) will not actually be sent to the completion queue or delivered via Next.

Parameters
corkedThe flag indicating whether the initial metadata is to be corked or not.

Definition at line 357 of file grpcpp/impl/codegen/client_context.h.

◆ set_wait_for_ready()

void grpc::ClientContext::set_wait_for_ready ( bool  wait_for_ready)
inline

Trigger wait-for-ready or not on this request. See https://github.com/grpc/grpc/blob/master/doc/wait-for-ready.md. If set, if an RPC is made when a channel's connectivity state is TRANSIENT_FAILURE or CONNECTING, the call will not "fail fast", and the channel will wait until the channel is READY before making the call.

Definition at line 285 of file grpcpp/impl/codegen/client_context.h.

◆ SetGlobalCallbacks()

void grpc::ClientContext::SetGlobalCallbacks ( GlobalCallbacks callbacks)
static

Definition at line 184 of file client_context.cc.

◆ TryCancel()

void grpc::ClientContext::TryCancel ( )

Send a best-effort out-of-band cancel on the call associated with this client context. The call could be in any stage; e.g., if it is already finished, it may still return success.

There is no guarantee the call will be cancelled.

Note that TryCancel() does not change any of the tags that are pending on the completion queue. All pending tags will still be delivered (though their ok result may reflect the effect of cancellation).

Definition at line 157 of file client_context.cc.

Friends And Related Function Documentation

◆ grpc::Channel

friend class grpc::Channel
friend

Definition at line 425 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::ClientAsyncReader

template<class R >
friend class grpc::ClientAsyncReader
friend

Definition at line 433 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::ClientAsyncReaderWriter

template<class W , class R >
friend class grpc::ClientAsyncReaderWriter
friend

Definition at line 437 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::ClientAsyncResponseReader

template<class R >
friend class grpc::ClientAsyncResponseReader
friend

Definition at line 439 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::ClientAsyncWriter

template<class W >
friend class grpc::ClientAsyncWriter
friend

Definition at line 435 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::ClientReader

template<class R >
friend class grpc::ClientReader
friend

Definition at line 427 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::ClientReaderWriter

template<class W , class R >
friend class grpc::ClientReaderWriter
friend

Definition at line 431 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::ClientWriter

template<class W >
friend class grpc::ClientWriter
friend

Definition at line 429 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::internal::BlockingUnaryCallImpl

template<class InputMessage , class OutputMessage >
friend class grpc::internal::BlockingUnaryCallImpl
friend

Definition at line 442 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::internal::CallbackUnaryCallImpl

template<class InputMessage , class OutputMessage >
friend class grpc::internal::CallbackUnaryCallImpl
friend

Definition at line 444 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::internal::CallOpClientRecvStatus

Definition at line 423 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::internal::CallOpRecvInitialMetadata

Definition at line 424 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::internal::ClientAsyncResponseReaderHelper

Definition at line 440 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::internal::ClientCallbackReaderImpl

template<class Response >
friend class grpc::internal::ClientCallbackReaderImpl
friend

Definition at line 448 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::internal::ClientCallbackReaderWriterImpl

template<class Request , class Response >
friend class grpc::internal::ClientCallbackReaderWriterImpl
friend

Definition at line 446 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::internal::ClientCallbackUnaryImpl

Definition at line 451 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::internal::ClientCallbackWriterImpl

template<class Request >
friend class grpc::internal::ClientCallbackWriterImpl
friend

Definition at line 450 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::internal::ClientContextAccessor

friend class grpc::internal::ClientContextAccessor
friend

Definition at line 452 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::testing::ClientContextTestPeer

Definition at line 422 of file grpcpp/impl/codegen/client_context.h.

◆ grpc::testing::InteropClientContextInspector

Definition at line 421 of file grpcpp/impl/codegen/client_context.h.

Member Data Documentation

◆ auth_context_

std::shared_ptr<const grpc::AuthContext> grpc::ClientContext::auth_context_
mutableprivate

Definition at line 500 of file grpcpp/impl/codegen/client_context.h.

◆ authority_

grpc::string grpc::ClientContext::authority_
private

Definition at line 498 of file grpcpp/impl/codegen/client_context.h.

◆ call_

grpc_call* grpc::ClientContext::call_
private

Definition at line 495 of file grpcpp/impl/codegen/client_context.h.

◆ call_canceled_

bool grpc::ClientContext::call_canceled_
private

Definition at line 496 of file grpcpp/impl/codegen/client_context.h.

◆ census_context_

struct census_context* grpc::ClientContext::census_context_
private

Definition at line 501 of file grpcpp/impl/codegen/client_context.h.

◆ channel_

std::shared_ptr<grpc::Channel> grpc::ClientContext::channel_
private

Definition at line 493 of file grpcpp/impl/codegen/client_context.h.

◆ compression_algorithm_

grpc_compression_algorithm grpc::ClientContext::compression_algorithm_
private

Definition at line 509 of file grpcpp/impl/codegen/client_context.h.

◆ creds_

std::shared_ptr<grpc::CallCredentials> grpc::ClientContext::creds_
private

Definition at line 499 of file grpcpp/impl/codegen/client_context.h.

◆ deadline_

gpr_timespec grpc::ClientContext::deadline_
private

Definition at line 497 of file grpcpp/impl/codegen/client_context.h.

◆ debug_error_string_

std::string grpc::ClientContext::debug_error_string_
private

Definition at line 512 of file grpcpp/impl/codegen/client_context.h.

◆ initial_metadata_corked_

bool grpc::ClientContext::initial_metadata_corked_
private

Definition at line 510 of file grpcpp/impl/codegen/client_context.h.

◆ initial_metadata_received_

bool grpc::ClientContext::initial_metadata_received_
private

Definition at line 490 of file grpcpp/impl/codegen/client_context.h.

◆ mu_

grpc::internal::Mutex grpc::ClientContext::mu_
private

Definition at line 494 of file grpcpp/impl/codegen/client_context.h.

◆ propagate_from_call_

grpc_call* grpc::ClientContext::propagate_from_call_
private

Definition at line 506 of file grpcpp/impl/codegen/client_context.h.

◆ propagation_options_

PropagationOptions grpc::ClientContext::propagation_options_
private

Definition at line 507 of file grpcpp/impl/codegen/client_context.h.

◆ recv_initial_metadata_

grpc::internal::MetadataMap grpc::ClientContext::recv_initial_metadata_
mutableprivate

Definition at line 503 of file grpcpp/impl/codegen/client_context.h.

◆ rpc_info_

grpc::experimental::ClientRpcInfo grpc::ClientContext::rpc_info_
private

Definition at line 514 of file grpcpp/impl/codegen/client_context.h.

◆ send_initial_metadata_

std::multimap<std::string, std::string> grpc::ClientContext::send_initial_metadata_
private

Definition at line 502 of file grpcpp/impl/codegen/client_context.h.

◆ trailing_metadata_

grpc::internal::MetadataMap grpc::ClientContext::trailing_metadata_
mutableprivate

Definition at line 504 of file grpcpp/impl/codegen/client_context.h.

◆ wait_for_ready_

bool grpc::ClientContext::wait_for_ready_
private

Definition at line 491 of file grpcpp/impl/codegen/client_context.h.

◆ wait_for_ready_explicitly_set_

bool grpc::ClientContext::wait_for_ready_explicitly_set_
private

Definition at line 492 of file grpcpp/impl/codegen/client_context.h.


The documentation for this class was generated from the following files:


grpc
Author(s):
autogenerated on Fri May 16 2025 03:03:30