Merge branch 'master' of https://github.com/grpc/grpc into bazel-0.26
commit
29b5a83920
146 changed files with 2793 additions and 2348 deletions
@ -0,0 +1,491 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2015 gRPC authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
/// A ClientContext allows the person implementing a service client to:
|
||||||
|
///
|
||||||
|
/// - Add custom metadata key-value pairs that will propagated to the server
|
||||||
|
/// side.
|
||||||
|
/// - Control call settings such as compression and authentication.
|
||||||
|
/// - Initial and trailing metadata coming from the server.
|
||||||
|
/// - Get performance metrics (ie, census).
|
||||||
|
///
|
||||||
|
/// 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 \a grpc::CreateCustomChannel).
|
||||||
|
///
|
||||||
|
/// \warning ClientContext instances should \em not be reused across rpcs.
|
||||||
|
|
||||||
|
#ifndef GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_IMPL_H |
||||||
|
#define GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_IMPL_H |
||||||
|
|
||||||
|
#include <map> |
||||||
|
#include <memory> |
||||||
|
#include <mutex> |
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include <grpc/impl/codegen/compression_types.h> |
||||||
|
#include <grpc/impl/codegen/propagation_bits.h> |
||||||
|
#include <grpcpp/impl/codegen/client_interceptor.h> |
||||||
|
#include <grpcpp/impl/codegen/config.h> |
||||||
|
#include <grpcpp/impl/codegen/core_codegen_interface.h> |
||||||
|
#include <grpcpp/impl/codegen/create_auth_context.h> |
||||||
|
#include <grpcpp/impl/codegen/metadata_map.h> |
||||||
|
#include <grpcpp/impl/codegen/rpc_method.h> |
||||||
|
#include <grpcpp/impl/codegen/security/auth_context.h> |
||||||
|
#include <grpcpp/impl/codegen/slice.h> |
||||||
|
#include <grpcpp/impl/codegen/status.h> |
||||||
|
#include <grpcpp/impl/codegen/string_ref.h> |
||||||
|
#include <grpcpp/impl/codegen/sync.h> |
||||||
|
#include <grpcpp/impl/codegen/time.h> |
||||||
|
|
||||||
|
struct census_context; |
||||||
|
struct grpc_call; |
||||||
|
|
||||||
|
namespace grpc { |
||||||
|
|
||||||
|
class ChannelInterface; |
||||||
|
|
||||||
|
namespace internal { |
||||||
|
class RpcMethod; |
||||||
|
class CallOpClientRecvStatus; |
||||||
|
class CallOpRecvInitialMetadata; |
||||||
|
template <class InputMessage, class OutputMessage> |
||||||
|
class BlockingUnaryCallImpl; |
||||||
|
template <class InputMessage, class OutputMessage> |
||||||
|
class CallbackUnaryCallImpl; |
||||||
|
template <class Request, class Response> |
||||||
|
class ClientCallbackReaderWriterImpl; |
||||||
|
template <class Response> |
||||||
|
class ClientCallbackReaderImpl; |
||||||
|
template <class Request> |
||||||
|
class ClientCallbackWriterImpl; |
||||||
|
class ClientCallbackUnaryImpl; |
||||||
|
} // namespace internal
|
||||||
|
|
||||||
|
template <class R> |
||||||
|
class ClientReader; |
||||||
|
template <class W> |
||||||
|
class ClientWriter; |
||||||
|
template <class W, class R> |
||||||
|
class ClientReaderWriter; |
||||||
|
template <class R> |
||||||
|
class ClientAsyncReader; |
||||||
|
template <class W> |
||||||
|
class ClientAsyncWriter; |
||||||
|
template <class W, class R> |
||||||
|
class ClientAsyncReaderWriter; |
||||||
|
template <class R> |
||||||
|
class ClientAsyncResponseReader; |
||||||
|
|
||||||
|
namespace testing { |
||||||
|
class InteropClientContextInspector; |
||||||
|
} // namespace testing
|
||||||
|
} // namespace grpc
|
||||||
|
namespace grpc_impl { |
||||||
|
|
||||||
|
class CallCredentials; |
||||||
|
class Channel; |
||||||
|
class CompletionQueue; |
||||||
|
class ServerContext; |
||||||
|
|
||||||
|
/// Options for \a ClientContext::FromServerContext specifying which traits from
|
||||||
|
/// the \a ServerContext to propagate (copy) from it into a new \a
|
||||||
|
/// ClientContext.
|
||||||
|
///
|
||||||
|
/// \see ClientContext::FromServerContext
|
||||||
|
class PropagationOptions { |
||||||
|
public: |
||||||
|
PropagationOptions() : propagate_(GRPC_PROPAGATE_DEFAULTS) {} |
||||||
|
|
||||||
|
PropagationOptions& enable_deadline_propagation() { |
||||||
|
propagate_ |= GRPC_PROPAGATE_DEADLINE; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
PropagationOptions& disable_deadline_propagation() { |
||||||
|
propagate_ &= ~GRPC_PROPAGATE_DEADLINE; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
PropagationOptions& enable_census_stats_propagation() { |
||||||
|
propagate_ |= GRPC_PROPAGATE_CENSUS_STATS_CONTEXT; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
PropagationOptions& disable_census_stats_propagation() { |
||||||
|
propagate_ &= ~GRPC_PROPAGATE_CENSUS_STATS_CONTEXT; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
PropagationOptions& enable_census_tracing_propagation() { |
||||||
|
propagate_ |= GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
PropagationOptions& disable_census_tracing_propagation() { |
||||||
|
propagate_ &= ~GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
PropagationOptions& enable_cancellation_propagation() { |
||||||
|
propagate_ |= GRPC_PROPAGATE_CANCELLATION; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
PropagationOptions& disable_cancellation_propagation() { |
||||||
|
propagate_ &= ~GRPC_PROPAGATE_CANCELLATION; |
||||||
|
return *this; |
||||||
|
} |
||||||
|
|
||||||
|
uint32_t c_bitmask() const { return propagate_; } |
||||||
|
|
||||||
|
private: |
||||||
|
uint32_t propagate_; |
||||||
|
}; |
||||||
|
|
||||||
|
/// A ClientContext allows the person implementing a service client to:
|
||||||
|
///
|
||||||
|
/// - Add custom metadata key-value pairs that will propagated to the server
|
||||||
|
/// side.
|
||||||
|
/// - Control call settings such as compression and authentication.
|
||||||
|
/// - Initial and trailing metadata coming from the server.
|
||||||
|
/// - Get performance metrics (ie, census).
|
||||||
|
///
|
||||||
|
/// 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 \a grpc::CreateCustomChannel).
|
||||||
|
///
|
||||||
|
/// \warning ClientContext instances should \em not be reused across rpcs.
|
||||||
|
/// \warning The ClientContext instance used for creating an rpc must remain
|
||||||
|
/// alive and valid for the lifetime of the rpc.
|
||||||
|
class ClientContext { |
||||||
|
public: |
||||||
|
ClientContext(); |
||||||
|
~ClientContext(); |
||||||
|
|
||||||
|
/// Create a new \a ClientContext as a child of an incoming server call,
|
||||||
|
/// according to \a options (\see PropagationOptions).
|
||||||
|
///
|
||||||
|
/// \param server_context The source server context to use as the basis for
|
||||||
|
/// constructing the client context.
|
||||||
|
/// \param options The options controlling what to copy from the \a
|
||||||
|
/// server_context.
|
||||||
|
///
|
||||||
|
/// \return A newly constructed \a ClientContext instance based on \a
|
||||||
|
/// server_context, with traits propagated (copied) according to \a options.
|
||||||
|
static std::unique_ptr<ClientContext> FromServerContext( |
||||||
|
const grpc_impl::ServerContext& server_context, |
||||||
|
PropagationOptions options = PropagationOptions()); |
||||||
|
|
||||||
|
/// Add the (\a meta_key, \a meta_value) pair to the metadata associated with
|
||||||
|
/// a client call. These are made available at the server side by the \a
|
||||||
|
/// grpc::ServerContext::client_metadata() method.
|
||||||
|
///
|
||||||
|
/// \warning This method should only be called before invoking the rpc.
|
||||||
|
///
|
||||||
|
/// \param meta_key The metadata key. If \a meta_value is binary data, it must
|
||||||
|
/// end in "-bin".
|
||||||
|
/// \param meta_value The 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
|
||||||
|
void AddMetadata(const grpc::string& meta_key, |
||||||
|
const grpc::string& meta_value); |
||||||
|
|
||||||
|
/// Return a collection of initial metadata key-value pairs. Note that keys
|
||||||
|
/// may happen more than once (ie, a \a std::multimap is returned).
|
||||||
|
///
|
||||||
|
/// \warning This method should only be called after initial metadata has been
|
||||||
|
/// received. For streaming calls, see \a
|
||||||
|
/// ClientReaderInterface::WaitForInitialMetadata().
|
||||||
|
///
|
||||||
|
/// \return A multimap of initial metadata key-value pairs from the server.
|
||||||
|
const std::multimap<grpc::string_ref, grpc::string_ref>& |
||||||
|
GetServerInitialMetadata() const { |
||||||
|
GPR_CODEGEN_ASSERT(initial_metadata_received_); |
||||||
|
return *recv_initial_metadata_.map(); |
||||||
|
} |
||||||
|
|
||||||
|
/// Return a collection of trailing metadata key-value pairs. Note that keys
|
||||||
|
/// may happen more than once (ie, a \a std::multimap is returned).
|
||||||
|
///
|
||||||
|
/// \warning This method is only callable once the stream has finished.
|
||||||
|
///
|
||||||
|
/// \return A multimap of metadata trailing key-value pairs from the server.
|
||||||
|
const std::multimap<grpc::string_ref, grpc::string_ref>& |
||||||
|
GetServerTrailingMetadata() const { |
||||||
|
// TODO(yangg) check finished
|
||||||
|
return *trailing_metadata_.map(); |
||||||
|
} |
||||||
|
|
||||||
|
/// Set the deadline for the client call.
|
||||||
|
///
|
||||||
|
/// \warning This method should only be called before invoking the rpc.
|
||||||
|
///
|
||||||
|
/// \param deadline the deadline for the client call. Units are determined by
|
||||||
|
/// the type used. The deadline is an absolute (not relative) time.
|
||||||
|
template <typename T> |
||||||
|
void set_deadline(const T& deadline) { |
||||||
|
grpc::TimePoint<T> deadline_tp(deadline); |
||||||
|
deadline_ = deadline_tp.raw_time(); |
||||||
|
} |
||||||
|
|
||||||
|
/// EXPERIMENTAL: Indicate that this request is idempotent.
|
||||||
|
/// By default, RPCs are assumed to <i>not</i> be idempotent.
|
||||||
|
///
|
||||||
|
/// If true, the gRPC library assumes that it's safe to initiate
|
||||||
|
/// this RPC multiple times.
|
||||||
|
void set_idempotent(bool idempotent) { idempotent_ = idempotent; } |
||||||
|
|
||||||
|
/// EXPERIMENTAL: Set this request to be cacheable.
|
||||||
|
/// If set, grpc is free to use the HTTP GET verb for sending the request,
|
||||||
|
/// with the possibility of receiving a cached response.
|
||||||
|
void set_cacheable(bool cacheable) { cacheable_ = cacheable; } |
||||||
|
|
||||||
|
/// EXPERIMENTAL: 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.
|
||||||
|
void set_wait_for_ready(bool wait_for_ready) { |
||||||
|
wait_for_ready_ = wait_for_ready; |
||||||
|
wait_for_ready_explicitly_set_ = true; |
||||||
|
} |
||||||
|
|
||||||
|
/// DEPRECATED: Use set_wait_for_ready() instead.
|
||||||
|
void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); } |
||||||
|
|
||||||
|
/// Return the deadline for the client call.
|
||||||
|
std::chrono::system_clock::time_point deadline() const { |
||||||
|
return grpc::Timespec2Timepoint(deadline_); |
||||||
|
} |
||||||
|
|
||||||
|
/// Return a \a gpr_timespec representation of the client call's deadline.
|
||||||
|
gpr_timespec raw_deadline() const { return deadline_; } |
||||||
|
|
||||||
|
/// Set the per call authority header (see
|
||||||
|
/// https://tools.ietf.org/html/rfc7540#section-8.1.2.3).
|
||||||
|
void set_authority(const grpc::string& authority) { authority_ = authority; } |
||||||
|
|
||||||
|
/// Return the authentication context for this client call.
|
||||||
|
///
|
||||||
|
/// \see grpc::AuthContext.
|
||||||
|
std::shared_ptr<const grpc::AuthContext> auth_context() const { |
||||||
|
if (auth_context_.get() == nullptr) { |
||||||
|
auth_context_ = grpc::CreateAuthContext(call_); |
||||||
|
} |
||||||
|
return auth_context_; |
||||||
|
} |
||||||
|
|
||||||
|
/// 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.
|
||||||
|
///
|
||||||
|
/// \see https://grpc.io/docs/guides/auth.html
|
||||||
|
void set_credentials( |
||||||
|
const std::shared_ptr<grpc_impl::CallCredentials>& creds) { |
||||||
|
creds_ = creds; |
||||||
|
} |
||||||
|
|
||||||
|
/// 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.
|
||||||
|
grpc_compression_algorithm compression_algorithm() const { |
||||||
|
return compression_algorithm_; |
||||||
|
} |
||||||
|
|
||||||
|
/// Set \a algorithm to be the compression algorithm used for the client call.
|
||||||
|
///
|
||||||
|
/// \param algorithm The compression algorithm used for the client call.
|
||||||
|
void set_compression_algorithm(grpc_compression_algorithm algorithm); |
||||||
|
|
||||||
|
/// Flag whether the initial metadata should be \a corked
|
||||||
|
///
|
||||||
|
/// If \a 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.
|
||||||
|
///
|
||||||
|
/// \param corked The flag indicating whether the initial metadata is to be
|
||||||
|
/// corked or not.
|
||||||
|
void set_initial_metadata_corked(bool corked) { |
||||||
|
initial_metadata_corked_ = corked; |
||||||
|
} |
||||||
|
|
||||||
|
/// Return the peer uri in a string.
|
||||||
|
///
|
||||||
|
/// \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.
|
||||||
|
///
|
||||||
|
/// \return The call's peer URI.
|
||||||
|
grpc::string peer() const; |
||||||
|
|
||||||
|
/// Get and set census context.
|
||||||
|
void set_census_context(struct census_context* ccp) { census_context_ = ccp; } |
||||||
|
struct census_context* census_context() const { |
||||||
|
return census_context_; |
||||||
|
} |
||||||
|
|
||||||
|
/// 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).
|
||||||
|
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 ~GlobalCallbacks() {} |
||||||
|
virtual void DefaultConstructor(ClientContext* context) = 0; |
||||||
|
virtual void Destructor(ClientContext* context) = 0; |
||||||
|
}; |
||||||
|
static void SetGlobalCallbacks(GlobalCallbacks* callbacks); |
||||||
|
|
||||||
|
/// Should be used for framework-level extensions only.
|
||||||
|
/// Applications never need to call this method.
|
||||||
|
grpc_call* c_call() { return call_; } |
||||||
|
|
||||||
|
/// 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.
|
||||||
|
grpc::string debug_error_string() const { return debug_error_string_; } |
||||||
|
|
||||||
|
private: |
||||||
|
// Disallow copy and assign.
|
||||||
|
ClientContext(const ClientContext&); |
||||||
|
ClientContext& operator=(const ClientContext&); |
||||||
|
|
||||||
|
friend class ::grpc::testing::InteropClientContextInspector; |
||||||
|
friend class ::grpc::internal::CallOpClientRecvStatus; |
||||||
|
friend class ::grpc::internal::CallOpRecvInitialMetadata; |
||||||
|
friend class ::grpc_impl::Channel; |
||||||
|
template <class R> |
||||||
|
friend class ::grpc::ClientReader; |
||||||
|
template <class W> |
||||||
|
friend class ::grpc::ClientWriter; |
||||||
|
template <class W, class R> |
||||||
|
friend class ::grpc::ClientReaderWriter; |
||||||
|
template <class R> |
||||||
|
friend class ::grpc::ClientAsyncReader; |
||||||
|
template <class W> |
||||||
|
friend class ::grpc::ClientAsyncWriter; |
||||||
|
template <class W, class R> |
||||||
|
friend class ::grpc::ClientAsyncReaderWriter; |
||||||
|
template <class R> |
||||||
|
friend class ::grpc::ClientAsyncResponseReader; |
||||||
|
template <class InputMessage, class OutputMessage> |
||||||
|
friend class ::grpc::internal::BlockingUnaryCallImpl; |
||||||
|
template <class InputMessage, class OutputMessage> |
||||||
|
friend class ::grpc::internal::CallbackUnaryCallImpl; |
||||||
|
template <class Request, class Response> |
||||||
|
friend class ::grpc::internal::ClientCallbackReaderWriterImpl; |
||||||
|
template <class Response> |
||||||
|
friend class ::grpc::internal::ClientCallbackReaderImpl; |
||||||
|
template <class Request> |
||||||
|
friend class ::grpc::internal::ClientCallbackWriterImpl; |
||||||
|
friend class ::grpc::internal::ClientCallbackUnaryImpl; |
||||||
|
|
||||||
|
// Used by friend class CallOpClientRecvStatus
|
||||||
|
void set_debug_error_string(const grpc::string& debug_error_string) { |
||||||
|
debug_error_string_ = debug_error_string; |
||||||
|
} |
||||||
|
|
||||||
|
grpc_call* call() const { return call_; } |
||||||
|
void set_call(grpc_call* call, |
||||||
|
const std::shared_ptr<::grpc_impl::Channel>& channel); |
||||||
|
|
||||||
|
grpc::experimental::ClientRpcInfo* set_client_rpc_info( |
||||||
|
const char* method, grpc::internal::RpcMethod::RpcType type, |
||||||
|
grpc::ChannelInterface* channel, |
||||||
|
const std::vector<std::unique_ptr< |
||||||
|
grpc::experimental::ClientInterceptorFactoryInterface>>& creators, |
||||||
|
size_t interceptor_pos) { |
||||||
|
rpc_info_ = grpc::experimental::ClientRpcInfo(this, type, method, channel); |
||||||
|
rpc_info_.RegisterInterceptors(creators, interceptor_pos); |
||||||
|
return &rpc_info_; |
||||||
|
} |
||||||
|
|
||||||
|
uint32_t initial_metadata_flags() const { |
||||||
|
return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) | |
||||||
|
(wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) | |
||||||
|
(cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) | |
||||||
|
(wait_for_ready_explicitly_set_ |
||||||
|
? GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET |
||||||
|
: 0) | |
||||||
|
(initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0); |
||||||
|
} |
||||||
|
|
||||||
|
grpc::string authority() { return authority_; } |
||||||
|
|
||||||
|
void SendCancelToInterceptors(); |
||||||
|
|
||||||
|
bool initial_metadata_received_; |
||||||
|
bool wait_for_ready_; |
||||||
|
bool wait_for_ready_explicitly_set_; |
||||||
|
bool idempotent_; |
||||||
|
bool cacheable_; |
||||||
|
std::shared_ptr<::grpc_impl::Channel> channel_; |
||||||
|
grpc::internal::Mutex mu_; |
||||||
|
grpc_call* call_; |
||||||
|
bool call_canceled_; |
||||||
|
gpr_timespec deadline_; |
||||||
|
grpc::string authority_; |
||||||
|
std::shared_ptr<grpc_impl::CallCredentials> creds_; |
||||||
|
mutable std::shared_ptr<const grpc::AuthContext> auth_context_; |
||||||
|
struct census_context* census_context_; |
||||||
|
std::multimap<grpc::string, grpc::string> send_initial_metadata_; |
||||||
|
mutable grpc::internal::MetadataMap recv_initial_metadata_; |
||||||
|
mutable grpc::internal::MetadataMap trailing_metadata_; |
||||||
|
|
||||||
|
grpc_call* propagate_from_call_; |
||||||
|
PropagationOptions propagation_options_; |
||||||
|
|
||||||
|
grpc_compression_algorithm compression_algorithm_; |
||||||
|
bool initial_metadata_corked_; |
||||||
|
|
||||||
|
grpc::string debug_error_string_; |
||||||
|
|
||||||
|
grpc::experimental::ClientRpcInfo rpc_info_; |
||||||
|
}; |
||||||
|
|
||||||
|
} // namespace grpc_impl
|
||||||
|
|
||||||
|
#endif // GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_IMPL_H
|
@ -0,0 +1,376 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2019 gRPC authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_IMPL_H |
||||||
|
#define GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_IMPL_H |
||||||
|
#include <map> |
||||||
|
#include <memory> |
||||||
|
#include <vector> |
||||||
|
|
||||||
|
#include <grpc/impl/codegen/compression_types.h> |
||||||
|
|
||||||
|
#include <grpcpp/impl/codegen/call.h> |
||||||
|
#include <grpcpp/impl/codegen/call_op_set.h> |
||||||
|
#include <grpcpp/impl/codegen/callback_common.h> |
||||||
|
#include <grpcpp/impl/codegen/completion_queue_tag.h> |
||||||
|
#include <grpcpp/impl/codegen/config.h> |
||||||
|
#include <grpcpp/impl/codegen/create_auth_context.h> |
||||||
|
#include <grpcpp/impl/codegen/metadata_map.h> |
||||||
|
#include <grpcpp/impl/codegen/security/auth_context.h> |
||||||
|
#include <grpcpp/impl/codegen/server_interceptor.h> |
||||||
|
#include <grpcpp/impl/codegen/string_ref.h> |
||||||
|
#include <grpcpp/impl/codegen/time.h> |
||||||
|
|
||||||
|
struct grpc_metadata; |
||||||
|
struct grpc_call; |
||||||
|
struct census_context; |
||||||
|
|
||||||
|
namespace grpc_impl { |
||||||
|
class ClientContext; |
||||||
|
class CompletionQueue; |
||||||
|
class Server; |
||||||
|
} // namespace grpc_impl
|
||||||
|
namespace grpc { |
||||||
|
class GenericServerContext; |
||||||
|
class ServerInterface; |
||||||
|
template <class W, class R> |
||||||
|
class ServerAsyncReader; |
||||||
|
template <class W> |
||||||
|
class ServerAsyncWriter; |
||||||
|
template <class W> |
||||||
|
class ServerAsyncResponseWriter; |
||||||
|
template <class W, class R> |
||||||
|
class ServerAsyncReaderWriter; |
||||||
|
template <class R> |
||||||
|
class ServerReader; |
||||||
|
template <class W> |
||||||
|
class ServerWriter; |
||||||
|
|
||||||
|
namespace internal { |
||||||
|
template <class W, class R> |
||||||
|
class ServerReaderWriterBody; |
||||||
|
template <class ServiceType, class RequestType, class ResponseType> |
||||||
|
class RpcMethodHandler; |
||||||
|
template <class ServiceType, class RequestType, class ResponseType> |
||||||
|
class ClientStreamingHandler; |
||||||
|
template <class ServiceType, class RequestType, class ResponseType> |
||||||
|
class ServerStreamingHandler; |
||||||
|
template <class ServiceType, class RequestType, class ResponseType> |
||||||
|
class BidiStreamingHandler; |
||||||
|
template <class RequestType, class ResponseType> |
||||||
|
class CallbackUnaryHandler; |
||||||
|
template <class RequestType, class ResponseType> |
||||||
|
class CallbackClientStreamingHandler; |
||||||
|
template <class RequestType, class ResponseType> |
||||||
|
class CallbackServerStreamingHandler; |
||||||
|
template <class RequestType, class ResponseType> |
||||||
|
class CallbackBidiHandler; |
||||||
|
template <class Streamer, bool WriteNeeded> |
||||||
|
class TemplatedBidiStreamingHandler; |
||||||
|
template <StatusCode code> |
||||||
|
class ErrorMethodHandler; |
||||||
|
class Call; |
||||||
|
class ServerReactor; |
||||||
|
} // namespace internal
|
||||||
|
|
||||||
|
class ServerInterface; |
||||||
|
namespace testing { |
||||||
|
class InteropServerContextInspector; |
||||||
|
class ServerContextTestSpouse; |
||||||
|
} // namespace testing
|
||||||
|
} // namespace grpc
|
||||||
|
|
||||||
|
namespace grpc_impl { |
||||||
|
/// A ServerContext allows the person implementing a service handler to:
|
||||||
|
///
|
||||||
|
/// - Add custom initial and trailing metadata key-value pairs that will
|
||||||
|
/// propagated to the client side.
|
||||||
|
/// - Control call settings such as compression and authentication.
|
||||||
|
/// - Access metadata coming from the client.
|
||||||
|
/// - Get performance metrics (ie, census).
|
||||||
|
///
|
||||||
|
/// Context settings are only relevant to the call handler they are supplied to,
|
||||||
|
/// that is to say, they aren't sticky across multiple calls. Some of these
|
||||||
|
/// settings, such as the compression options, can be made persistent at server
|
||||||
|
/// construction time by specifying the appropriate \a ChannelArguments
|
||||||
|
/// to a \a grpc::ServerBuilder, via \a ServerBuilder::AddChannelArgument.
|
||||||
|
///
|
||||||
|
/// \warning ServerContext instances should \em not be reused across rpcs.
|
||||||
|
class ServerContext { |
||||||
|
public: |
||||||
|
ServerContext(); // for async calls
|
||||||
|
~ServerContext(); |
||||||
|
|
||||||
|
/// Return the deadline for the server call.
|
||||||
|
std::chrono::system_clock::time_point deadline() const { |
||||||
|
return ::grpc::Timespec2Timepoint(deadline_); |
||||||
|
} |
||||||
|
|
||||||
|
/// Return a \a gpr_timespec representation of the server call's deadline.
|
||||||
|
gpr_timespec raw_deadline() const { return deadline_; } |
||||||
|
|
||||||
|
/// Add the (\a key, \a value) pair to the initial metadata
|
||||||
|
/// associated with a server call. These are made available at the client side
|
||||||
|
/// by the \a grpc::ClientContext::GetServerInitialMetadata() method.
|
||||||
|
///
|
||||||
|
/// \warning This method should only be called before sending initial metadata
|
||||||
|
/// to the client (which can happen explicitly, or implicitly when sending a
|
||||||
|
/// a response message or status to the client).
|
||||||
|
///
|
||||||
|
/// \param key The metadata key. If \a value is binary data, it must
|
||||||
|
/// end in "-bin".
|
||||||
|
/// \param value The 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
|
||||||
|
void AddInitialMetadata(const grpc::string& key, const grpc::string& value); |
||||||
|
|
||||||
|
/// Add the (\a key, \a value) pair to the initial metadata
|
||||||
|
/// associated with a server call. These are made available at the client
|
||||||
|
/// side by the \a grpc::ClientContext::GetServerTrailingMetadata() method.
|
||||||
|
///
|
||||||
|
/// \warning This method should only be called before sending trailing
|
||||||
|
/// metadata to the client (which happens when the call is finished and a
|
||||||
|
/// status is sent to the client).
|
||||||
|
///
|
||||||
|
/// \param key The metadata key. If \a value is binary data,
|
||||||
|
/// it must end in "-bin".
|
||||||
|
/// \param value The 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
|
||||||
|
void AddTrailingMetadata(const grpc::string& key, const grpc::string& value); |
||||||
|
|
||||||
|
/// IsCancelled is always safe to call when using sync or callback API.
|
||||||
|
/// When using async API, it is only safe to call IsCancelled after
|
||||||
|
/// the AsyncNotifyWhenDone tag has been delivered.
|
||||||
|
bool IsCancelled() const; |
||||||
|
|
||||||
|
/// Cancel the Call from the server. This is a best-effort API and
|
||||||
|
/// depending on when it is called, the RPC may still appear successful to
|
||||||
|
/// the client.
|
||||||
|
/// For example, if TryCancel() is called on a separate thread, it might race
|
||||||
|
/// with the server handler which might return success to the client before
|
||||||
|
/// TryCancel() was even started by the thread.
|
||||||
|
///
|
||||||
|
/// It is the caller's responsibility to prevent such races and ensure that if
|
||||||
|
/// TryCancel() is called, the serverhandler must return Status::CANCELLED.
|
||||||
|
/// The only exception is that if the serverhandler is already returning an
|
||||||
|
/// error status code, it is ok to not return Status::CANCELLED even if
|
||||||
|
/// TryCancel() was called.
|
||||||
|
///
|
||||||
|
/// 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).
|
||||||
|
void TryCancel() const; |
||||||
|
|
||||||
|
/// Return a collection of initial metadata key-value pairs sent from the
|
||||||
|
/// client. Note that keys may happen more than
|
||||||
|
/// once (ie, a \a std::multimap is returned).
|
||||||
|
///
|
||||||
|
/// It is safe to use this method after initial metadata has been received,
|
||||||
|
/// Calls always begin with the client sending initial metadata, so this is
|
||||||
|
/// safe to access as soon as the call has begun on the server side.
|
||||||
|
///
|
||||||
|
/// \return A multimap of initial metadata key-value pairs from the server.
|
||||||
|
const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata() |
||||||
|
const { |
||||||
|
return *client_metadata_.map(); |
||||||
|
} |
||||||
|
|
||||||
|
/// Return the compression algorithm to be used by the server call.
|
||||||
|
grpc_compression_level compression_level() const { |
||||||
|
return compression_level_; |
||||||
|
} |
||||||
|
|
||||||
|
/// Set \a level to be the compression level used for the server call.
|
||||||
|
///
|
||||||
|
/// \param level The compression level used for the server call.
|
||||||
|
void set_compression_level(grpc_compression_level level) { |
||||||
|
compression_level_set_ = true; |
||||||
|
compression_level_ = level; |
||||||
|
} |
||||||
|
|
||||||
|
/// Return a bool indicating whether the compression level for this call
|
||||||
|
/// has been set (either implicitly or through a previous call to
|
||||||
|
/// \a set_compression_level.
|
||||||
|
bool compression_level_set() const { return compression_level_set_; } |
||||||
|
|
||||||
|
/// Return the compression algorithm the server call will request be used.
|
||||||
|
/// Note that the gRPC runtime may decide to ignore this request, for example,
|
||||||
|
/// due to resource constraints, or if the server is aware the client doesn't
|
||||||
|
/// support the requested algorithm.
|
||||||
|
grpc_compression_algorithm compression_algorithm() const { |
||||||
|
return compression_algorithm_; |
||||||
|
} |
||||||
|
/// Set \a algorithm to be the compression algorithm used for the server call.
|
||||||
|
///
|
||||||
|
/// \param algorithm The compression algorithm used for the server call.
|
||||||
|
void set_compression_algorithm(grpc_compression_algorithm algorithm); |
||||||
|
|
||||||
|
/// Set the serialized load reporting costs in \a cost_data for the call.
|
||||||
|
void SetLoadReportingCosts(const std::vector<grpc::string>& cost_data); |
||||||
|
|
||||||
|
/// Return the authentication context for this server call.
|
||||||
|
///
|
||||||
|
/// \see grpc::AuthContext.
|
||||||
|
std::shared_ptr<const ::grpc::AuthContext> auth_context() const { |
||||||
|
if (auth_context_.get() == nullptr) { |
||||||
|
auth_context_ = ::grpc::CreateAuthContext(call_); |
||||||
|
} |
||||||
|
return auth_context_; |
||||||
|
} |
||||||
|
|
||||||
|
/// Return the peer uri in a string.
|
||||||
|
/// 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.
|
||||||
|
grpc::string peer() const; |
||||||
|
|
||||||
|
/// Get the census context associated with this server call.
|
||||||
|
const struct census_context* census_context() const; |
||||||
|
|
||||||
|
/// Async only. Has to be called before the rpc starts.
|
||||||
|
/// Returns the tag in completion queue when the rpc finishes.
|
||||||
|
/// IsCancelled() can then be called to check whether the rpc was cancelled.
|
||||||
|
/// TODO(vjpai): Fix this so that the tag is returned even if the call never
|
||||||
|
/// starts (https://github.com/grpc/grpc/issues/10136).
|
||||||
|
void AsyncNotifyWhenDone(void* tag) { |
||||||
|
has_notify_when_done_tag_ = true; |
||||||
|
async_notify_when_done_tag_ = tag; |
||||||
|
} |
||||||
|
|
||||||
|
/// Should be used for framework-level extensions only.
|
||||||
|
/// Applications never need to call this method.
|
||||||
|
grpc_call* c_call() { return call_; } |
||||||
|
|
||||||
|
private: |
||||||
|
friend class ::grpc::testing::InteropServerContextInspector; |
||||||
|
friend class ::grpc::testing::ServerContextTestSpouse; |
||||||
|
friend class ::grpc::ServerInterface; |
||||||
|
friend class ::grpc_impl::Server; |
||||||
|
template <class W, class R> |
||||||
|
friend class ::grpc::ServerAsyncReader; |
||||||
|
template <class W> |
||||||
|
friend class ::grpc::ServerAsyncWriter; |
||||||
|
template <class W> |
||||||
|
friend class ::grpc::ServerAsyncResponseWriter; |
||||||
|
template <class W, class R> |
||||||
|
friend class ::grpc::ServerAsyncReaderWriter; |
||||||
|
template <class R> |
||||||
|
friend class ::grpc::ServerReader; |
||||||
|
template <class W> |
||||||
|
friend class ::grpc::ServerWriter; |
||||||
|
template <class W, class R> |
||||||
|
friend class ::grpc::internal::ServerReaderWriterBody; |
||||||
|
template <class ServiceType, class RequestType, class ResponseType> |
||||||
|
friend class ::grpc::internal::RpcMethodHandler; |
||||||
|
template <class ServiceType, class RequestType, class ResponseType> |
||||||
|
friend class ::grpc::internal::ClientStreamingHandler; |
||||||
|
template <class ServiceType, class RequestType, class ResponseType> |
||||||
|
friend class ::grpc::internal::ServerStreamingHandler; |
||||||
|
template <class Streamer, bool WriteNeeded> |
||||||
|
friend class ::grpc::internal::TemplatedBidiStreamingHandler; |
||||||
|
template <class RequestType, class ResponseType> |
||||||
|
friend class ::grpc::internal::CallbackUnaryHandler; |
||||||
|
template <class RequestType, class ResponseType> |
||||||
|
friend class ::grpc::internal::CallbackClientStreamingHandler; |
||||||
|
template <class RequestType, class ResponseType> |
||||||
|
friend class ::grpc::internal::CallbackServerStreamingHandler; |
||||||
|
template <class RequestType, class ResponseType> |
||||||
|
friend class ::grpc::internal::CallbackBidiHandler; |
||||||
|
template <::grpc::StatusCode code> |
||||||
|
friend class ::grpc::internal::ErrorMethodHandler; |
||||||
|
friend class ::grpc_impl::ClientContext; |
||||||
|
friend class ::grpc::GenericServerContext; |
||||||
|
|
||||||
|
/// Prevent copying.
|
||||||
|
ServerContext(const ServerContext&); |
||||||
|
ServerContext& operator=(const ServerContext&); |
||||||
|
|
||||||
|
class CompletionOp; |
||||||
|
|
||||||
|
void BeginCompletionOp(::grpc::internal::Call* call, |
||||||
|
std::function<void(bool)> callback, |
||||||
|
::grpc::internal::ServerReactor* reactor); |
||||||
|
/// Return the tag queued by BeginCompletionOp()
|
||||||
|
::grpc::internal::CompletionQueueTag* GetCompletionOpTag(); |
||||||
|
|
||||||
|
ServerContext(gpr_timespec deadline, grpc_metadata_array* arr); |
||||||
|
|
||||||
|
void set_call(grpc_call* call) { call_ = call; } |
||||||
|
|
||||||
|
void BindDeadlineAndMetadata(gpr_timespec deadline, grpc_metadata_array* arr); |
||||||
|
|
||||||
|
void Clear(); |
||||||
|
|
||||||
|
void Setup(gpr_timespec deadline); |
||||||
|
|
||||||
|
uint32_t initial_metadata_flags() const { return 0; } |
||||||
|
|
||||||
|
void SetCancelCallback(std::function<void()> callback); |
||||||
|
void ClearCancelCallback(); |
||||||
|
|
||||||
|
::grpc::experimental::ServerRpcInfo* set_server_rpc_info( |
||||||
|
const char* method, ::grpc::internal::RpcMethod::RpcType type, |
||||||
|
const std::vector<std::unique_ptr< |
||||||
|
::grpc::experimental::ServerInterceptorFactoryInterface>>& creators) { |
||||||
|
if (creators.size() != 0) { |
||||||
|
rpc_info_ = new ::grpc::experimental::ServerRpcInfo(this, method, type); |
||||||
|
rpc_info_->RegisterInterceptors(creators); |
||||||
|
} |
||||||
|
return rpc_info_; |
||||||
|
} |
||||||
|
|
||||||
|
CompletionOp* completion_op_; |
||||||
|
bool has_notify_when_done_tag_; |
||||||
|
void* async_notify_when_done_tag_; |
||||||
|
::grpc::internal::CallbackWithSuccessTag completion_tag_; |
||||||
|
|
||||||
|
gpr_timespec deadline_; |
||||||
|
grpc_call* call_; |
||||||
|
::grpc_impl::CompletionQueue* cq_; |
||||||
|
bool sent_initial_metadata_; |
||||||
|
mutable std::shared_ptr<const ::grpc::AuthContext> auth_context_; |
||||||
|
mutable ::grpc::internal::MetadataMap client_metadata_; |
||||||
|
std::multimap<grpc::string, grpc::string> initial_metadata_; |
||||||
|
std::multimap<grpc::string, grpc::string> trailing_metadata_; |
||||||
|
|
||||||
|
bool compression_level_set_; |
||||||
|
grpc_compression_level compression_level_; |
||||||
|
grpc_compression_algorithm compression_algorithm_; |
||||||
|
|
||||||
|
::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, |
||||||
|
::grpc::internal::CallOpSendMessage> |
||||||
|
pending_ops_; |
||||||
|
bool has_pending_ops_; |
||||||
|
|
||||||
|
::grpc::experimental::ServerRpcInfo* rpc_info_; |
||||||
|
}; |
||||||
|
} // namespace grpc_impl
|
||||||
|
#endif // GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_IMPL_H
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue