diff options
Diffstat (limited to 'include/grpc++')
-rw-r--r-- | include/grpc++/async_server.h | 70 | ||||
-rw-r--r-- | include/grpc++/async_server_context.h | 93 | ||||
-rw-r--r-- | include/grpc++/channel_interface.h | 68 | ||||
-rw-r--r-- | include/grpc++/client_context.h | 85 | ||||
-rw-r--r-- | include/grpc++/completion_queue.h | 87 | ||||
-rw-r--r-- | include/grpc++/config.h | 45 | ||||
-rw-r--r-- | include/grpc++/create_channel.h | 53 | ||||
-rw-r--r-- | include/grpc++/credentials.h | 95 | ||||
-rw-r--r-- | include/grpc++/server.h | 111 | ||||
-rw-r--r-- | include/grpc++/server_builder.h | 75 | ||||
-rw-r--r-- | include/grpc++/status.h | 65 | ||||
-rw-r--r-- | include/grpc++/status_code_enum.h | 199 | ||||
-rw-r--r-- | include/grpc++/stream.h | 178 | ||||
-rw-r--r-- | include/grpc++/stream_context_interface.h | 64 | ||||
-rw-r--r-- | include/grpc++/thread_pool_interface.h | 52 |
15 files changed, 1340 insertions, 0 deletions
diff --git a/include/grpc++/async_server.h b/include/grpc++/async_server.h new file mode 100644 index 0000000000..fe2c5d9367 --- /dev/null +++ b/include/grpc++/async_server.h @@ -0,0 +1,70 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_ASYNC_SERVER_H__ +#define __GRPCPP_ASYNC_SERVER_H__ + +#include <mutex> + +#include <grpc++/config.h> + +struct grpc_server; + +namespace grpc { +class CompletionQueue; + +class AsyncServer { + public: + explicit AsyncServer(CompletionQueue* cc); + ~AsyncServer(); + + void AddPort(const grpc::string& addr); + + void Start(); + + // The user has to call this to get one new rpc on the completion + // queue. + void RequestOneRpc(); + + void Shutdown(); + + private: + bool started_; + std::mutex shutdown_mu_; + bool shutdown_; + grpc_server* server_; +}; + +} // namespace grpc + +#endif // __GRPCPP_ASYNC_SERVER_H__ diff --git a/include/grpc++/async_server_context.h b/include/grpc++/async_server_context.h new file mode 100644 index 0000000000..dd4097b25c --- /dev/null +++ b/include/grpc++/async_server_context.h @@ -0,0 +1,93 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_ASYNC_SERVER_CONTEXT_H__ +#define __GRPCPP_ASYNC_SERVER_CONTEXT_H__ + +#include <chrono> + +#include <grpc++/config.h> + +struct grpc_byte_buffer; +struct grpc_call; +struct grpc_completion_queue; + +namespace google { +namespace protobuf { +class Message; +} +} + +using std::chrono::system_clock; + +namespace grpc { +class Status; + +// TODO(rocking): wrap grpc c structures. +class AsyncServerContext { + public: + AsyncServerContext(grpc_call* call, const grpc::string& method, + const grpc::string& host, + system_clock::time_point absolute_deadline); + ~AsyncServerContext(); + + // Accept this rpc, bind it to a completion queue. + void Accept(grpc_completion_queue* cq); + + // Read and write calls, all async. Return true for success. + bool StartRead(google::protobuf::Message* request); + bool StartWrite(const google::protobuf::Message& response, int flags); + bool StartWriteStatus(const Status& status); + + bool ParseRead(grpc_byte_buffer* read_buffer); + + grpc::string method() const { return method_; } + grpc::string host() const { return host_; } + system_clock::time_point absolute_deadline() { return absolute_deadline_; } + + private: + AsyncServerContext(const AsyncServerContext&); + AsyncServerContext& operator=(const AsyncServerContext&); + + // These properties may be moved to a ServerContext class. + const grpc::string method_; + const grpc::string host_; + system_clock::time_point absolute_deadline_; + + google::protobuf::Message* request_; // not owned + grpc_call* call_; // owned +}; + +} // namespace grpc + +#endif // __GRPCPP_ASYNC_SERVER_CONTEXT_H__ diff --git a/include/grpc++/channel_interface.h b/include/grpc++/channel_interface.h new file mode 100644 index 0000000000..4b9d76e0d1 --- /dev/null +++ b/include/grpc++/channel_interface.h @@ -0,0 +1,68 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_CHANNEL_INTERFACE_H__ +#define __GRPCPP_CHANNEL_INTERFACE_H__ + +#include <grpc++/status.h> + +namespace google { +namespace protobuf { +class Message; +} +} + +namespace grpc { + +class ClientContext; +class RpcMethod; +class StreamContextInterface; + +class ChannelInterface { + public: + virtual ~ChannelInterface() {} + + virtual Status StartBlockingRpc(const RpcMethod& method, + ClientContext* context, + const google::protobuf::Message& request, + google::protobuf::Message* result) = 0; + + virtual StreamContextInterface* CreateStream(const RpcMethod& method, + ClientContext* context, + const google::protobuf::Message* request, + google::protobuf::Message* result) = 0; +}; + +} // namespace grpc + +#endif // __GRPCPP_CHANNEL_INTERFACE_H__ diff --git a/include/grpc++/client_context.h b/include/grpc++/client_context.h new file mode 100644 index 0000000000..8301b3c630 --- /dev/null +++ b/include/grpc++/client_context.h @@ -0,0 +1,85 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_CLIENT_CONTEXT_H__ +#define __GRPCPP_CLIENT_CONTEXT_H__ + +#include <chrono> +#include <string> +#include <vector> + +#include <grpc++/config.h> + +using std::chrono::system_clock; + +struct grpc_call; +struct grpc_completion_queue; + +namespace grpc { + +class ClientContext { + public: + ClientContext(); + ~ClientContext(); + + void AddMetadata(const grpc::string &meta_key, + const grpc::string &meta_value); + + void set_absolute_deadline(const system_clock::time_point &deadline); + system_clock::time_point absolute_deadline(); + + void StartCancel(); + + private: + // Disallow copy and assign. + ClientContext(const ClientContext &); + ClientContext &operator=(const ClientContext &); + + friend class Channel; + friend class StreamContext; + + grpc_call *call() { return call_; } + void set_call(grpc_call *call) { call_ = call; } + + grpc_completion_queue *cq() { return cq_; } + void set_cq(grpc_completion_queue *cq) { cq_ = cq; } + + grpc_call *call_; + grpc_completion_queue *cq_; + system_clock::time_point absolute_deadline_; + std::vector<std::pair<grpc::string, grpc::string> > metadata_; +}; + +} // namespace grpc + +#endif // __GRPCPP_CLIENT_CONTEXT_H__ diff --git a/include/grpc++/completion_queue.h b/include/grpc++/completion_queue.h new file mode 100644 index 0000000000..72f6253f8e --- /dev/null +++ b/include/grpc++/completion_queue.h @@ -0,0 +1,87 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_COMPLETION_QUEUE_H__ +#define __GRPCPP_COMPLETION_QUEUE_H__ + +struct grpc_completion_queue; + +namespace grpc { + +// grpc_completion_queue wrapper class +class CompletionQueue { + public: + CompletionQueue(); + ~CompletionQueue(); + + enum CompletionType { + QUEUE_CLOSED = 0, // Shutting down. + RPC_END = 1, // An RPC finished. Either at client or server. + CLIENT_READ_OK = 2, // A client-side read has finished successfully. + CLIENT_READ_ERROR = 3, // A client-side read has finished with error. + CLIENT_WRITE_OK = 4, + CLIENT_WRITE_ERROR = 5, + SERVER_RPC_NEW = 6, // A new RPC just arrived at the server. + SERVER_READ_OK = 7, // A server-side read has finished successfully. + SERVER_READ_ERROR = 8, // A server-side read has finished with error. + SERVER_WRITE_OK = 9, + SERVER_WRITE_ERROR = 10, + // Client or server has sent half close successfully. + HALFCLOSE_OK = 11, + // New CompletionTypes may be added in the future, so user code should + // always + // handle the default case of a CompletionType that appears after such code + // was + // written. + DO_NOT_USE = 20, + }; + + // Blocking read from queue. + // For QUEUE_CLOSED, *tag is not changed. + // For SERVER_RPC_NEW, *tag will be a newly allocated AsyncServerContext. + // For others, *tag will be the AsyncServerContext of this rpc. + CompletionType Next(void** tag); + + // Shutdown has to be called, and the CompletionQueue can only be + // destructed when the QUEUE_CLOSED message has been read with Next(). + void Shutdown(); + + grpc_completion_queue* cq() { return cq_; } + + private: + grpc_completion_queue* cq_; // owned +}; + +} // namespace grpc + +#endif // __GRPCPP_COMPLETION_QUEUE_H__ diff --git a/include/grpc++/config.h b/include/grpc++/config.h new file mode 100644 index 0000000000..153b288f0c --- /dev/null +++ b/include/grpc++/config.h @@ -0,0 +1,45 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_CONFIG_H__ +#define __GRPCPP_CONFIG_H__ + +#include <string> + +namespace grpc { + +typedef std::string string; + +} + +#endif // __GRPCPP_CONFIG_H__ diff --git a/include/grpc++/create_channel.h b/include/grpc++/create_channel.h new file mode 100644 index 0000000000..3bb16b2618 --- /dev/null +++ b/include/grpc++/create_channel.h @@ -0,0 +1,53 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_CREATE_CHANNEL_H__ +#define __GRPCPP_CREATE_CHANNEL_H__ + +#include <memory> + +#include <grpc++/config.h> +#include <grpc++/credentials.h> + +namespace grpc { +class ChannelInterface; + +std::shared_ptr<ChannelInterface> CreateChannel(const grpc::string& target); + +std::shared_ptr<ChannelInterface> CreateChannel( + const grpc::string& target, + const std::unique_ptr<grpc::Credentials>& creds); + +} // namespace grpc + +#endif // __GRPCPP_CREATE_CHANNEL_H__ diff --git a/include/grpc++/credentials.h b/include/grpc++/credentials.h new file mode 100644 index 0000000000..f0db6c252b --- /dev/null +++ b/include/grpc++/credentials.h @@ -0,0 +1,95 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_CREDENTIALS_H_ +#define __GRPCPP_CREDENTIALS_H_ + +#include <memory> + +#include <grpc++/config.h> + +struct grpc_credentials; + +namespace grpc { + +// grpc_credentials wrapper class. Typical use in C++ applications is limited +// to creating an instance using CredentialsFactory, and passing it down +// during channel construction. + +class Credentials final { + public: + ~Credentials(); + // TODO(abhikumar): Specify a plugin API here to be implemented by + // credentials that do not have a corresponding implementation in C. + + protected: + explicit Credentials(grpc_credentials*); + + private: + grpc_credentials* GetRawCreds(); + + friend class CredentialsFactory; + + grpc_credentials* creds_; +}; + +// Options used to build SslCredentials +struct SslCredentialsOptions { + grpc::string pem_root_certs; + grpc::string pem_private_key; + grpc::string pem_cert_chain; +}; + +// Factory for building different types of Credentials +class CredentialsFactory { + public: + // Builds credentials with reasonable defaults. + static std::unique_ptr<Credentials> DefaultCredentials(); + + // Builds SSL Credentials given SSL specific options + static std::unique_ptr<Credentials> SslCredentials( + const SslCredentialsOptions& options); + + // Builds credentials for use when running in GCE + static std::unique_ptr<Credentials> ComputeEngineCredentials(); + + + // Combines two credentials objects into a composite credentials + static std::unique_ptr<Credentials> ComposeCredentials( + const std::unique_ptr<Credentials>& creds1, + const std::unique_ptr<Credentials>& creds2); +}; + +} // namespace grpc + +#endif // __GRPCPP_CREDENTIALS_H_ diff --git a/include/grpc++/server.h b/include/grpc++/server.h new file mode 100644 index 0000000000..443b4d42f2 --- /dev/null +++ b/include/grpc++/server.h @@ -0,0 +1,111 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_SERVER_H__ +#define __GRPCPP_SERVER_H__ + +#include <condition_variable> +#include <map> +#include <memory> +#include <mutex> + +#include <grpc++/completion_queue.h> +#include <grpc++/config.h> +#include <grpc++/status.h> + +struct grpc_server; + +namespace google { +namespace protobuf { +class Message; +} +} + +namespace grpc { +class AsyncServerContext; +class RpcService; +class RpcServiceMethod; +class ThreadPoolInterface; + +// Currently it only supports handling rpcs in a single thread. +class Server { + public: + ~Server(); + + // Shutdown the server, block until all rpc processing finishes. + void Shutdown(); + + private: + friend class ServerBuilder; + + // ServerBuilder use only + explicit Server(ThreadPoolInterface* thread_pool); + Server(); + // Register a service. This call does not take ownership of the service. + // The service must exist for the lifetime of the Server instance. + void RegisterService(RpcService* service); + // Add a listening port. Can be called multiple times. + void AddPort(const grpc::string& addr); + // Start the server. + void Start(); + + void AllowOneRpc(); + void HandleQueueClosed(); + void RunRpc(); + void ScheduleCallback(); + + // Completion queue. + CompletionQueue cq_; + + // Sever status + std::mutex mu_; + bool started_; + bool shutdown_; + // The number of threads which are running callbacks. + int num_running_cb_; + std::condition_variable callback_cv_; + + // Pointer to the c grpc server. + grpc_server* server_; + + // A map for all method information. + std::map<grpc::string, RpcServiceMethod*> method_map_; + + ThreadPoolInterface* thread_pool_; + // Whether the thread pool is created and owned by the server. + bool thread_pool_owned_; +}; + +} // namespace grpc + +#endif // __GRPCPP_SERVER_H__ diff --git a/include/grpc++/server_builder.h b/include/grpc++/server_builder.h new file mode 100644 index 0000000000..89e9a25107 --- /dev/null +++ b/include/grpc++/server_builder.h @@ -0,0 +1,75 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_SERVER_BUILDER_H__ +#define __GRPCPP_SERVER_BUILDER_H__ + +#include <memory> +#include <vector> + +#include <grpc++/config.h> + +namespace grpc { + +class RpcService; +class Server; +class ThreadPoolInterface; + +class ServerBuilder { + public: + ServerBuilder(); + + // Register a service. This call does not take ownership of the service. + // The service must exist for the lifetime of the Server instance returned by + // BuildAndStart(). + void RegisterService(RpcService* service); + + // Add a listening port. Can be called multiple times. + void AddPort(const grpc::string& addr); + + // Set the thread pool used for running appliation rpc handlers. + // Does not take ownership. + void SetThreadPool(ThreadPoolInterface* thread_pool); + + // Return a running server which is ready for processing rpcs. + std::unique_ptr<Server> BuildAndStart(); + + private: + std::vector<RpcService*> services_; + std::vector<grpc::string> ports_; + ThreadPoolInterface* thread_pool_; +}; + +} // namespace grpc + +#endif // __GRPCPP_SERVER_BUILDER_H__ diff --git a/include/grpc++/status.h b/include/grpc++/status.h new file mode 100644 index 0000000000..432158a989 --- /dev/null +++ b/include/grpc++/status.h @@ -0,0 +1,65 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_STATUS_H__ +#define __GRPCPP_STATUS_H__ + +#include <grpc++/status_code_enum.h> +#include <grpc++/config.h> + +namespace grpc { + +class Status { + public: + Status() : code_(StatusCode::OK) {} + explicit Status(StatusCode code) : code_(code) {} + Status(StatusCode code, const grpc::string& details) + : code_(code), details_(details) {} + + // Pre-defined special status objects. + static const Status& OK; + static const Status& Cancelled; + + StatusCode code() const { return code_; } + grpc::string details() const { return details_; } + + bool IsOk() const { return code_ == StatusCode::OK; } + + private: + StatusCode code_; + grpc::string details_; +}; + +} // namespace grpc + +#endif // __GRPCPP_STATUS_H__ diff --git a/include/grpc++/status_code_enum.h b/include/grpc++/status_code_enum.h new file mode 100644 index 0000000000..964420dcc4 --- /dev/null +++ b/include/grpc++/status_code_enum.h @@ -0,0 +1,199 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_STATUS_CODE_ENUM_H__ +#define __GRPCPP_STATUS_CODE_ENUM_H__ + + +namespace grpc { + +enum StatusCode { + /* Not an error; returned on success + + HTTP Mapping: 200 OK */ + OK = 0, + + /* The operation was cancelled (typically by the caller). + + HTTP Mapping: 499 Client Closed Request */ + CANCELLED = 1, + + /* Unknown error. An example of where this error may be returned is + if a Status value received from another address space belongs to + an error-space that is not known in this address space. Also + errors raised by APIs that do not return enough error information + may be converted to this error. + + HTTP Mapping: 500 Internal Server Error */ + UNKNOWN = 2, + + /* Client specified an invalid argument. Note that this differs + from FAILED_PRECONDITION. INVALID_ARGUMENT indicates arguments + that are problematic regardless of the state of the system + (e.g., a malformed file name). + + HTTP Mapping: 400 Bad Request */ + INVALID_ARGUMENT = 3, + + /* Deadline expired before operation could complete. For operations + that change the state of the system, this error may be returned + even if the operation has completed successfully. For example, a + successful response from a server could have been delayed long + enough for the deadline to expire. + + HTTP Mapping: 504 Gateway Timeout */ + DEADLINE_EXCEEDED = 4, + + /* Some requested entity (e.g., file or directory) was not found. + + HTTP Mapping: 404 Not Found */ + NOT_FOUND = 5, + + /* Some entity that we attempted to create (e.g., file or directory) + already exists. + + HTTP Mapping: 409 Conflict */ + ALREADY_EXISTS = 6, + + /* The caller does not have permission to execute the specified + operation. PERMISSION_DENIED must not be used for rejections + caused by exhausting some resource (use RESOURCE_EXHAUSTED + instead for those errors). PERMISSION_DENIED must not be + used if the caller can not be identified (use UNAUTHENTICATED + instead for those errors). + + HTTP Mapping: 403 Forbidden */ + PERMISSION_DENIED = 7, + + /* The request does not have valid authentication credentials for the + operation. + + HTTP Mapping: 401 Unauthorized */ + UNAUTHENTICATED = 16, + + /* Some resource has been exhausted, perhaps a per-user quota, or + perhaps the entire file system is out of space. + + HTTP Mapping: 429 Too Many Requests */ + RESOURCE_EXHAUSTED = 8, + + /* Operation was rejected because the system is not in a state + required for the operation's execution. For example, directory + to be deleted may be non-empty, an rmdir operation is applied to + a non-directory, etc. + + A litmus test that may help a service implementor in deciding + between FAILED_PRECONDITION, ABORTED, and UNAVAILABLE: + (a) Use UNAVAILABLE if the client can retry just the failing call. + (b) Use ABORTED if the client should retry at a higher-level + (e.g., restarting a read-modify-write sequence). + (c) Use FAILED_PRECONDITION if the client should not retry until + the system state has been explicitly fixed. E.g., if an "rmdir" + fails because the directory is non-empty, FAILED_PRECONDITION + should be returned since the client should not retry unless + they have first fixed up the directory by deleting files from it. + (d) Use FAILED_PRECONDITION if the client performs conditional + REST Get/Update/Delete on a resource and the resource on the + server does not match the condition. E.g., conflicting + read-modify-write on the same resource. + + HTTP Mapping: 400 Bad Request + + NOTE: HTTP spec says 412 Precondition Failed should only be used if + the request contains Etag related headers. So if the server does see + Etag related headers in the request, it may choose to return 412 + instead of 400 for this error code. */ + FAILED_PRECONDITION = 9, + + /* The operation was aborted, typically due to a concurrency issue + like sequencer check failures, transaction aborts, etc. + + See litmus test above for deciding between FAILED_PRECONDITION, + ABORTED, and UNAVAILABLE. + + HTTP Mapping: 409 Conflict */ + ABORTED = 10, + + /* Operation was attempted past the valid range. E.g., seeking or + reading past end of file. + + Unlike INVALID_ARGUMENT, this error indicates a problem that may + be fixed if the system state changes. For example, a 32-bit file + system will generate INVALID_ARGUMENT if asked to read at an + offset that is not in the range [0,2^32-1], but it will generate + OUT_OF_RANGE if asked to read from an offset past the current + file size. + + There is a fair bit of overlap between FAILED_PRECONDITION and + OUT_OF_RANGE. We recommend using OUT_OF_RANGE (the more specific + error) when it applies so that callers who are iterating through + a space can easily look for an OUT_OF_RANGE error to detect when + they are done. + + HTTP Mapping: 400 Bad Request */ + OUT_OF_RANGE = 11, + + /* Operation is not implemented or not supported/enabled in this service. + + HTTP Mapping: 501 Not Implemented */ + UNIMPLEMENTED = 12, + + /* Internal errors. Means some invariants expected by underlying + system has been broken. If you see one of these errors, + something is very broken. + + HTTP Mapping: 500 Internal Server Error */ + INTERNAL = 13, + + /* The service is currently unavailable. This is a most likely a + transient condition and may be corrected by retrying with + a backoff. + + See litmus test above for deciding between FAILED_PRECONDITION, + ABORTED, and UNAVAILABLE. + + HTTP Mapping: 503 Service Unavailable */ + UNAVAILABLE = 14, + + /* Unrecoverable data loss or corruption. + + HTTP Mapping: 500 Internal Server Error */ + DATA_LOSS = 15, + + /* Force users to include a default branch: */ + DO_NOT_USE = -1 +}; + +} // namespace grpc + +#endif // __GRPCPP_STATUS_CODE_ENUM_H_ diff --git a/include/grpc++/stream.h b/include/grpc++/stream.h new file mode 100644 index 0000000000..50c21565b5 --- /dev/null +++ b/include/grpc++/stream.h @@ -0,0 +1,178 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_STREAM_H__ +#define __GRPCPP_STREAM_H__ + +#include <grpc++/stream_context_interface.h> +#include <grpc++/status.h> +#include <grpc/support/log.h> + +namespace grpc { + +// Common interface for all client side streaming. +class ClientStreamingInterface { + public: + virtual ~ClientStreamingInterface() {} + + // Try to cancel the stream. Wait() still needs to be called to get the final + // status. Cancelling after the stream has finished has no effects. + virtual void Cancel() = 0; + + // Wait until the stream finishes, and return the final status. When the + // client side declares it has no more message to send, either implicitly or + // by calling WritesDone, it needs to make sure there is no more message to + // be received from the server, either implicitly or by getting a false from + // a Read(). Otherwise, this implicitly cancels the stream. + virtual const Status& Wait() = 0; +}; + +// An interface that yields a sequence of R messages. +template <class R> +class ReaderInterface { + public: + virtual ~ReaderInterface() {} + + // Blocking read a message and parse to msg. Returns true on success. + // The method returns false when there will be no more incoming messages, + // either because the other side has called WritesDone or the stream has + // failed (or been cancelled). + virtual bool Read(R* msg) = 0; +}; + +// An interface that can be fed a sequence of W messages. +template <class W> +class WriterInterface { + public: + virtual ~WriterInterface() {} + + // Blocking write msg to the stream. Returns true on success. + // Returns false when the stream has been closed. + virtual bool Write(const W& msg) = 0; +}; + +template <class R> +class ClientReader : public ClientStreamingInterface, + public ReaderInterface<R> { + public: + // Blocking create a stream and write the first request out. + explicit ClientReader(StreamContextInterface* context) : context_(context) { + GPR_ASSERT(context_); + context_->Start(true); + context_->Write(context_->request(), true); + } + + ~ClientReader() { delete context_; } + + virtual bool Read(R* msg) { return context_->Read(msg); } + + virtual void Cancel() { context_->FinishStream(Status::Cancelled, true); } + + virtual const Status& Wait() { return context_->Wait(); } + + private: + StreamContextInterface* const context_; +}; + +template <class W> +class ClientWriter : public ClientStreamingInterface, + public WriterInterface<W> { + public: + // Blocking create a stream. + explicit ClientWriter(StreamContextInterface* context) : context_(context) { + GPR_ASSERT(context_); + context_->Start(false); + } + + ~ClientWriter() { delete context_; } + + virtual bool Write(const W& msg) { + return context_->Write(const_cast<W*>(&msg), false); + } + + virtual void WritesDone() { context_->Write(nullptr, true); } + + virtual void Cancel() { context_->FinishStream(Status::Cancelled, true); } + + // Read the final response and wait for the final status. + virtual const Status& Wait() { + bool success = context_->Read(context_->response()); + if (!success) { + Cancel(); + } else { + success = context_->Read(nullptr); + if (success) { + Cancel(); + } + } + return context_->Wait(); + } + + private: + StreamContextInterface* const context_; +}; + +// Client-side interface for bi-directional streaming. +template <class W, class R> +class ClientReaderWriter : public ClientStreamingInterface, + public WriterInterface<W>, + public ReaderInterface<R> { + public: + // Blocking create a stream. + explicit ClientReaderWriter(StreamContextInterface* context) + : context_(context) { + GPR_ASSERT(context_); + context_->Start(false); + } + + ~ClientReaderWriter() { delete context_; } + + virtual bool Read(R* msg) { return context_->Read(msg); } + + virtual bool Write(const W& msg) { + return context_->Write(const_cast<W*>(&msg), false); + } + + virtual void WritesDone() { context_->Write(nullptr, true); } + + virtual void Cancel() { context_->FinishStream(Status::Cancelled, true); } + + virtual const Status& Wait() { return context_->Wait(); } + + private: + StreamContextInterface* const context_; +}; + +} // namespace grpc + +#endif // __GRPCPP_STREAM_H__ diff --git a/include/grpc++/stream_context_interface.h b/include/grpc++/stream_context_interface.h new file mode 100644 index 0000000000..eb5c092059 --- /dev/null +++ b/include/grpc++/stream_context_interface.h @@ -0,0 +1,64 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_STREAM_CONTEXT_INTERFACE_H__ +#define __GRPCPP_STREAM_CONTEXT_INTERFACE_H__ + +namespace google { +namespace protobuf { +class Message; +} +} + +namespace grpc { +class Status; + +// An interface to avoid dependency on internal implementation. +class StreamContextInterface { + public: + virtual ~StreamContextInterface() {} + + virtual void Start(bool buffered) = 0; + + virtual bool Read(google::protobuf::Message* msg) = 0; + virtual bool Write(const google::protobuf::Message* msg, bool is_last) = 0; + virtual const Status& Wait() = 0; + virtual void FinishStream(const Status& status, bool send) = 0; + + virtual const google::protobuf::Message* request() = 0; + virtual google::protobuf::Message* response() = 0; +}; + +} // namespace grpc + +#endif // __GRPCPP_STREAM_CONTEXT_INTERFACE_H__ diff --git a/include/grpc++/thread_pool_interface.h b/include/grpc++/thread_pool_interface.h new file mode 100644 index 0000000000..a8eacb037f --- /dev/null +++ b/include/grpc++/thread_pool_interface.h @@ -0,0 +1,52 @@ +/* + * + * Copyright 2014, 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 __GRPCPP_THREAD_POOL_INTERFACE_H__ +#define __GRPCPP_THREAD_POOL_INTERFACE_H__ + +#include <functional> + +namespace grpc { + +// A thread pool interface for running callbacks. +class ThreadPoolInterface { + public: + virtual ~ThreadPoolInterface() {} + + // Schedule the given callback for execution. + virtual void ScheduleCallback(const std::function<void()>& callback) = 0; +}; + +} // namespace grpc + +#endif // __GRPCPP_THREAD_POOL_INTERFACE_H__ |