diff --git a/.gitignore b/.gitignore index bc324b5f826..a8c58277c34 100644 --- a/.gitignore +++ b/.gitignore @@ -144,3 +144,5 @@ bm_*.json !.vscode/launch.json !.vscode/extensions.json +# Clion artifacts +cmake-build-debug/ diff --git a/BUILD b/BUILD index 4247d491fc5..29eed995008 100644 --- a/BUILD +++ b/BUILD @@ -2186,6 +2186,7 @@ grpc_cc_library( "include/grpcpp/impl/codegen/serialization_traits.h", "include/grpcpp/impl/codegen/server_callback.h", "include/grpcpp/impl/codegen/server_context.h", + "include/grpcpp/impl/codegen/server_context_impl.h", "include/grpcpp/impl/codegen/server_interceptor.h", "include/grpcpp/impl/codegen/server_interface.h", "include/grpcpp/impl/codegen/service_type.h", diff --git a/BUILD.gn b/BUILD.gn index 3041adef98a..e5396f51426 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1082,6 +1082,7 @@ config("grpc_config") { "include/grpcpp/impl/codegen/serialization_traits.h", "include/grpcpp/impl/codegen/server_callback.h", "include/grpcpp/impl/codegen/server_context.h", + "include/grpcpp/impl/codegen/server_context_impl.h", "include/grpcpp/impl/codegen/server_interceptor.h", "include/grpcpp/impl/codegen/server_interface.h", "include/grpcpp/impl/codegen/service_type.h", diff --git a/CMakeLists.txt b/CMakeLists.txt index bb5b9e58578..900b527d9fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3337,6 +3337,7 @@ foreach(_hdr include/grpcpp/impl/codegen/serialization_traits.h include/grpcpp/impl/codegen/server_callback.h include/grpcpp/impl/codegen/server_context.h + include/grpcpp/impl/codegen/server_context_impl.h include/grpcpp/impl/codegen/server_interceptor.h include/grpcpp/impl/codegen/server_interface.h include/grpcpp/impl/codegen/service_type.h @@ -3957,6 +3958,7 @@ foreach(_hdr include/grpcpp/impl/codegen/serialization_traits.h include/grpcpp/impl/codegen/server_callback.h include/grpcpp/impl/codegen/server_context.h + include/grpcpp/impl/codegen/server_context_impl.h include/grpcpp/impl/codegen/server_interceptor.h include/grpcpp/impl/codegen/server_interface.h include/grpcpp/impl/codegen/service_type.h @@ -4393,6 +4395,7 @@ foreach(_hdr include/grpcpp/impl/codegen/serialization_traits.h include/grpcpp/impl/codegen/server_callback.h include/grpcpp/impl/codegen/server_context.h + include/grpcpp/impl/codegen/server_context_impl.h include/grpcpp/impl/codegen/server_interceptor.h include/grpcpp/impl/codegen/server_interface.h include/grpcpp/impl/codegen/service_type.h @@ -4593,6 +4596,7 @@ foreach(_hdr include/grpcpp/impl/codegen/serialization_traits.h include/grpcpp/impl/codegen/server_callback.h include/grpcpp/impl/codegen/server_context.h + include/grpcpp/impl/codegen/server_context_impl.h include/grpcpp/impl/codegen/server_interceptor.h include/grpcpp/impl/codegen/server_interface.h include/grpcpp/impl/codegen/service_type.h @@ -4953,6 +4957,7 @@ foreach(_hdr include/grpcpp/impl/codegen/serialization_traits.h include/grpcpp/impl/codegen/server_callback.h include/grpcpp/impl/codegen/server_context.h + include/grpcpp/impl/codegen/server_context_impl.h include/grpcpp/impl/codegen/server_interceptor.h include/grpcpp/impl/codegen/server_interface.h include/grpcpp/impl/codegen/service_type.h diff --git a/Makefile b/Makefile index 44e18c1f474..0325374e39e 100644 --- a/Makefile +++ b/Makefile @@ -5692,6 +5692,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/serialization_traits.h \ include/grpcpp/impl/codegen/server_callback.h \ include/grpcpp/impl/codegen/server_context.h \ + include/grpcpp/impl/codegen/server_context_impl.h \ include/grpcpp/impl/codegen/server_interceptor.h \ include/grpcpp/impl/codegen/server_interface.h \ include/grpcpp/impl/codegen/service_type.h \ @@ -6320,6 +6321,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/serialization_traits.h \ include/grpcpp/impl/codegen/server_callback.h \ include/grpcpp/impl/codegen/server_context.h \ + include/grpcpp/impl/codegen/server_context_impl.h \ include/grpcpp/impl/codegen/server_interceptor.h \ include/grpcpp/impl/codegen/server_interface.h \ include/grpcpp/impl/codegen/service_type.h \ @@ -6728,6 +6730,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/serialization_traits.h \ include/grpcpp/impl/codegen/server_callback.h \ include/grpcpp/impl/codegen/server_context.h \ + include/grpcpp/impl/codegen/server_context_impl.h \ include/grpcpp/impl/codegen/server_interceptor.h \ include/grpcpp/impl/codegen/server_interface.h \ include/grpcpp/impl/codegen/service_type.h \ @@ -6899,6 +6902,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/serialization_traits.h \ include/grpcpp/impl/codegen/server_callback.h \ include/grpcpp/impl/codegen/server_context.h \ + include/grpcpp/impl/codegen/server_context_impl.h \ include/grpcpp/impl/codegen/server_interceptor.h \ include/grpcpp/impl/codegen/server_interface.h \ include/grpcpp/impl/codegen/service_type.h \ @@ -7265,6 +7269,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/serialization_traits.h \ include/grpcpp/impl/codegen/server_callback.h \ include/grpcpp/impl/codegen/server_context.h \ + include/grpcpp/impl/codegen/server_context_impl.h \ include/grpcpp/impl/codegen/server_interceptor.h \ include/grpcpp/impl/codegen/server_interface.h \ include/grpcpp/impl/codegen/service_type.h \ diff --git a/build.yaml b/build.yaml index 580cda1baab..bb53d6693fe 100644 --- a/build.yaml +++ b/build.yaml @@ -1282,6 +1282,7 @@ filegroups: - include/grpcpp/impl/codegen/serialization_traits.h - include/grpcpp/impl/codegen/server_callback.h - include/grpcpp/impl/codegen/server_context.h + - include/grpcpp/impl/codegen/server_context_impl.h - include/grpcpp/impl/codegen/server_interceptor.h - include/grpcpp/impl/codegen/server_interface.h - include/grpcpp/impl/codegen/service_type.h diff --git a/gRPC-C++.podspec b/gRPC-C++.podspec index 861f757bb7f..18ed5f89102 100644 --- a/gRPC-C++.podspec +++ b/gRPC-C++.podspec @@ -183,6 +183,7 @@ Pod::Spec.new do |s| 'include/grpcpp/impl/codegen/serialization_traits.h', 'include/grpcpp/impl/codegen/server_callback.h', 'include/grpcpp/impl/codegen/server_context.h', + 'include/grpcpp/impl/codegen/server_context_impl.h', 'include/grpcpp/impl/codegen/server_interceptor.h', 'include/grpcpp/impl/codegen/server_interface.h', 'include/grpcpp/impl/codegen/service_type.h', diff --git a/include/grpcpp/impl/codegen/client_context_impl.h b/include/grpcpp/impl/codegen/client_context_impl.h index e730450a5c9..8491215cf0e 100644 --- a/include/grpcpp/impl/codegen/client_context_impl.h +++ b/include/grpcpp/impl/codegen/client_context_impl.h @@ -92,7 +92,6 @@ template class ClientAsyncReaderWriter; template class ClientAsyncResponseReader; -class ServerContext; namespace testing { class InteropClientContextInspector; @@ -103,6 +102,7 @@ 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 @@ -191,7 +191,7 @@ class ClientContext { /// \return A newly constructed \a ClientContext instance based on \a /// server_context, with traits propagated (copied) according to \a options. static std::unique_ptr FromServerContext( - const grpc::ServerContext& server_context, + const grpc_impl::ServerContext& server_context, PropagationOptions options = PropagationOptions()); /// Add the (\a meta_key, \a meta_value) pair to the metadata associated with diff --git a/include/grpcpp/impl/codegen/completion_queue_impl.h b/include/grpcpp/impl/codegen/completion_queue_impl.h index d78c410704a..0a6b0b0977b 100644 --- a/include/grpcpp/impl/codegen/completion_queue_impl.h +++ b/include/grpcpp/impl/codegen/completion_queue_impl.h @@ -46,6 +46,7 @@ namespace grpc_impl { class Channel; class Server; class ServerBuilder; +class ServerContext; } // namespace grpc_impl namespace grpc { @@ -65,7 +66,6 @@ class ServerReaderWriterBody; } // namespace internal class ChannelInterface; -class ServerContext; class ServerInterface; namespace internal { @@ -277,7 +277,7 @@ class CompletionQueue : private ::grpc::GrpcLibraryCodegen { template <::grpc::StatusCode code> friend class ::grpc::internal::ErrorMethodHandler; friend class ::grpc_impl::Server; - friend class ::grpc::ServerContext; + friend class ::grpc_impl::ServerContext; friend class ::grpc::ServerInterface; template friend class ::grpc::internal::BlockingUnaryCallImpl; diff --git a/include/grpcpp/impl/codegen/rpc_service_method.h b/include/grpcpp/impl/codegen/rpc_service_method.h index 21fb2ac2130..0b16feed820 100644 --- a/include/grpcpp/impl/codegen/rpc_service_method.h +++ b/include/grpcpp/impl/codegen/rpc_service_method.h @@ -31,9 +31,11 @@ #include #include -namespace grpc { +namespace grpc_impl { class ServerContext; +} +namespace grpc { namespace internal { /// Base class for running an RPC handler. class MethodHandler { @@ -50,7 +52,7 @@ class MethodHandler { /// \param requester : used only by the callback API. It is a function /// called by the RPC Controller to request another RPC (and also /// to set up the state required to make that request possible) - HandlerParameter(Call* c, ServerContext* context, void* req, + HandlerParameter(Call* c, ::grpc_impl::ServerContext* context, void* req, Status req_status, void* handler_data, std::function requester) : call(c), @@ -61,7 +63,7 @@ class MethodHandler { call_requester(std::move(requester)) {} ~HandlerParameter() {} Call* call; - ServerContext* server_context; + ::grpc_impl::ServerContext* server_context; void* request; Status status; void* internal_data; diff --git a/include/grpcpp/impl/codegen/server_callback.h b/include/grpcpp/impl/codegen/server_callback.h index 9254518b605..da08ec963e4 100644 --- a/include/grpcpp/impl/codegen/server_callback.h +++ b/include/grpcpp/impl/codegen/server_callback.h @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include @@ -53,7 +53,7 @@ class ServerReactor { virtual void OnCancel() = 0; private: - friend class ::grpc::ServerContext; + friend class ::grpc_impl::ServerContext; template friend class CallbackClientStreamingHandler; template @@ -313,7 +313,7 @@ class ServerBidiReactor : public internal::ServerReactor { /// is a result of the client calling StartCall(). /// /// \param[in] context The context object now associated with this RPC - virtual void OnStarted(ServerContext* context) {} + virtual void OnStarted(::grpc_impl::ServerContext* context) {} /// Notifies the application that an explicit StartSendInitialMetadata /// operation completed. Not used when the sending of initial metadata @@ -372,7 +372,7 @@ class ServerReadReactor : public internal::ServerReactor { /// /// \param[in] context The context object now associated with this RPC /// \param[in] resp The response object to be used by this RPC - virtual void OnStarted(ServerContext* context, Response* resp) {} + virtual void OnStarted(::grpc_impl::ServerContext* context, Response* resp) {} /// The following notifications are exactly like ServerBidiReactor. virtual void OnSendInitialMetadataDone(bool ok) {} @@ -413,7 +413,8 @@ class ServerWriteReactor : public internal::ServerReactor { /// /// \param[in] context The context object now associated with this RPC /// \param[in] req The request object sent by the client - virtual void OnStarted(ServerContext* context, const Request* req) {} + virtual void OnStarted(::grpc_impl::ServerContext* context, + const Request* req) {} /// The following notifications are exactly like ServerBidiReactor. virtual void OnSendInitialMetadataDone(bool ok) {} @@ -437,7 +438,7 @@ class UnimplementedReadReactor : public experimental::ServerReadReactor { public: void OnDone() override { delete this; } - void OnStarted(ServerContext*, Response*) override { + void OnStarted(::grpc_impl::ServerContext*, Response*) override { this->Finish(Status(StatusCode::UNIMPLEMENTED, "")); } }; @@ -447,7 +448,7 @@ class UnimplementedWriteReactor : public experimental::ServerWriteReactor { public: void OnDone() override { delete this; } - void OnStarted(ServerContext*, const Request*) override { + void OnStarted(::grpc_impl::ServerContext*, const Request*) override { this->Finish(Status(StatusCode::UNIMPLEMENTED, "")); } }; @@ -457,7 +458,7 @@ class UnimplementedBidiReactor : public experimental::ServerBidiReactor { public: void OnDone() override { delete this; } - void OnStarted(ServerContext*) override { + void OnStarted(::grpc_impl::ServerContext*) override { this->Finish(Status(StatusCode::UNIMPLEMENTED, "")); } }; @@ -466,7 +467,8 @@ template class CallbackUnaryHandler : public MethodHandler { public: CallbackUnaryHandler( - std::function func) : func_(func) {} @@ -525,8 +527,8 @@ class CallbackUnaryHandler : public MethodHandler { } private: - std::function + std::function func_; experimental::MessageAllocator* allocator_ = nullptr; @@ -597,7 +599,7 @@ class CallbackUnaryHandler : public MethodHandler { friend class CallbackUnaryHandler; ServerCallbackRpcControllerImpl( - ServerContext* ctx, Call* call, + ::grpc_impl::ServerContext* ctx, Call* call, experimental::MessageHolder* allocator_state, std::function call_requester) : ctx_(ctx), @@ -628,7 +630,7 @@ class CallbackUnaryHandler : public MethodHandler { finish_ops_; CallbackWithSuccessTag finish_tag_; - ServerContext* ctx_; + ::grpc_impl::ServerContext* ctx_; Call call_; experimental::MessageHolder* const allocator_state_; @@ -732,7 +734,8 @@ class CallbackClientStreamingHandler : public MethodHandler { friend class CallbackClientStreamingHandler; ServerCallbackReaderImpl( - ServerContext* ctx, Call* call, std::function call_requester, + ::grpc_impl::ServerContext* ctx, Call* call, + std::function call_requester, experimental::ServerReadReactor* reactor) : ctx_(ctx), call_(*call), @@ -772,7 +775,7 @@ class CallbackClientStreamingHandler : public MethodHandler { CallOpSet> read_ops_; CallbackWithSuccessTag read_tag_; - ServerContext* ctx_; + ::grpc_impl::ServerContext* ctx_; Call call_; ResponseType resp_; std::function call_requester_; @@ -909,7 +912,7 @@ class CallbackServerStreamingHandler : public MethodHandler { friend class CallbackServerStreamingHandler; ServerCallbackWriterImpl( - ServerContext* ctx, Call* call, const RequestType* req, + ::grpc_impl::ServerContext* ctx, Call* call, const RequestType* req, std::function call_requester, experimental::ServerWriteReactor* reactor) : ctx_(ctx), @@ -950,7 +953,7 @@ class CallbackServerStreamingHandler : public MethodHandler { CallOpSet write_ops_; CallbackWithSuccessTag write_tag_; - ServerContext* ctx_; + ::grpc_impl::ServerContext* ctx_; Call call_; const RequestType* req_; std::function call_requester_; @@ -1078,7 +1081,8 @@ class CallbackBidiHandler : public MethodHandler { friend class CallbackBidiHandler; ServerCallbackReaderWriterImpl( - ServerContext* ctx, Call* call, std::function call_requester, + ::grpc_impl::ServerContext* ctx, Call* call, + std::function call_requester, experimental::ServerBidiReactor* reactor) : ctx_(ctx), call_(*call), @@ -1124,7 +1128,7 @@ class CallbackBidiHandler : public MethodHandler { CallOpSet> read_ops_; CallbackWithSuccessTag read_tag_; - ServerContext* ctx_; + ::grpc_impl::ServerContext* ctx_; Call call_; std::function call_requester_; experimental::ServerBidiReactor* reactor_; diff --git a/include/grpcpp/impl/codegen/server_context.h b/include/grpcpp/impl/codegen/server_context.h index 7f3280b3d4c..7ae12755a1c 100644 --- a/include/grpcpp/impl/codegen/server_context.h +++ b/include/grpcpp/impl/codegen/server_context.h @@ -19,361 +19,10 @@ #ifndef GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_H #define GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_H -#include -#include -#include +#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -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 ServerAsyncReader; -template -class ServerAsyncWriter; -template -class ServerAsyncResponseWriter; -template -class ServerAsyncReaderWriter; -template -class ServerReader; -template -class ServerWriter; - -namespace internal { -template -class ServerReaderWriterBody; -template -class RpcMethodHandler; -template -class ClientStreamingHandler; -template -class ServerStreamingHandler; -template -class BidiStreamingHandler; -template -class CallbackUnaryHandler; -template -class CallbackClientStreamingHandler; -template -class CallbackServerStreamingHandler; -template -class CallbackBidiHandler; -template -class TemplatedBidiStreamingHandler; -template -class ErrorMethodHandler; -class Call; -class ServerReactor; -} // namespace internal - -class ServerInterface; -namespace testing { -class InteropServerContextInspector; -class ServerContextTestSpouse; -} // namespace testing - -/// 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 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& 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& cost_data); - - /// Return the authentication context for this server call. - /// - /// \see grpc::AuthContext. - std::shared_ptr auth_context() const { - if (auth_context_.get() == nullptr) { - auth_context_ = 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 - friend class ::grpc::ServerAsyncReader; - template - friend class ::grpc::ServerAsyncWriter; - template - friend class ::grpc::ServerAsyncResponseWriter; - template - friend class ::grpc::ServerAsyncReaderWriter; - template - friend class ::grpc::ServerReader; - template - friend class ::grpc::ServerWriter; - template - friend class ::grpc::internal::ServerReaderWriterBody; - template - friend class ::grpc::internal::RpcMethodHandler; - template - friend class ::grpc::internal::ClientStreamingHandler; - template - friend class ::grpc::internal::ServerStreamingHandler; - template - friend class ::grpc::internal::TemplatedBidiStreamingHandler; - template - friend class ::grpc::internal::CallbackUnaryHandler; - template - friend class ::grpc::internal::CallbackClientStreamingHandler; - template - friend class ::grpc::internal::CallbackServerStreamingHandler; - template - friend class ::grpc::internal::CallbackBidiHandler; - template - friend class internal::ErrorMethodHandler; - friend class ::grpc_impl::ClientContext; - friend class ::grpc::GenericServerContext; - - /// Prevent copying. - ServerContext(const ServerContext&); - ServerContext& operator=(const ServerContext&); - - class CompletionOp; - - void BeginCompletionOp(internal::Call* call, - std::function callback, - internal::ServerReactor* reactor); - /// Return the tag queued by BeginCompletionOp() - 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 callback); - void ClearCancelCallback(); - - experimental::ServerRpcInfo* set_server_rpc_info( - const char* method, internal::RpcMethod::RpcType type, - const std::vector< - std::unique_ptr>& - creators) { - if (creators.size() != 0) { - rpc_info_ = new 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_; - internal::CallbackWithSuccessTag completion_tag_; - - gpr_timespec deadline_; - grpc_call* call_; - ::grpc_impl::CompletionQueue* cq_; - bool sent_initial_metadata_; - mutable std::shared_ptr auth_context_; - mutable internal::MetadataMap client_metadata_; - std::multimap initial_metadata_; - std::multimap trailing_metadata_; - - bool compression_level_set_; - grpc_compression_level compression_level_; - grpc_compression_algorithm compression_algorithm_; - - internal::CallOpSet - pending_ops_; - bool has_pending_ops_; - - experimental::ServerRpcInfo* rpc_info_; -}; - +typedef ::grpc_impl::ServerContext ServerContext; } // namespace grpc #endif // GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_H diff --git a/include/grpcpp/impl/codegen/server_context_impl.h b/include/grpcpp/impl/codegen/server_context_impl.h new file mode 100644 index 00000000000..9497735eacc --- /dev/null +++ b/include/grpcpp/impl/codegen/server_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 +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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 ServerAsyncReader; +template +class ServerAsyncWriter; +template +class ServerAsyncResponseWriter; +template +class ServerAsyncReaderWriter; +template +class ServerReader; +template +class ServerWriter; + +namespace internal { +template +class ServerReaderWriterBody; +template +class RpcMethodHandler; +template +class ClientStreamingHandler; +template +class ServerStreamingHandler; +template +class BidiStreamingHandler; +template +class CallbackUnaryHandler; +template +class CallbackClientStreamingHandler; +template +class CallbackServerStreamingHandler; +template +class CallbackBidiHandler; +template +class TemplatedBidiStreamingHandler; +template +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& 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& cost_data); + + /// Return the authentication context for this server call. + /// + /// \see grpc::AuthContext. + std::shared_ptr 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 + friend class ::grpc::ServerAsyncReader; + template + friend class ::grpc::ServerAsyncWriter; + template + friend class ::grpc::ServerAsyncResponseWriter; + template + friend class ::grpc::ServerAsyncReaderWriter; + template + friend class ::grpc::ServerReader; + template + friend class ::grpc::ServerWriter; + template + friend class ::grpc::internal::ServerReaderWriterBody; + template + friend class ::grpc::internal::RpcMethodHandler; + template + friend class ::grpc::internal::ClientStreamingHandler; + template + friend class ::grpc::internal::ServerStreamingHandler; + template + friend class ::grpc::internal::TemplatedBidiStreamingHandler; + template + friend class ::grpc::internal::CallbackUnaryHandler; + template + friend class ::grpc::internal::CallbackClientStreamingHandler; + template + friend class ::grpc::internal::CallbackServerStreamingHandler; + template + 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 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 callback); + void ClearCancelCallback(); + + ::grpc::experimental::ServerRpcInfo* set_server_rpc_info( + const char* method, ::grpc::internal::RpcMethod::RpcType type, + const std::vector>& 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 auth_context_; + mutable ::grpc::internal::MetadataMap client_metadata_; + std::multimap initial_metadata_; + std::multimap 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 diff --git a/include/grpcpp/impl/codegen/server_interceptor.h b/include/grpcpp/impl/codegen/server_interceptor.h index 8875a28bf32..28fdc27ec68 100644 --- a/include/grpcpp/impl/codegen/server_interceptor.h +++ b/include/grpcpp/impl/codegen/server_interceptor.h @@ -26,9 +26,11 @@ #include #include -namespace grpc { - +namespace grpc_impl { class ServerContext; +} + +namespace grpc { namespace internal { class InterceptorBatchMethodsImpl; @@ -78,7 +80,7 @@ class ServerRpcInfo { /// Return a pointer to the underlying ServerContext structure associated /// with the RPC to support features that apply to it - grpc::ServerContext* server_context() { return ctx_; } + grpc_impl::ServerContext* server_context() { return ctx_; } private: static_assert(Type::UNARY == @@ -94,7 +96,7 @@ class ServerRpcInfo { static_cast(internal::RpcMethod::BIDI_STREAMING), "violated expectation about Type enum"); - ServerRpcInfo(grpc::ServerContext* ctx, const char* method, + ServerRpcInfo(grpc_impl::ServerContext* ctx, const char* method, internal::RpcMethod::RpcType type) : ctx_(ctx), method_(method), type_(static_cast(type)) { ref_.store(1); @@ -127,14 +129,14 @@ class ServerRpcInfo { } } - grpc::ServerContext* ctx_ = nullptr; + grpc_impl::ServerContext* ctx_ = nullptr; const char* method_ = nullptr; const Type type_; std::atomic_int ref_; std::vector> interceptors_; friend class internal::InterceptorBatchMethodsImpl; - friend class grpc::ServerContext; + friend class grpc_impl::ServerContext; }; } // namespace experimental diff --git a/include/grpcpp/impl/codegen/server_interface.h b/include/grpcpp/impl/codegen/server_interface.h index d8d49344965..9600e5f053d 100644 --- a/include/grpcpp/impl/codegen/server_interface.h +++ b/include/grpcpp/impl/codegen/server_interface.h @@ -34,12 +34,12 @@ class Channel; class CompletionQueue; class ServerCompletionQueue; class ServerCredentials; +class ServerContext; } // namespace grpc_impl namespace grpc { class AsyncGenericService; class GenericServerContext; -class ServerContext; class Service; extern CoreCodegenInterface* g_core_codegen_interface; @@ -176,7 +176,8 @@ class ServerInterface : public internal::CallHook { class BaseAsyncRequest : public internal::CompletionQueueTag { public: - BaseAsyncRequest(ServerInterface* server, ServerContext* context, + BaseAsyncRequest(ServerInterface* server, + ::grpc_impl::ServerContext* context, internal::ServerAsyncStreamingInterface* stream, ::grpc_impl::CompletionQueue* call_cq, ::grpc_impl::ServerCompletionQueue* notification_cq, @@ -190,7 +191,7 @@ class ServerInterface : public internal::CallHook { protected: ServerInterface* const server_; - ServerContext* const context_; + ::grpc_impl::ServerContext* const context_; internal::ServerAsyncStreamingInterface* const stream_; ::grpc_impl::CompletionQueue* const call_cq_; ::grpc_impl::ServerCompletionQueue* const notification_cq_; @@ -205,7 +206,8 @@ class ServerInterface : public internal::CallHook { /// RegisteredAsyncRequest is not part of the C++ API class RegisteredAsyncRequest : public BaseAsyncRequest { public: - RegisteredAsyncRequest(ServerInterface* server, ServerContext* context, + RegisteredAsyncRequest(ServerInterface* server, + ::grpc_impl::ServerContext* context, internal::ServerAsyncStreamingInterface* stream, ::grpc_impl::CompletionQueue* call_cq, ::grpc_impl::ServerCompletionQueue* notification_cq, @@ -234,7 +236,8 @@ class ServerInterface : public internal::CallHook { class NoPayloadAsyncRequest final : public RegisteredAsyncRequest { public: NoPayloadAsyncRequest(internal::RpcServiceMethod* registered_method, - ServerInterface* server, ServerContext* context, + ServerInterface* server, + ::grpc_impl::ServerContext* context, internal::ServerAsyncStreamingInterface* stream, ::grpc_impl::CompletionQueue* call_cq, ::grpc_impl::ServerCompletionQueue* notification_cq, @@ -252,7 +255,8 @@ class ServerInterface : public internal::CallHook { class PayloadAsyncRequest final : public RegisteredAsyncRequest { public: PayloadAsyncRequest(internal::RpcServiceMethod* registered_method, - ServerInterface* server, ServerContext* context, + ServerInterface* server, + ::grpc_impl::ServerContext* context, internal::ServerAsyncStreamingInterface* stream, ::grpc_impl::CompletionQueue* call_cq, ::grpc_impl::ServerCompletionQueue* notification_cq, @@ -309,7 +313,7 @@ class ServerInterface : public internal::CallHook { private: internal::RpcServiceMethod* const registered_method_; ServerInterface* const server_; - ServerContext* const context_; + ::grpc_impl::ServerContext* const context_; internal::ServerAsyncStreamingInterface* const stream_; ::grpc_impl::CompletionQueue* const call_cq_; @@ -335,7 +339,7 @@ class ServerInterface : public internal::CallHook { template void RequestAsyncCall(internal::RpcServiceMethod* method, - ServerContext* context, + ::grpc_impl::ServerContext* context, internal::ServerAsyncStreamingInterface* stream, ::grpc_impl::CompletionQueue* call_cq, ::grpc_impl::ServerCompletionQueue* notification_cq, @@ -346,7 +350,7 @@ class ServerInterface : public internal::CallHook { } void RequestAsyncCall(internal::RpcServiceMethod* method, - ServerContext* context, + ::grpc_impl::ServerContext* context, internal::ServerAsyncStreamingInterface* stream, ::grpc_impl::CompletionQueue* call_cq, ::grpc_impl::ServerCompletionQueue* notification_cq, diff --git a/include/grpcpp/impl/codegen/service_type.h b/include/grpcpp/impl/codegen/service_type.h index f1d1272dc20..4109ee1b504 100644 --- a/include/grpcpp/impl/codegen/service_type.h +++ b/include/grpcpp/impl/codegen/service_type.h @@ -30,11 +30,11 @@ namespace grpc_impl { class Server; class CompletionQueue; +class ServerContext; } // namespace grpc_impl namespace grpc { class ServerInterface; -class ServerContext; namespace internal { class Call; @@ -146,7 +146,8 @@ class Service { experimental_type experimental() { return experimental_type(this); } template - void RequestAsyncUnary(int index, ServerContext* context, Message* request, + void RequestAsyncUnary(int index, ::grpc_impl::ServerContext* context, + Message* request, internal::ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, ServerCompletionQueue* notification_cq, void* tag) { @@ -158,7 +159,7 @@ class Service { notification_cq, tag, request); } void RequestAsyncClientStreaming( - int index, ServerContext* context, + int index, ::grpc_impl::ServerContext* context, internal::ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, ServerCompletionQueue* notification_cq, void* tag) { size_t idx = static_cast(index); @@ -167,7 +168,7 @@ class Service { } template void RequestAsyncServerStreaming( - int index, ServerContext* context, Message* request, + int index, ::grpc_impl::ServerContext* context, Message* request, internal::ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, ServerCompletionQueue* notification_cq, void* tag) { size_t idx = static_cast(index); @@ -175,7 +176,7 @@ class Service { notification_cq, tag, request); } void RequestAsyncBidiStreaming( - int index, ServerContext* context, + int index, ::grpc_impl::ServerContext* context, internal::ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, ServerCompletionQueue* notification_cq, void* tag) { size_t idx = static_cast(index); diff --git a/include/grpcpp/opencensus.h b/include/grpcpp/opencensus.h index 14c6add1d9e..39bac6bc28c 100644 --- a/include/grpcpp/opencensus.h +++ b/include/grpcpp/opencensus.h @@ -30,7 +30,7 @@ static inline void RegisterOpenCensusViewsForExport() { ::grpc_impl::RegisterOpenCensusViewsForExport(); } static inline ::opencensus::trace::Span GetSpanFromServerContext( - ServerContext* context) { + ::grpc_impl::ServerContext* context) { return ::grpc_impl::GetSpanFromServerContext(context); } diff --git a/include/grpcpp/opencensus_impl.h b/include/grpcpp/opencensus_impl.h index 631d2b861fd..dbcb7c9caeb 100644 --- a/include/grpcpp/opencensus_impl.h +++ b/include/grpcpp/opencensus_impl.h @@ -21,11 +21,8 @@ #include "opencensus/trace/span.h" -namespace grpc { - -class ServerContext; -} namespace grpc_impl { +class ServerContext; // These symbols in this file will not be included in the binary unless // grpc_opencensus_plugin build target was added as a dependency. At the moment // it is only setup to be built with Bazel. @@ -43,8 +40,7 @@ void RegisterOpenCensusPlugin(); void RegisterOpenCensusViewsForExport(); // Returns the tracing Span for the current RPC. -::opencensus::trace::Span GetSpanFromServerContext( - grpc::ServerContext* context); +::opencensus::trace::Span GetSpanFromServerContext(ServerContext* context); } // namespace grpc_impl diff --git a/include/grpcpp/server_impl.h b/include/grpcpp/server_impl.h index 59a780276e0..90019e25032 100644 --- a/include/grpcpp/server_impl.h +++ b/include/grpcpp/server_impl.h @@ -44,7 +44,6 @@ struct grpc_server; namespace grpc { class AsyncGenericService; -class ServerContext; namespace internal { class ExternalConnectionAcceptorImpl; @@ -54,6 +53,7 @@ class ExternalConnectionAcceptorImpl; namespace grpc_impl { class HealthCheckServiceInterface; +class ServerContext; class ServerInitializer; /// Represents a gRPC server. @@ -82,9 +82,9 @@ class Server : public grpc::ServerInterface, private grpc::GrpcLibraryCodegen { /// Called before server is created. virtual void UpdateArguments(grpc::ChannelArguments* args) {} /// Called before application callback for each synchronous server request - virtual void PreSynchronousRequest(grpc::ServerContext* context) = 0; + virtual void PreSynchronousRequest(grpc_impl::ServerContext* context) = 0; /// Called after application callback for each synchronous server request - virtual void PostSynchronousRequest(grpc::ServerContext* context) = 0; + virtual void PostSynchronousRequest(grpc_impl::ServerContext* context) = 0; /// Called before server is started. virtual void PreServerStart(Server* server) {} /// Called after a server port is added. diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc index bcc849035c6..23e3bd50eff 100644 --- a/src/compiler/cpp_generator.cc +++ b/src/compiler/cpp_generator.cc @@ -157,13 +157,13 @@ grpc::string GetHeaderIncludes(grpc_generator::File* file, printer->Print(vars, "namespace grpc_impl {\n"); printer->Print(vars, "class CompletionQueue;\n"); printer->Print(vars, "class ServerCompletionQueue;\n"); + printer->Print(vars, "class ServerContext;\n"); printer->Print(vars, "} // namespace grpc_impl\n\n"); printer->Print(vars, "namespace grpc {\n"); printer->Print(vars, "namespace experimental {\n"); printer->Print(vars, "template \n"); printer->Print(vars, "class MessageAllocator;\n"); printer->Print(vars, "} // namespace experimental\n"); - printer->Print(vars, "class ServerContext;\n"); printer->Print(vars, "} // namespace grpc\n\n"); vars["message_header_ext"] = params.message_header_extension.empty() diff --git a/src/cpp/ext/filters/census/grpc_plugin.h b/src/cpp/ext/filters/census/grpc_plugin.h index 993faae0a94..13176759e37 100644 --- a/src/cpp/ext/filters/census/grpc_plugin.h +++ b/src/cpp/ext/filters/census/grpc_plugin.h @@ -25,8 +25,11 @@ #include "include/grpcpp/opencensus.h" #include "opencensus/stats/stats.h" -namespace grpc { +namespace grpc_impl { class ServerContext; +} + +namespace grpc { // The tag keys set when recording RPC stats. ::opencensus::stats::TagKey ClientMethodTagKey(); diff --git a/src/cpp/server/server_context.cc b/src/cpp/server/server_context.cc index eced89d1a79..ba834237ae9 100644 --- a/src/cpp/server/server_context.cc +++ b/src/cpp/server/server_context.cc @@ -16,7 +16,7 @@ * */ -#include +#include #include #include @@ -28,23 +28,25 @@ #include #include #include -#include #include +#include #include #include "src/core/lib/gprpp/ref_counted.h" #include "src/core/lib/gprpp/sync.h" #include "src/core/lib/surface/call.h" -namespace grpc { +namespace grpc_impl { // CompletionOp -class ServerContext::CompletionOp final : public internal::CallOpSetInterface { +class ServerContext::CompletionOp final + : public ::grpc::internal::CallOpSetInterface { public: // initial refs: one in the server context, one in the cq // must ref the call before calling constructor and after deleting this - CompletionOp(internal::Call* call, internal::ServerReactor* reactor) + CompletionOp(::grpc::internal::Call* call, + ::grpc::internal::ServerReactor* reactor) : call_(*call), reactor_(reactor), has_tag_(false), @@ -67,7 +69,7 @@ class ServerContext::CompletionOp final : public internal::CallOpSetInterface { } } - void FillOps(internal::Call* call) override; + void FillOps(::grpc::internal::Call* call) override; // This should always be arena allocated in the call, so override delete. // But this class is not trivially destructible, so must actually call delete @@ -149,8 +151,8 @@ class ServerContext::CompletionOp final : public internal::CallOpSetInterface { return finalized_ ? (cancelled_ != 0) : false; } - internal::Call call_; - internal::ServerReactor* const reactor_; + ::grpc::internal::Call call_; + ::grpc::internal::ServerReactor* const reactor_; bool has_tag_; void* tag_; void* core_cq_tag_; @@ -160,7 +162,7 @@ class ServerContext::CompletionOp final : public internal::CallOpSetInterface { int cancelled_; // This is an int (not bool) because it is passed to core std::function cancel_callback_; bool done_intercepting_; - internal::InterceptorBatchMethodsImpl interceptor_methods_; + ::grpc::internal::InterceptorBatchMethodsImpl interceptor_methods_; }; void ServerContext::CompletionOp::Unref() { @@ -171,7 +173,7 @@ void ServerContext::CompletionOp::Unref() { } } -void ServerContext::CompletionOp::FillOps(internal::Call* call) { +void ServerContext::CompletionOp::FillOps(::grpc::internal::Call* call) { grpc_op ops; ops.op = GRPC_OP_RECV_CLOSE_ON_SERVER; ops.data.recv_close_on_server.cancelled = &cancelled_; @@ -227,7 +229,7 @@ bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) { } /* Add interception point and run through interceptors */ interceptor_methods_.AddInterceptionHookPoint( - experimental::InterceptionHookPoints::POST_RECV_CLOSE); + ::grpc::experimental::InterceptionHookPoints::POST_RECV_CLOSE); if (interceptor_methods_.RunInterceptors()) { /* No interceptors were run */ if (has_tag_) { @@ -292,9 +294,9 @@ void ServerContext::Clear() { } } -void ServerContext::BeginCompletionOp(internal::Call* call, - std::function callback, - internal::ServerReactor* reactor) { +void ServerContext::BeginCompletionOp( + ::grpc::internal::Call* call, std::function callback, + ::grpc::internal::ServerReactor* reactor) { GPR_ASSERT(!completion_op_); if (rpc_info_) { rpc_info_->Ref(); @@ -313,8 +315,8 @@ void ServerContext::BeginCompletionOp(internal::Call* call, call->PerformOps(completion_op_); } -internal::CompletionQueueTag* ServerContext::GetCompletionOpTag() { - return static_cast(completion_op_); +::grpc::internal::CompletionQueueTag* ServerContext::GetCompletionOpTag() { + return static_cast<::grpc::internal::CompletionQueueTag*>(completion_op_); } void ServerContext::AddInitialMetadata(const grpc::string& key, @@ -328,7 +330,7 @@ void ServerContext::AddTrailingMetadata(const grpc::string& key, } void ServerContext::TryCancel() const { - internal::CancelInterceptorBatchMethods cancel_methods; + ::grpc::internal::CancelInterceptorBatchMethods cancel_methods; if (rpc_info_) { for (size_t i = 0; i < rpc_info_->interceptors_.size(); i++) { rpc_info_->RunInterceptor(&cancel_methods, i); @@ -398,4 +400,4 @@ void ServerContext::SetLoadReportingCosts( } } -} // namespace grpc +} // namespace grpc_impl diff --git a/test/cpp/codegen/compiler_test_golden b/test/cpp/codegen/compiler_test_golden index 761c3ac6106..64ab123123b 100644 --- a/test/cpp/codegen/compiler_test_golden +++ b/test/cpp/codegen/compiler_test_golden @@ -43,6 +43,7 @@ namespace grpc_impl { class CompletionQueue; class ServerCompletionQueue; +class ServerContext; } // namespace grpc_impl namespace grpc { @@ -50,7 +51,6 @@ namespace experimental { template class MessageAllocator; } // namespace experimental -class ServerContext; } // namespace grpc namespace grpc { diff --git a/tools/doxygen/Doxyfile.c++ b/tools/doxygen/Doxyfile.c++ index 3daf46f580c..466bb5cd5a3 100644 --- a/tools/doxygen/Doxyfile.c++ +++ b/tools/doxygen/Doxyfile.c++ @@ -983,6 +983,7 @@ include/grpcpp/impl/codegen/security/auth_context.h \ include/grpcpp/impl/codegen/serialization_traits.h \ include/grpcpp/impl/codegen/server_callback.h \ include/grpcpp/impl/codegen/server_context.h \ +include/grpcpp/impl/codegen/server_context_impl.h \ include/grpcpp/impl/codegen/server_interceptor.h \ include/grpcpp/impl/codegen/server_interface.h \ include/grpcpp/impl/codegen/service_type.h \ diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index 0bf0682fb7e..9b0f8f9fcd7 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -985,6 +985,7 @@ include/grpcpp/impl/codegen/security/auth_context.h \ include/grpcpp/impl/codegen/serialization_traits.h \ include/grpcpp/impl/codegen/server_callback.h \ include/grpcpp/impl/codegen/server_context.h \ +include/grpcpp/impl/codegen/server_context_impl.h \ include/grpcpp/impl/codegen/server_interceptor.h \ include/grpcpp/impl/codegen/server_interface.h \ include/grpcpp/impl/codegen/service_type.h \ diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json index a9a5a372128..0da1dc36ff9 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -10152,6 +10152,7 @@ "include/grpcpp/impl/codegen/serialization_traits.h", "include/grpcpp/impl/codegen/server_callback.h", "include/grpcpp/impl/codegen/server_context.h", + "include/grpcpp/impl/codegen/server_context_impl.h", "include/grpcpp/impl/codegen/server_interceptor.h", "include/grpcpp/impl/codegen/server_interface.h", "include/grpcpp/impl/codegen/service_type.h", @@ -10231,6 +10232,7 @@ "include/grpcpp/impl/codegen/serialization_traits.h", "include/grpcpp/impl/codegen/server_callback.h", "include/grpcpp/impl/codegen/server_context.h", + "include/grpcpp/impl/codegen/server_context_impl.h", "include/grpcpp/impl/codegen/server_interceptor.h", "include/grpcpp/impl/codegen/server_interface.h", "include/grpcpp/impl/codegen/service_type.h",