diff options
author | David Garcia Quintas <dgq@google.com> | 2016-01-27 18:41:26 -0800 |
---|---|---|
committer | David Garcia Quintas <dgq@google.com> | 2016-01-27 18:41:26 -0800 |
commit | e1300deb87b5fca2b4361a753d0bd4d19b078ea4 (patch) | |
tree | 62016bec7621d6f1650472c20d56fdbf713f077c /include/grpc++ | |
parent | 6a48405ed003a416bd574d3f480b20e3dee9e9df (diff) |
After GrpcLibrary refactoring. Compiles and passes. WIP still
Diffstat (limited to 'include/grpc++')
-rw-r--r-- | include/grpc++/alarm.h | 9 | ||||
-rw-r--r-- | include/grpc++/channel.h | 10 | ||||
-rw-r--r-- | include/grpc++/completion_queue.h | 10 | ||||
-rw-r--r-- | include/grpc++/impl/client_unary_call.h | 6 | ||||
-rw-r--r-- | include/grpc++/impl/codegen/grpc_library.h | 67 | ||||
-rw-r--r-- | include/grpc++/impl/grpc_library.h | 24 | ||||
-rw-r--r-- | include/grpc++/security/credentials.h | 15 | ||||
-rw-r--r-- | include/grpc++/server.h | 6 | ||||
-rw-r--r-- | include/grpc++/support/sync_stream.h | 62 |
9 files changed, 146 insertions, 63 deletions
diff --git a/include/grpc++/alarm.h b/include/grpc++/alarm.h index 957d11c500..381e8c7698 100644 --- a/include/grpc++/alarm.h +++ b/include/grpc++/alarm.h @@ -36,14 +36,15 @@ #ifndef GRPCXX_ALARM_H #define GRPCXX_ALARM_H -#include <grpc++/completion_queue.h> -#include <grpc++/impl/grpc_library.h> -#include <grpc++/support/time.h> +#include <grpc++/impl/codegen/grpc_library.h> +#include <grpc++/impl/codegen/time.h> namespace grpc { +class CompletionQueue; + /// A thin wrapper around \a grpc_alarm (see / \a / src/core/surface/alarm.h). -class Alarm : public GrpcLibrary { +class Alarm : private GrpcLibrary { public: /// Create a completion queue alarm instance associated to \a cq. /// diff --git a/include/grpc++/channel.h b/include/grpc++/channel.h index d6f55a8bf6..80547f7ab8 100644 --- a/include/grpc++/channel.h +++ b/include/grpc++/channel.h @@ -36,20 +36,20 @@ #include <memory> -#include <grpc/grpc.h> #include <grpc++/impl/call.h> #include <grpc++/impl/codegen/channel_interface.h> -#include <grpc++/impl/grpc_library.h> -#include <grpc++/support/config.h> +#include <grpc++/impl/codegen/config.h> +#include <grpc++/impl/codegen/grpc_library.h> +#include <grpc/grpc.h> struct grpc_channel; namespace grpc { /// Channels represent a connection to an endpoint. Created by \a CreateChannel. class Channel GRPC_FINAL : public ChannelInterface, - public GrpcLibrary, public CallHook, - public std::enable_shared_from_this<Channel> { + public std::enable_shared_from_this<Channel>, + private GrpcLibrary { public: ~Channel(); diff --git a/include/grpc++/completion_queue.h b/include/grpc++/completion_queue.h index 7ed8f59008..911d7919e1 100644 --- a/include/grpc++/completion_queue.h +++ b/include/grpc++/completion_queue.h @@ -36,9 +36,9 @@ #ifndef GRPCXX_COMPLETION_QUEUE_H #define GRPCXX_COMPLETION_QUEUE_H -#include <grpc++/impl/grpc_library.h> -#include <grpc++/support/status.h> -#include <grpc++/support/time.h> +#include <grpc++/impl/codegen/grpc_library.h> +#include <grpc++/impl/codegen/status.h> +#include <grpc++/impl/codegen/time.h> struct grpc_completion_queue; @@ -78,7 +78,7 @@ class ServerContext; /// A thin wrapper around \a grpc_completion_queue (see / \a /// src/core/surface/completion_queue.h). -class CompletionQueue : public GrpcLibrary { +class CompletionQueue : private GrpcLibrary { public: /// Default constructor. Implicitly creates a \a grpc_completion_queue /// instance. @@ -90,7 +90,7 @@ class CompletionQueue : public GrpcLibrary { explicit CompletionQueue(grpc_completion_queue* take); /// Destructor. Destroys the owned wrapped completion queue / instance. - ~CompletionQueue() GRPC_OVERRIDE; + ~CompletionQueue(); /// Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT. enum NextStatus { diff --git a/include/grpc++/impl/client_unary_call.h b/include/grpc++/impl/client_unary_call.h index 6033fb7cc2..9017bf5e35 100644 --- a/include/grpc++/impl/client_unary_call.h +++ b/include/grpc++/impl/client_unary_call.h @@ -34,10 +34,10 @@ #ifndef GRPCXX_IMPL_CLIENT_UNARY_CALL_H #define GRPCXX_IMPL_CLIENT_UNARY_CALL_H -#include <grpc++/impl/call.h> +#include <grpc++/impl/codegen/call.h> #include <grpc++/impl/codegen/channel_interface.h> -#include <grpc++/support/config.h> -#include <grpc++/support/status.h> +#include <grpc++/impl/codegen/config.h> +#include <grpc++/impl/codegen/status.h> namespace grpc { diff --git a/include/grpc++/impl/codegen/grpc_library.h b/include/grpc++/impl/codegen/grpc_library.h new file mode 100644 index 0000000000..eb7152a2c6 --- /dev/null +++ b/include/grpc++/impl/codegen/grpc_library.h @@ -0,0 +1,67 @@ +/* + * + * Copyright 2016, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef GRPCXX_IMPL_CODEGEN_GRPC_LIBRARY_H +#define GRPCXX_IMPL_CODEGEN_GRPC_LIBRARY_H + +#include <grpc/impl/codegen/log.h> + +namespace grpc { + +class GrpcLibraryInterface { + public: + virtual void init() = 0; + virtual void shutdown() = 0; +}; + +extern GrpcLibraryInterface* g_glip; + +class GrpcLibrary { + public: + GrpcLibrary() { + GPR_ASSERT(g_glip && + "gRPC library not initialized. See " + "grpc::internal::GrpcLibraryInitializer."); + g_glip->init(); + } + virtual ~GrpcLibrary() { + GPR_ASSERT(g_glip && + "gRPC library not initialized. See " + "grpc::internal::GrpcLibraryInitializer."); + g_glip->shutdown(); + } +}; + +} // namespace grpc + +#endif // GRPCXX_IMPL_GRPC_LIBRARY_H diff --git a/include/grpc++/impl/grpc_library.h b/include/grpc++/impl/grpc_library.h index ce4211418d..041a88f2b6 100644 --- a/include/grpc++/impl/grpc_library.h +++ b/include/grpc++/impl/grpc_library.h @@ -34,16 +34,34 @@ #ifndef GRPCXX_IMPL_GRPC_LIBRARY_H #define GRPCXX_IMPL_GRPC_LIBRARY_H +#include <grpc++/impl/codegen/config.h> +#include <grpc++/impl/codegen/grpc_library.h> #include <grpc/grpc.h> namespace grpc { -class GrpcLibrary { +namespace internal { +class GrpcLibrary GRPC_FINAL : public GrpcLibraryInterface { public: - GrpcLibrary() { grpc_init(); } - virtual ~GrpcLibrary() { grpc_shutdown(); } + void init() GRPC_OVERRIDE { grpc_init(); } + + void shutdown() GRPC_OVERRIDE { grpc_shutdown(); } }; +static GrpcLibrary g_gli; + +class GrpcLibraryInitializer GRPC_FINAL { + public: + GrpcLibraryInitializer() { grpc::g_glip = &g_gli; } + + /// A no-op method to force the linker to reference this class, which will + /// take care of initializing and shutting down the gRPC runtime. + inline void summon() {} +}; + +extern GrpcLibraryInitializer g_gli_initializer; + +} // namespace internal } // namespace grpc #endif // GRPCXX_IMPL_GRPC_LIBRARY_H diff --git a/include/grpc++/security/credentials.h b/include/grpc++/security/credentials.h index 75945fd8e8..4d64b5e227 100644 --- a/include/grpc++/security/credentials.h +++ b/include/grpc++/security/credentials.h @@ -37,12 +37,13 @@ #include <map> #include <memory> -#include <grpc++/impl/grpc_library.h> +#include <grpc++/impl/codegen/grpc_library.h> #include <grpc++/security/auth_context.h> -#include <grpc++/support/config.h> #include <grpc++/support/status.h> #include <grpc++/support/string_ref.h> +struct grpc_call; + namespace grpc { class ChannelArguments; class Channel; @@ -56,9 +57,10 @@ class SecureCallCredentials; /// for all the calls on that channel. /// /// \see http://www.grpc.io/docs/guides/auth.html -class ChannelCredentials : public GrpcLibrary { +class ChannelCredentials : private GrpcLibrary { public: - ~ChannelCredentials() GRPC_OVERRIDE; + ChannelCredentials(); + ~ChannelCredentials(); protected: friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials( @@ -81,9 +83,10 @@ class ChannelCredentials : public GrpcLibrary { /// authenticate with a server for a given call on a channel. /// /// \see http://www.grpc.io/docs/guides/auth.html -class CallCredentials : public GrpcLibrary { +class CallCredentials { public: - ~CallCredentials() GRPC_OVERRIDE; + CallCredentials(); + ~CallCredentials(); /// Apply this instance's credentials to \a call. virtual bool ApplyToCall(grpc_call* call) = 0; diff --git a/include/grpc++/server.h b/include/grpc++/server.h index c6af748c51..e7943f9dae 100644 --- a/include/grpc++/server.h +++ b/include/grpc++/server.h @@ -39,10 +39,10 @@ #include <grpc++/completion_queue.h> #include <grpc++/impl/call.h> -#include <grpc++/impl/grpc_library.h> +#include <grpc++/impl/codegen/grpc_library.h> +#include <grpc++/impl/codegen/server_interface.h> #include <grpc++/impl/rpc_service_method.h> #include <grpc++/impl/sync.h> -#include <grpc++/impl/codegen/server_interface.h> #include <grpc++/security/server_credentials.h> #include <grpc++/support/channel_arguments.h> #include <grpc++/support/config.h> @@ -62,7 +62,7 @@ class ThreadPoolInterface; /// Models a gRPC server. /// /// Servers are configured and started via \a grpc::ServerBuilder. -class Server GRPC_FINAL : public ServerInterface, public GrpcLibrary { +class Server GRPC_FINAL : public ServerInterface, private GrpcLibrary { public: ~Server(); diff --git a/include/grpc++/support/sync_stream.h b/include/grpc++/support/sync_stream.h index 77066ada62..3557ba5156 100644 --- a/include/grpc++/support/sync_stream.h +++ b/include/grpc++/support/sync_stream.h @@ -34,18 +34,18 @@ #ifndef GRPCXX_SUPPORT_SYNC_STREAM_H #define GRPCXX_SUPPORT_SYNC_STREAM_H -#include <grpc/impl/codegen/log.h> +#include <grpc++/channel.h> +#include <grpc++/client_context.h> +#include <grpc++/completion_queue.h> +#include <grpc++/impl/call.h> #include <grpc++/impl/codegen/channel_interface.h> -#include <grpc++/impl/codegen/client_context.h> -#include <grpc++/impl/codegen/call.h> -#include <grpc++/impl/codegen/service_type.h> -#include <grpc++/impl/codegen/server_context.h> -#include <grpc++/impl/codegen/status.h> +#include <grpc++/impl/service_type.h> +#include <grpc++/server_context.h> +#include <grpc++/support/status.h> +#include <grpc/support/log.h> namespace grpc { -class CompletionQueue; - /// Common interface for all synchronous client side streaming. class ClientStreamingInterface { public: @@ -121,9 +121,7 @@ class ClientReader GRPC_FINAL : public ClientReaderInterface<R> { template <class W> ClientReader(ChannelInterface* channel, const RpcMethod& method, ClientContext* context, const W& request) - : context_(context), - cq_(new CompletionQueue), - call_(channel->CreateCall(method, context, cq_.get())) { + : context_(context), call_(channel->CreateCall(method, context, &cq_)) { CallOpSet<CallOpSendInitialMetadata, CallOpSendMessage, CallOpClientSendClose> ops; ops.SendInitialMetadata(context->send_initial_metadata_); @@ -131,7 +129,7 @@ class ClientReader GRPC_FINAL : public ClientReaderInterface<R> { GPR_ASSERT(ops.SendMessage(request).ok()); ops.ClientSendClose(); call_.PerformOps(&ops); - cq_->Pluck(&ops); + cq_.Pluck(&ops); } void WaitForInitialMetadata() GRPC_OVERRIDE { @@ -140,7 +138,7 @@ class ClientReader GRPC_FINAL : public ClientReaderInterface<R> { CallOpSet<CallOpRecvInitialMetadata> ops; ops.RecvInitialMetadata(context_); call_.PerformOps(&ops); - cq_->Pluck(&ops); /// status ignored + cq_.Pluck(&ops); /// status ignored } bool Read(R* msg) GRPC_OVERRIDE { @@ -150,7 +148,7 @@ class ClientReader GRPC_FINAL : public ClientReaderInterface<R> { } ops.RecvMessage(msg); call_.PerformOps(&ops); - return cq_->Pluck(&ops) && ops.got_message; + return cq_.Pluck(&ops) && ops.got_message; } Status Finish() GRPC_OVERRIDE { @@ -158,13 +156,13 @@ class ClientReader GRPC_FINAL : public ClientReaderInterface<R> { Status status; ops.ClientRecvStatus(context_, &status); call_.PerformOps(&ops); - GPR_ASSERT(cq_->Pluck(&ops)); + GPR_ASSERT(cq_.Pluck(&ops)); return status; } private: ClientContext* context_; - std::unique_ptr<CompletionQueue> cq_; + CompletionQueue cq_; Call call_; }; @@ -187,15 +185,13 @@ class ClientWriter : public ClientWriterInterface<W> { template <class R> ClientWriter(ChannelInterface* channel, const RpcMethod& method, ClientContext* context, R* response) - : context_(context), - cq_(new CompletionQueue), - call_(channel->CreateCall(method, context, cq_.get())) { + : context_(context), call_(channel->CreateCall(method, context, &cq_)) { finish_ops_.RecvMessage(response); CallOpSet<CallOpSendInitialMetadata> ops; ops.SendInitialMetadata(context->send_initial_metadata_); call_.PerformOps(&ops); - cq_->Pluck(&ops); + cq_.Pluck(&ops); } using WriterInterface<W>::Write; @@ -205,14 +201,14 @@ class ClientWriter : public ClientWriterInterface<W> { return false; } call_.PerformOps(&ops); - return cq_->Pluck(&ops); + return cq_.Pluck(&ops); } bool WritesDone() GRPC_OVERRIDE { CallOpSet<CallOpClientSendClose> ops; ops.ClientSendClose(); call_.PerformOps(&ops); - return cq_->Pluck(&ops); + return cq_.Pluck(&ops); } /// Read the final response and wait for the final status. @@ -220,14 +216,14 @@ class ClientWriter : public ClientWriterInterface<W> { Status status; finish_ops_.ClientRecvStatus(context_, &status); call_.PerformOps(&finish_ops_); - GPR_ASSERT(cq_->Pluck(&finish_ops_)); + GPR_ASSERT(cq_.Pluck(&finish_ops_)); return status; } private: ClientContext* context_; CallOpSet<CallOpGenericRecvMessage, CallOpClientRecvStatus> finish_ops_; - std::unique_ptr<CompletionQueue> cq_; + CompletionQueue cq_; Call call_; }; @@ -255,13 +251,11 @@ class ClientReaderWriter GRPC_FINAL : public ClientReaderWriterInterface<W, R> { /// Blocking create a stream. ClientReaderWriter(ChannelInterface* channel, const RpcMethod& method, ClientContext* context) - : context_(context), - cq_(new CompletionQueue), - call_(channel->CreateCall(method, context, cq_.get())) { + : context_(context), call_(channel->CreateCall(method, context, &cq_)) { CallOpSet<CallOpSendInitialMetadata> ops; ops.SendInitialMetadata(context->send_initial_metadata_); call_.PerformOps(&ops); - cq_->Pluck(&ops); + cq_.Pluck(&ops); } void WaitForInitialMetadata() GRPC_OVERRIDE { @@ -270,7 +264,7 @@ class ClientReaderWriter GRPC_FINAL : public ClientReaderWriterInterface<W, R> { CallOpSet<CallOpRecvInitialMetadata> ops; ops.RecvInitialMetadata(context_); call_.PerformOps(&ops); - cq_->Pluck(&ops); // status ignored + cq_.Pluck(&ops); // status ignored } bool Read(R* msg) GRPC_OVERRIDE { @@ -280,7 +274,7 @@ class ClientReaderWriter GRPC_FINAL : public ClientReaderWriterInterface<W, R> { } ops.RecvMessage(msg); call_.PerformOps(&ops); - return cq_->Pluck(&ops) && ops.got_message; + return cq_.Pluck(&ops) && ops.got_message; } using WriterInterface<W>::Write; @@ -288,14 +282,14 @@ class ClientReaderWriter GRPC_FINAL : public ClientReaderWriterInterface<W, R> { CallOpSet<CallOpSendMessage> ops; if (!ops.SendMessage(msg, options).ok()) return false; call_.PerformOps(&ops); - return cq_->Pluck(&ops); + return cq_.Pluck(&ops); } bool WritesDone() GRPC_OVERRIDE { CallOpSet<CallOpClientSendClose> ops; ops.ClientSendClose(); call_.PerformOps(&ops); - return cq_->Pluck(&ops); + return cq_.Pluck(&ops); } Status Finish() GRPC_OVERRIDE { @@ -303,13 +297,13 @@ class ClientReaderWriter GRPC_FINAL : public ClientReaderWriterInterface<W, R> { Status status; ops.ClientRecvStatus(context_, &status); call_.PerformOps(&ops); - GPR_ASSERT(cq_->Pluck(&ops)); + GPR_ASSERT(cq_.Pluck(&ops)); return status; } private: ClientContext* context_; - std::unique_ptr<CompletionQueue> cq_; + CompletionQueue cq_; Call call_; }; |