GRPC C++  1.0.0
method_handler_impl.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_CODEGEN_METHOD_HANDLER_IMPL_H
35 #define GRPCXX_IMPL_CODEGEN_METHOD_HANDLER_IMPL_H
36 
40 
41 namespace grpc {
42 
43 // A wrapper class of an application provided rpc method handler.
44 template <class ServiceType, class RequestType, class ResponseType>
45 class RpcMethodHandler : public MethodHandler {
46  public:
47  RpcMethodHandler(std::function<Status(ServiceType*, ServerContext*,
48  const RequestType*, ResponseType*)>
49  func,
50  ServiceType* service)
51  : func_(func), service_(service) {}
52 
53  void RunHandler(const HandlerParameter& param) GRPC_FINAL {
54  RequestType req;
56  param.request, &req, param.max_message_size);
57  ResponseType rsp;
58  if (status.ok()) {
59  status = func_(service_, param.server_context, &req, &rsp);
60  }
61 
62  GPR_CODEGEN_ASSERT(!param.server_context->sent_initial_metadata_);
65  ops;
66  ops.SendInitialMetadata(param.server_context->initial_metadata_,
67  param.server_context->initial_metadata_flags());
68  if (param.server_context->compression_level_set()) {
69  ops.set_compression_level(param.server_context->compression_level());
70  }
71  if (status.ok()) {
72  status = ops.SendMessage(rsp);
73  }
74  ops.ServerSendStatus(param.server_context->trailing_metadata_, status);
75  param.call->PerformOps(&ops);
76  param.call->cq()->Pluck(&ops);
77  }
78 
79  private:
80  // Application provided rpc handler function.
81  std::function<Status(ServiceType*, ServerContext*, const RequestType*,
82  ResponseType*)>
83  func_;
84  // The class the above handler function lives in.
85  ServiceType* service_;
86 };
87 
88 // A wrapper class of an application provided client streaming handler.
89 template <class ServiceType, class RequestType, class ResponseType>
91  public:
93  std::function<Status(ServiceType*, ServerContext*,
94  ServerReader<RequestType>*, ResponseType*)>
95  func,
96  ServiceType* service)
97  : func_(func), service_(service) {}
98 
99  void RunHandler(const HandlerParameter& param) GRPC_FINAL {
100  ServerReader<RequestType> reader(param.call, param.server_context);
101  ResponseType rsp;
102  Status status = func_(service_, param.server_context, &reader, &rsp);
103 
104  GPR_CODEGEN_ASSERT(!param.server_context->sent_initial_metadata_);
107  ops;
108  ops.SendInitialMetadata(param.server_context->initial_metadata_,
109  param.server_context->initial_metadata_flags());
110  if (param.server_context->compression_level_set()) {
111  ops.set_compression_level(param.server_context->compression_level());
112  }
113  if (status.ok()) {
114  status = ops.SendMessage(rsp);
115  }
116  ops.ServerSendStatus(param.server_context->trailing_metadata_, status);
117  param.call->PerformOps(&ops);
118  param.call->cq()->Pluck(&ops);
119  }
120 
121  private:
122  std::function<Status(ServiceType*, ServerContext*, ServerReader<RequestType>*,
123  ResponseType*)>
124  func_;
125  ServiceType* service_;
126 };
127 
128 // A wrapper class of an application provided server streaming handler.
129 template <class ServiceType, class RequestType, class ResponseType>
130 class ServerStreamingHandler : public MethodHandler {
131  public:
133  std::function<Status(ServiceType*, ServerContext*, const RequestType*,
135  func,
136  ServiceType* service)
137  : func_(func), service_(service) {}
138 
140  RequestType req;
142  param.request, &req, param.max_message_size);
143 
144  if (status.ok()) {
145  ServerWriter<ResponseType> writer(param.call, param.server_context);
146  status = func_(service_, param.server_context, &req, &writer);
147  }
148 
150  if (!param.server_context->sent_initial_metadata_) {
151  ops.SendInitialMetadata(param.server_context->initial_metadata_,
152  param.server_context->initial_metadata_flags());
153  if (param.server_context->compression_level_set()) {
154  ops.set_compression_level(param.server_context->compression_level());
155  }
156  }
157  ops.ServerSendStatus(param.server_context->trailing_metadata_, status);
158  param.call->PerformOps(&ops);
159  param.call->cq()->Pluck(&ops);
160  }
161 
162  private:
163  std::function<Status(ServiceType*, ServerContext*, const RequestType*,
165  func_;
166  ServiceType* service_;
167 };
168 
169 // A wrapper class of an application provided bidi-streaming handler.
170 template <class ServiceType, class RequestType, class ResponseType>
171 class BidiStreamingHandler : public MethodHandler {
172  public:
174  std::function<Status(ServiceType*, ServerContext*,
176  func,
177  ServiceType* service)
178  : func_(func), service_(service) {}
179 
182  param.server_context);
183  Status status = func_(service_, param.server_context, &stream);
184 
186  if (!param.server_context->sent_initial_metadata_) {
187  ops.SendInitialMetadata(param.server_context->initial_metadata_,
188  param.server_context->initial_metadata_flags());
189  if (param.server_context->compression_level_set()) {
190  ops.set_compression_level(param.server_context->compression_level());
191  }
192  }
193  ops.ServerSendStatus(param.server_context->trailing_metadata_, status);
194  param.call->PerformOps(&ops);
195  param.call->cq()->Pluck(&ops);
196  }
197 
198  private:
199  std::function<Status(ServiceType*, ServerContext*,
201  func_;
202  ServiceType* service_;
203 };
204 
205 // Handle unknown method by returning UNIMPLEMENTED error.
207  public:
208  template <class T>
209  static void FillOps(ServerContext* context, T* ops) {
210  Status status(StatusCode::UNIMPLEMENTED, "");
211  if (!context->sent_initial_metadata_) {
212  ops->SendInitialMetadata(context->initial_metadata_,
213  context->initial_metadata_flags());
214  if (context->compression_level_set()) {
215  ops->set_compression_level(context->compression_level());
216  }
217  context->sent_initial_metadata_ = true;
218  }
219  ops->ServerSendStatus(context->trailing_metadata_, status);
220  }
221 
224  FillOps(param.server_context, &ops);
225  param.call->PerformOps(&ops);
226  param.call->cq()->Pluck(&ops);
227  }
228 };
229 
230 } // namespace grpc
231 
232 #endif // GRPCXX_IMPL_CODEGEN_METHOD_HANDLER_IMPL_H
grpc_compression_level compression_level() const
Definition: server_context.h:130
Definition: rpc_service_method.h:53
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:97
ClientStreamingHandler(std::function< Status(ServiceType *, ServerContext *, ServerReader< RequestType > *, ResponseType *)> func, ServiceType *service)
Definition: method_handler_impl.h:92
void RunHandler(const HandlerParameter &param)
Definition: method_handler_impl.h:222
ServerStreamingHandler(std::function< Status(ServiceType *, ServerContext *, const RequestType *, ServerWriter< ResponseType > *)> func, ServiceType *service)
Definition: method_handler_impl.h:132
static void FillOps(ServerContext *context, T *ops)
Definition: method_handler_impl.h:209
void RunHandler(const HandlerParameter &param)
Definition: method_handler_impl.h:139
Definition: call.h:427
void RunHandler(const HandlerParameter &param)
Definition: method_handler_impl.h:99
Definition: completion_queue.h:68
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:64
Definition: method_handler_impl.h:206
Definition: call.h:232
void RunHandler(const HandlerParameter &param)
Definition: method_handler_impl.h:180
RpcMethodHandler(std::function< Status(ServiceType *, ServerContext *, const RequestType *, ResponseType *)> func, ServiceType *service)
Definition: method_handler_impl.h:47
Definition: alarm.h:48
Primary implementaiton of CallOpSetInterface.
Definition: call.h:593
Definition: server_context.h:91
void RunHandler(const HandlerParameter &param)
Definition: method_handler_impl.h:53
Definition: completion_queue.h:70
#define GRPC_FINAL
Definition: config.h:72
Definition: completion_queue.h:76
BidiStreamingHandler(std::function< Status(ServiceType *, ServerContext *, ServerReaderWriter< ResponseType, RequestType > *)> func, ServiceType *service)
Definition: method_handler_impl.h:173
Server-side interface for bi-directional streaming.
Definition: completion_queue.h:72
bool ok() const
Is the status OK?
Definition: status.h:67
Definition: rpc_service_method.h:56
Definition: completion_queue.h:78
Did it work? If it didn&#39;t, why?
Definition: status.h:45
Operation is not implemented or not supported/enabled in this service.
Definition: status_code_enum.h:130
Definition: call.h:181
bool compression_level_set() const
Definition: server_context.h:139
Definition: completion_queue.h:80