GRPC C++  0.13.1-pre1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
method_handler_impl.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015-2016, 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 
39 
40 namespace grpc {
41 
42 // A wrapper class of an application provided rpc method handler.
43 template <class ServiceType, class RequestType, class ResponseType>
44 class RpcMethodHandler : public MethodHandler {
45  public:
47  std::function<Status(ServiceType*, ServerContext*, const RequestType*,
48  ResponseType*)> func,
49  ServiceType* service)
50  : func_(func), service_(service) {}
51 
52  void RunHandler(const HandlerParameter& param) GRPC_FINAL {
53  RequestType req;
55  param.request, &req, param.max_message_size);
56  ResponseType rsp;
57  if (status.ok()) {
58  status = func_(service_, param.server_context, &req, &rsp);
59  }
60 
61  GPR_ASSERT(!param.server_context->sent_initial_metadata_);
64  ops.SendInitialMetadata(param.server_context->initial_metadata_);
65  if (status.ok()) {
66  status = ops.SendMessage(rsp);
67  }
68  ops.ServerSendStatus(param.server_context->trailing_metadata_, status);
69  param.call->PerformOps(&ops);
70  param.call->cq()->Pluck(&ops);
71  }
72 
73  private:
74  // Application provided rpc handler function.
75  std::function<Status(ServiceType*, ServerContext*, const RequestType*,
76  ResponseType*)> func_;
77  // The class the above handler function lives in.
78  ServiceType* service_;
79 };
80 
81 // A wrapper class of an application provided client streaming handler.
82 template <class ServiceType, class RequestType, class ResponseType>
83 class ClientStreamingHandler : public MethodHandler {
84  public:
86  std::function<Status(ServiceType*, ServerContext*,
87  ServerReader<RequestType>*, ResponseType*)> func,
88  ServiceType* service)
89  : func_(func), service_(service) {}
90 
91  void RunHandler(const HandlerParameter& param) GRPC_FINAL {
92  ServerReader<RequestType> reader(param.call, param.server_context);
93  ResponseType rsp;
94  Status status = func_(service_, param.server_context, &reader, &rsp);
95 
96  GPR_ASSERT(!param.server_context->sent_initial_metadata_);
99  ops.SendInitialMetadata(param.server_context->initial_metadata_);
100  if (status.ok()) {
101  status = ops.SendMessage(rsp);
102  }
103  ops.ServerSendStatus(param.server_context->trailing_metadata_, status);
104  param.call->PerformOps(&ops);
105  param.call->cq()->Pluck(&ops);
106  }
107 
108  private:
109  std::function<Status(ServiceType*, ServerContext*, ServerReader<RequestType>*,
110  ResponseType*)> func_;
111  ServiceType* service_;
112 };
113 
114 // A wrapper class of an application provided server streaming handler.
115 template <class ServiceType, class RequestType, class ResponseType>
116 class ServerStreamingHandler : public MethodHandler {
117  public:
119  std::function<Status(ServiceType*, ServerContext*, const RequestType*,
121  ServiceType* service)
122  : func_(func), service_(service) {}
123 
125  RequestType req;
127  param.request, &req, param.max_message_size);
128 
129  if (status.ok()) {
130  ServerWriter<ResponseType> writer(param.call, param.server_context);
131  status = func_(service_, param.server_context, &req, &writer);
132  }
133 
135  if (!param.server_context->sent_initial_metadata_) {
136  ops.SendInitialMetadata(param.server_context->initial_metadata_);
137  }
138  ops.ServerSendStatus(param.server_context->trailing_metadata_, status);
139  param.call->PerformOps(&ops);
140  param.call->cq()->Pluck(&ops);
141  }
142 
143  private:
144  std::function<Status(ServiceType*, ServerContext*, const RequestType*,
146  ServiceType* service_;
147 };
148 
149 // A wrapper class of an application provided bidi-streaming handler.
150 template <class ServiceType, class RequestType, class ResponseType>
151 class BidiStreamingHandler : public MethodHandler {
152  public:
154  std::function<Status(ServiceType*, ServerContext*,
156  func,
157  ServiceType* service)
158  : func_(func), service_(service) {}
159 
162  param.server_context);
163  Status status = func_(service_, param.server_context, &stream);
164 
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*,
177  ServiceType* service_;
178 };
179 
180 // Handle unknown method by returning UNIMPLEMENTED error.
182  public:
183  template <class T>
184  static void FillOps(ServerContext* context, T* ops) {
185  Status status(StatusCode::UNIMPLEMENTED, "");
186  if (!context->sent_initial_metadata_) {
187  ops->SendInitialMetadata(context->initial_metadata_);
188  context->sent_initial_metadata_ = true;
189  }
190  ops->ServerSendStatus(context->trailing_metadata_, status);
191  }
192 
195  FillOps(param.server_context, &ops);
196  param.call->PerformOps(&ops);
197  param.call->cq()->Pluck(&ops);
198  }
199 };
200 
201 } // namespace grpc
202 
203 #endif // GRPCXX_IMPL_CODEGEN_METHOD_HANDLER_IMPL_H
Definition: rpc_service_method.h:53
ClientStreamingHandler(std::function< Status(ServiceType *, ServerContext *, ServerReader< RequestType > *, ResponseType *)> func, ServiceType *service)
Definition: method_handler_impl.h:85
void RunHandler(const HandlerParameter &param)
Definition: method_handler_impl.h:193
ServerStreamingHandler(std::function< Status(ServiceType *, ServerContext *, const RequestType *, ServerWriter< ResponseType > *)> func, ServiceType *service)
Definition: method_handler_impl.h:118
static void FillOps(ServerContext *context, T *ops)
Definition: method_handler_impl.h:184
void RunHandler(const HandlerParameter &param)
Definition: method_handler_impl.h:124
Definition: call.h:358
void RunHandler(const HandlerParameter &param)
Definition: method_handler_impl.h:91
Definition: completion_queue.h:54
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:64
Definition: method_handler_impl.h:181
Definition: call.h:182
void RunHandler(const HandlerParameter &param)
Definition: method_handler_impl.h:160
RpcMethodHandler(std::function< Status(ServiceType *, ServerContext *, const RequestType *, ResponseType *)> func, ServiceType *service)
Definition: method_handler_impl.h:46
Primary implementaiton of CallOpSetInterface.
Definition: call.h:524
Definition: server_context.h:90
void RunHandler(const HandlerParameter &param)
Definition: method_handler_impl.h:52
Definition: completion_queue.h:56
#define GRPC_FINAL
Definition: config.h:71
BidiStreamingHandler(std::function< Status(ServiceType *, ServerContext *, ServerReaderWriter< ResponseType, RequestType > *)> func, ServiceType *service)
Definition: method_handler_impl.h:153
Server-side interface for bi-directional streaming.
Definition: completion_queue.h:58
bool ok() const
Is the status OK?
Definition: status.h:67
Definition: rpc_service_method.h:56
Did it work? If it didn'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:150