GRPC C++ 0.10.0.0

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rpc_service_method.h
Go to the documentation of this file.
1 /*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 #ifndef GRPCXX_IMPL_RPC_SERVICE_METHOD_H
35 #define GRPCXX_IMPL_RPC_SERVICE_METHOD_H
36
37 #include <functional>
38 #include <map>
39 #include <memory>
40 #include <vector>
41
42 #include <grpc++/config.h>
43 #include <grpc++/impl/rpc_method.h>
44 #include <grpc++/status.h>
45 #include <grpc++/stream.h>
46
47 namespace grpc {
48 class ServerContext;
49 class StreamContextInterface;
50
51 // TODO(rocking): we might need to split this file into multiple ones.
52
53 // Base class for running an RPC handler.
54 class MethodHandler {
55 public:
56 virtual ~MethodHandler() {}
57 struct HandlerParameter {
58 HandlerParameter(Call* c, ServerContext* context, grpc_byte_buffer* req,
59 int max_size)
60 : call(c),
61 server_context(context),
62 request(req),
63 max_message_size(max_size) {}
64 Call* call;
65 ServerContext* server_context;
66 // Handler required to grpc_byte_buffer_destroy this
67 grpc_byte_buffer* request;
68 int max_message_size;
69 };
70 virtual void RunHandler(const HandlerParameter& param) = 0;
71 };
72
73 // A wrapper class of an application provided rpc method handler.
74 template <class ServiceType, class RequestType, class ResponseType>
75 class RpcMethodHandler : public MethodHandler {
76 public:
77 RpcMethodHandler(
78 std::function<Status(ServiceType*, ServerContext*, const RequestType*,
79 ResponseType*)> func,
80 ServiceType* service)
81 : func_(func), service_(service) {}
82
83 void RunHandler(const HandlerParameter& param) GRPC_FINAL {
84 RequestType req;
85 Status status = SerializationTraits<RequestType>::Deserialize(
86 param.request, &req, param.max_message_size);
87 ResponseType rsp;
88 if (status.ok()) {
89 status = func_(service_, param.server_context, &req, &rsp);
90 }
91
92 GPR_ASSERT(!param.server_context->sent_initial_metadata_);
93 CallOpSet<CallOpSendInitialMetadata, CallOpSendMessage,
94 CallOpServerSendStatus> ops;
95 ops.SendInitialMetadata(param.server_context->initial_metadata_);
96 if (status.ok()) {
97 status = ops.SendMessage(rsp);
98 }
99 ops.ServerSendStatus(param.server_context->trailing_metadata_, status);
100 param.call->PerformOps(&ops);
101 param.call->cq()->Pluck(&ops);
102 }
103
104 private:
105 // Application provided rpc handler function.
106 std::function<Status(ServiceType*, ServerContext*, const RequestType*,
107 ResponseType*)> func_;
108 // The class the above handler function lives in.
109 ServiceType* service_;
110 };
111
112 // A wrapper class of an application provided client streaming handler.
113 template <class ServiceType, class RequestType, class ResponseType>
114 class ClientStreamingHandler : public MethodHandler {
115 public:
116 ClientStreamingHandler(
117 std::function<Status(ServiceType*, ServerContext*,
118 ServerReader<RequestType>*, ResponseType*)> func,
119 ServiceType* service)
120 : func_(func), service_(service) {}
121
122 void RunHandler(const HandlerParameter& param) GRPC_FINAL {
123 ServerReader<RequestType> reader(param.call, param.server_context);
124 ResponseType rsp;
125 Status status = func_(service_, param.server_context, &reader, &rsp);
126
127 GPR_ASSERT(!param.server_context->sent_initial_metadata_);
128 CallOpSet<CallOpSendInitialMetadata, CallOpSendMessage,
129 CallOpServerSendStatus> ops;
130 ops.SendInitialMetadata(param.server_context->initial_metadata_);
131 if (status.ok()) {
132 status = ops.SendMessage(rsp);
133 }
134 ops.ServerSendStatus(param.server_context->trailing_metadata_, status);
135 param.call->PerformOps(&ops);
136 param.call->cq()->Pluck(&ops);
137 }
138
139 private:
140 std::function<Status(ServiceType*, ServerContext*, ServerReader<RequestType>*,
141 ResponseType*)> func_;
142 ServiceType* service_;
143 };
144
145 // A wrapper class of an application provided server streaming handler.
146 template <class ServiceType, class RequestType, class ResponseType>
147 class ServerStreamingHandler : public MethodHandler {
148 public:
149 ServerStreamingHandler(
150 std::function<Status(ServiceType*, ServerContext*, const RequestType*,
151 ServerWriter<ResponseType>*)> func,
152 ServiceType* service)
153 : func_(func), service_(service) {}
154
155 void RunHandler(const HandlerParameter& param) GRPC_FINAL {
156 RequestType req;
157 Status status = SerializationTraits<RequestType>::Deserialize(
158 param.request, &req, param.max_message_size);
159
160 if (status.ok()) {
161 ServerWriter<ResponseType> writer(param.call, param.server_context);
162 status = func_(service_, param.server_context, &req, &writer);
163 }
164
165 CallOpSet<CallOpSendInitialMetadata, CallOpServerSendStatus> ops;
166 if (!param.server_context->sent_initial_metadata_) {
167 ops.SendInitialMetadata(param.server_context->initial_metadata_);
168 }
169 ops.ServerSendStatus(param.server_context->trailing_metadata_, status);
170 param.call->PerformOps(&ops);
171 param.call->cq()->Pluck(&ops);
172 }
173
174 private:
175 std::function<Status(ServiceType*, ServerContext*, const RequestType*,
176 ServerWriter<ResponseType>*)> func_;
177 ServiceType* service_;
178 };
179
180 // A wrapper class of an application provided bidi-streaming handler.
181 template <class ServiceType, class RequestType, class ResponseType>
182 class BidiStreamingHandler : public MethodHandler {
183 public:
184 BidiStreamingHandler(
185 std::function<Status(ServiceType*, ServerContext*,
186 ServerReaderWriter<ResponseType, RequestType>*)>
187 func,
188 ServiceType* service)
189 : func_(func), service_(service) {}
190
191 void RunHandler(const HandlerParameter& param) GRPC_FINAL {
192 ServerReaderWriter<ResponseType, RequestType> stream(param.call,
193 param.server_context);
194 Status status = func_(service_, param.server_context, &stream);
195
196 CallOpSet<CallOpSendInitialMetadata, CallOpServerSendStatus> ops;
197 if (!param.server_context->sent_initial_metadata_) {
198 ops.SendInitialMetadata(param.server_context->initial_metadata_);
199 }
200 ops.ServerSendStatus(param.server_context->trailing_metadata_, status);
201 param.call->PerformOps(&ops);
202 param.call->cq()->Pluck(&ops);
203 }
204
205 private:
206 std::function<Status(ServiceType*, ServerContext*,
207 ServerReaderWriter<ResponseType, RequestType>*)> func_;
208 ServiceType* service_;
209 };
210
211 // Server side rpc method class
212 class RpcServiceMethod : public RpcMethod {
213 public:
214 // Takes ownership of the handler
215 RpcServiceMethod(const char* name, RpcMethod::RpcType type,
216 MethodHandler* handler)
217 : RpcMethod(name, type, nullptr), handler_(handler) {}
218
219 MethodHandler* handler() { return handler_.get(); }
220
221 private:
222 std::unique_ptr<MethodHandler> handler_;
223 };
224
225 // This class contains all the method information for an rpc service. It is
226 // used for registering a service on a grpc server.
227 class RpcService {
228 public:
229 // Takes ownership.
230 void AddMethod(RpcServiceMethod* method) { methods_.emplace_back(method); }
231
232 RpcServiceMethod* GetMethod(int i) { return methods_[i].get(); }
233 int GetMethodCount() const { return methods_.size(); }
234
235 private:
236 std::vector<std::unique_ptr<RpcServiceMethod>> methods_;
237 };
238
239 } // namespace grpc
240
241 #endif // GRPCXX_IMPL_RPC_SERVICE_METHOD_H
grpc::MethodHandler
Definition: rpc_service_method.h:54
grpc::RpcService::AddMethod
void AddMethod(RpcServiceMethod *method)
Definition: rpc_service_method.h:230
grpc::RpcService::GetMethod
RpcServiceMethod * GetMethod(int i)
Definition: rpc_service_method.h:232
grpc::ClientStreamingHandler::ClientStreamingHandler
ClientStreamingHandler(std::function< Status(ServiceType *, ServerContext *, ServerReader< RequestType > *, ResponseType *)> func, ServiceType *service)
Definition: rpc_service_method.h:116
grpc::ServerStreamingHandler::ServerStreamingHandler
ServerStreamingHandler(std::function< Status(ServiceType *, ServerContext *, const RequestType *, ServerWriter< ResponseType > *)> func, ServiceType *service)
Definition: rpc_service_method.h:149
grpc::MethodHandler::HandlerParameter::max_message_size
int max_message_size
Definition: rpc_service_method.h:68
config.h
grpc::MethodHandler::HandlerParameter::server_context
ServerContext * server_context
Definition: rpc_service_method.h:65
grpc::RpcMethod::RpcType
RpcType
Definition: rpc_method.h:41
grpc::MethodHandler::HandlerParameter::call
Call * call
Definition: rpc_service_method.h:64
grpc::ServerStreamingHandler::RunHandler
void RunHandler(const HandlerParameter &param)
Definition: rpc_service_method.h:155
grpc::CallOpServerSendStatus
Definition: call.h:360
GRPC_FINAL
#define GRPC_FINAL
Definition: config.h:71
grpc::ClientStreamingHandler::RunHandler
void RunHandler(const HandlerParameter &param)
Definition: rpc_service_method.h:122
stream.h
grpc::ServerReader
Definition: completion_queue.h:53
grpc::RpcServiceMethod
Definition: rpc_service_method.h:212
grpc::SerializationTraits
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:64
grpc::CallOpSendMessage
Definition: call.h:190
grpc::RpcService
Definition: rpc_service_method.h:227
grpc::MethodHandler::RunHandler
virtual void RunHandler(const HandlerParameter &param)=0
grpc::BidiStreamingHandler::RunHandler
void RunHandler(const HandlerParameter &param)
Definition: rpc_service_method.h:191
grpc::Call
Definition: call.h:565
grpc::RpcMethodHandler
Definition: completion_queue.h:59
grpc::RpcMethodHandler::RpcMethodHandler
RpcMethodHandler(std::function< Status(ServiceType *, ServerContext *, const RequestType *, ResponseType *)> func, ServiceType *service)
Definition: rpc_service_method.h:77
grpc::MethodHandler::~MethodHandler
virtual ~MethodHandler()
Definition: rpc_service_method.h:56
grpc::CallOpSet
Primary implementaiton of CallOpSetInterface.
Definition: call.h:506
grpc::ServerContext
Definition: server_context.h:86
grpc::RpcMethodHandler::RunHandler
void RunHandler(const HandlerParameter &param)
Definition: rpc_service_method.h:83
grpc::ServerWriter
Definition: completion_queue.h:55
status.h
grpc::RpcMethod
Definition: rpc_method.h:39
grpc::BidiStreamingHandler::BidiStreamingHandler
BidiStreamingHandler(std::function< Status(ServiceType *, ServerContext *, ServerReaderWriter< ResponseType, RequestType > *)> func, ServiceType *service)
Definition: rpc_service_method.h:184
rpc_method.h
grpc::ServerReaderWriter
Definition: completion_queue.h:57
grpc::Status::ok
bool ok() const
Definition: status.h:55
grpc::MethodHandler::HandlerParameter
Definition: rpc_service_method.h:57
grpc::Status
Definition: status.h:42
grpc::CallOpSendInitialMetadata
Definition: call.h:159
grpc::MethodHandler::HandlerParameter::HandlerParameter
HandlerParameter(Call *c, ServerContext *context, grpc_byte_buffer *req, int max_size)
Definition: rpc_service_method.h:58
grpc::RpcMethod::name
const char * name() const
Definition: rpc_method.h:51
grpc::RpcServiceMethod::handler
MethodHandler * handler()
Definition: rpc_service_method.h:219
grpc::RpcService::GetMethodCount
int GetMethodCount() const
Definition: rpc_service_method.h:233
grpc::MethodHandler::HandlerParameter::request
grpc_byte_buffer * request
Definition: rpc_service_method.h:67
grpc::RpcServiceMethod::RpcServiceMethod
RpcServiceMethod(const char *name, RpcMethod::RpcType type, MethodHandler *handler)
Definition: rpc_service_method.h:215