| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef GRPCPP_IMPL_SERVICE_TYPE_H |
| 20 |
#define GRPCPP_IMPL_SERVICE_TYPE_H |
| 21 |
|
| 22 |
#include <grpcpp/impl/codegen/core_codegen_interface.h> |
| 23 |
#include <grpcpp/impl/codegen/server_interface.h> |
| 24 |
#include <grpcpp/impl/rpc_service_method.h> |
| 25 |
#include <grpcpp/impl/serialization_traits.h> |
| 26 |
#include <grpcpp/support/config.h> |
| 27 |
#include <grpcpp/support/status.h> |
| 28 |
|
| 29 |
namespace grpc { |
| 30 |
|
| 31 |
class CompletionQueue; |
| 32 |
class ServerContext; |
| 33 |
class ServerInterface; |
| 34 |
|
| 35 |
namespace internal { |
| 36 |
class Call; |
| 37 |
class ServerAsyncStreamingInterface { |
| 38 |
public: |
| 39 |
virtual ~ServerAsyncStreamingInterface() {} |
| 40 |
|
| 41 |
/// Request notification of the sending of initial metadata to the client. |
| 42 |
/// Completion will be notified by \a tag on the associated completion |
| 43 |
/// queue. This call is optional, but if it is used, it cannot be used |
| 44 |
/// concurrently with or after the \a Finish method. |
| 45 |
/// |
| 46 |
/// \param[in] tag Tag identifying this request. |
| 47 |
virtual void SendInitialMetadata(void* tag) = 0; |
| 48 |
|
| 49 |
private: |
| 50 |
friend class grpc::ServerInterface; |
| 51 |
virtual void BindCall(Call* call) = 0; |
| 52 |
}; |
| 53 |
} // namespace internal |
| 54 |
|
| 55 |
/// Desriptor of an RPC service and its various RPC methods |
| 56 |
class Service { |
| 57 |
public: |
| 58 |
Service() : server_(nullptr) {} |
| 59 |
virtual ~Service() {} |
| 60 |
|
| 61 |
bool has_async_methods() const { |
| 62 |
for (const auto& method : methods_) { |
| 63 |
if (method && method->handler() == nullptr) { |
| 64 |
return true; |
| 65 |
} |
| 66 |
} |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
bool has_synchronous_methods() const { |
| 71 |
for (const auto& method : methods_) { |
| 72 |
if (method && |
| 73 |
method->api_type() == internal::RpcServiceMethod::ApiType::SYNC) { |
| 74 |
return true; |
| 75 |
} |
| 76 |
} |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
bool has_callback_methods() const { |
| 81 |
for (const auto& method : methods_) { |
| 82 |
if (method && (method->api_type() == |
| 83 |
internal::RpcServiceMethod::ApiType::CALL_BACK || |
| 84 |
method->api_type() == |
| 85 |
internal::RpcServiceMethod::ApiType::RAW_CALL_BACK)) { |
| 86 |
return true; |
| 87 |
} |
| 88 |
} |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
bool has_generic_methods() const { |
| 93 |
for (const auto& method : methods_) { |
| 94 |
if (method == nullptr) { |
| 95 |
return true; |
| 96 |
} |
| 97 |
} |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
protected: |
| 102 |
template <class Message> |
| 103 |
void RequestAsyncUnary(int index, grpc::ServerContext* context, |
| 104 |
Message* request, |
| 105 |
internal::ServerAsyncStreamingInterface* stream, |
| 106 |
grpc::CompletionQueue* call_cq, |
| 107 |
grpc::ServerCompletionQueue* notification_cq, |
| 108 |
void* tag) { |
| 109 |
// Typecast the index to size_t for indexing into a vector |
| 110 |
// while preserving the API that existed before a compiler |
| 111 |
// warning was first seen (grpc/grpc#11664) |
| 112 |
size_t idx = static_cast<size_t>(index); |
| 113 |
server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq, |
| 114 |
notification_cq, tag, request); |
| 115 |
} |
| 116 |
void RequestAsyncClientStreaming( |
| 117 |
int index, grpc::ServerContext* context, |
| 118 |
internal::ServerAsyncStreamingInterface* stream, |
| 119 |
grpc::CompletionQueue* call_cq, |
| 120 |
grpc::ServerCompletionQueue* notification_cq, void* tag) { |
| 121 |
size_t idx = static_cast<size_t>(index); |
| 122 |
server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq, |
| 123 |
notification_cq, tag); |
| 124 |
} |
| 125 |
template <class Message> |
| 126 |
void RequestAsyncServerStreaming( |
| 127 |
int index, grpc::ServerContext* context, Message* request, |
| 128 |
internal::ServerAsyncStreamingInterface* stream, |
| 129 |
grpc::CompletionQueue* call_cq, |
| 130 |
grpc::ServerCompletionQueue* notification_cq, void* tag) { |
| 131 |
size_t idx = static_cast<size_t>(index); |
| 132 |
server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq, |
| 133 |
notification_cq, tag, request); |
| 134 |
} |
| 135 |
void RequestAsyncBidiStreaming( |
| 136 |
int index, grpc::ServerContext* context, |
| 137 |
internal::ServerAsyncStreamingInterface* stream, |
| 138 |
grpc::CompletionQueue* call_cq, |
| 139 |
grpc::ServerCompletionQueue* notification_cq, void* tag) { |
| 140 |
size_t idx = static_cast<size_t>(index); |
| 141 |
server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq, |
| 142 |
notification_cq, tag); |
| 143 |
} |
| 144 |
|
| 145 |
void AddMethod(internal::RpcServiceMethod* method) { |
| 146 |
methods_.emplace_back(method); |
| 147 |
} |
| 148 |
|
| 149 |
void MarkMethodAsync(int index) { |
| 150 |
// This does not have to be a hard error, however no one has approached us |
| 151 |
// with a use case yet. Please file an issue if you believe you have one. |
| 152 |
size_t idx = static_cast<size_t>(index); |
| 153 |
GPR_CODEGEN_ASSERT( |
| 154 |
methods_[idx].get() != nullptr && |
| 155 |
"Cannot mark the method as 'async' because it has already been " |
| 156 |
"marked as 'generic'."); |
| 157 |
methods_[idx]->SetServerApiType(internal::RpcServiceMethod::ApiType::ASYNC); |
| 158 |
} |
| 159 |
|
| 160 |
void MarkMethodRaw(int index) { |
| 161 |
// This does not have to be a hard error, however no one has approached us |
| 162 |
// with a use case yet. Please file an issue if you believe you have one. |
| 163 |
size_t idx = static_cast<size_t>(index); |
| 164 |
GPR_CODEGEN_ASSERT(methods_[idx].get() != nullptr && |
| 165 |
"Cannot mark the method as 'raw' because it has already " |
| 166 |
"been marked as 'generic'."); |
| 167 |
methods_[idx]->SetServerApiType(internal::RpcServiceMethod::ApiType::RAW); |
| 168 |
} |
| 169 |
|
| 170 |
void MarkMethodGeneric(int index) { |
| 171 |
// This does not have to be a hard error, however no one has approached us |
| 172 |
// with a use case yet. Please file an issue if you believe you have one. |
| 173 |
size_t idx = static_cast<size_t>(index); |
| 174 |
GPR_CODEGEN_ASSERT( |
| 175 |
methods_[idx]->handler() != nullptr && |
| 176 |
"Cannot mark the method as 'generic' because it has already been " |
| 177 |
"marked as 'async' or 'raw'."); |
| 178 |
methods_[idx].reset(); |
| 179 |
} |
| 180 |
|
| 181 |
void MarkMethodStreamed(int index, internal::MethodHandler* streamed_method) { |
| 182 |
// This does not have to be a hard error, however no one has approached us |
| 183 |
// with a use case yet. Please file an issue if you believe you have one. |
| 184 |
size_t idx = static_cast<size_t>(index); |
| 185 |
GPR_CODEGEN_ASSERT(methods_[idx] && methods_[idx]->handler() && |
| 186 |
"Cannot mark an async or generic method Streamed"); |
| 187 |
methods_[idx]->SetHandler(streamed_method); |
| 188 |
|
| 189 |
// From the server's point of view, streamed unary is a special |
| 190 |
// case of BIDI_STREAMING that has 1 read and 1 write, in that order, |
| 191 |
// and split server-side streaming is BIDI_STREAMING with 1 read and |
| 192 |
// any number of writes, in that order. |
| 193 |
methods_[idx]->SetMethodType(internal::RpcMethod::BIDI_STREAMING); |
| 194 |
} |
| 195 |
|
| 196 |
void MarkMethodCallback(int index, internal::MethodHandler* handler) { |
| 197 |
// This does not have to be a hard error, however no one has approached us |
| 198 |
// with a use case yet. Please file an issue if you believe you have one. |
| 199 |
size_t idx = static_cast<size_t>(index); |
| 200 |
GPR_CODEGEN_ASSERT( |
| 201 |
methods_[idx].get() != nullptr && |
| 202 |
"Cannot mark the method as 'callback' because it has already been " |
| 203 |
"marked as 'generic'."); |
| 204 |
methods_[idx]->SetHandler(handler); |
| 205 |
methods_[idx]->SetServerApiType( |
| 206 |
internal::RpcServiceMethod::ApiType::CALL_BACK); |
| 207 |
} |
| 208 |
|
| 209 |
void MarkMethodRawCallback(int index, internal::MethodHandler* handler) { |
| 210 |
// This does not have to be a hard error, however no one has approached us |
| 211 |
// with a use case yet. Please file an issue if you believe you have one. |
| 212 |
size_t idx = static_cast<size_t>(index); |
| 213 |
GPR_CODEGEN_ASSERT( |
| 214 |
methods_[idx].get() != nullptr && |
| 215 |
"Cannot mark the method as 'raw callback' because it has already " |
| 216 |
"been marked as 'generic'."); |
| 217 |
methods_[idx]->SetHandler(handler); |
| 218 |
methods_[idx]->SetServerApiType( |
| 219 |
internal::RpcServiceMethod::ApiType::RAW_CALL_BACK); |
| 220 |
} |
| 221 |
|
| 222 |
internal::MethodHandler* GetHandler(int index) { |
| 223 |
size_t idx = static_cast<size_t>(index); |
| 224 |
return methods_[idx]->handler(); |
| 225 |
} |
| 226 |
|
| 227 |
private: |
| 228 |
friend class Server; |
| 229 |
friend class ServerInterface; |
| 230 |
ServerInterface* server_; |
| 231 |
std::vector<std::unique_ptr<internal::RpcServiceMethod>> methods_; |
| 232 |
}; |
| 233 |
|
| 234 |
} // namespace grpc |
| 235 |
|
| 236 |
#endif // GRPCPP_IMPL_SERVICE_TYPE_H |