| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2018 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_GENERIC_ASYNC_GENERIC_SERVICE_H |
| 20 |
#define GRPCPP_GENERIC_ASYNC_GENERIC_SERVICE_H |
| 21 |
|
| 22 |
#include <grpc/impl/codegen/port_platform.h> |
| 23 |
|
| 24 |
#include <grpcpp/impl/codegen/server_callback_handlers.h> |
| 25 |
#include <grpcpp/support/async_stream.h> |
| 26 |
#include <grpcpp/support/byte_buffer.h> |
| 27 |
#include <grpcpp/support/server_callback.h> |
| 28 |
|
| 29 |
struct grpc_server; |
| 30 |
|
| 31 |
namespace grpc { |
| 32 |
|
| 33 |
typedef ServerAsyncReaderWriter<ByteBuffer, ByteBuffer> |
| 34 |
GenericServerAsyncReaderWriter; |
| 35 |
typedef ServerAsyncResponseWriter<ByteBuffer> GenericServerAsyncResponseWriter; |
| 36 |
typedef ServerAsyncReader<ByteBuffer, ByteBuffer> GenericServerAsyncReader; |
| 37 |
typedef ServerAsyncWriter<ByteBuffer> GenericServerAsyncWriter; |
| 38 |
|
| 39 |
class GenericServerContext final : public ServerContext { |
| 40 |
public: |
| 41 |
const std::string& method() const { return method_; } |
| 42 |
const std::string& host() const { return host_; } |
| 43 |
|
| 44 |
private: |
| 45 |
friend class ServerInterface; |
| 46 |
|
| 47 |
std::string method_; |
| 48 |
std::string host_; |
| 49 |
}; |
| 50 |
|
| 51 |
// A generic service at the server side accepts all RPC methods and hosts. It is |
| 52 |
// typically used in proxies. The generic service can be registered to a server |
| 53 |
// which also has other services. |
| 54 |
// Sample usage: |
| 55 |
// ServerBuilder builder; |
| 56 |
// auto cq = builder.AddCompletionQueue(); |
| 57 |
// AsyncGenericService generic_service; |
| 58 |
// builder.RegisterAsyncGenericService(&generic_service); |
| 59 |
// auto server = builder.BuildAndStart(); |
| 60 |
// |
| 61 |
// // request a new call |
| 62 |
// GenericServerContext context; |
| 63 |
// GenericServerAsyncReaderWriter stream; |
| 64 |
// generic_service.RequestCall(&context, &stream, cq.get(), cq.get(), tag); |
| 65 |
// |
| 66 |
// When tag is retrieved from cq->Next(), context.method() can be used to look |
| 67 |
// at the method and the RPC can be handled accordingly. |
| 68 |
class AsyncGenericService final { |
| 69 |
public: |
| 70 |
AsyncGenericService() : server_(nullptr) {} |
| 71 |
|
| 72 |
void RequestCall(GenericServerContext* ctx, |
| 73 |
GenericServerAsyncReaderWriter* reader_writer, |
| 74 |
grpc::CompletionQueue* call_cq, |
| 75 |
grpc::ServerCompletionQueue* notification_cq, void* tag); |
| 76 |
|
| 77 |
private: |
| 78 |
friend class grpc::Server; |
| 79 |
grpc::Server* server_; |
| 80 |
}; |
| 81 |
|
| 82 |
/// \a ServerGenericBidiReactor is the reactor class for bidi streaming RPCs |
| 83 |
/// invoked on a CallbackGenericService. It is just a ServerBidi reactor with |
| 84 |
/// ByteBuffer arguments. |
| 85 |
using ServerGenericBidiReactor = ServerBidiReactor<ByteBuffer, ByteBuffer>; |
| 86 |
|
| 87 |
class GenericCallbackServerContext final : public grpc::CallbackServerContext { |
| 88 |
public: |
| 89 |
const std::string& method() const { return method_; } |
| 90 |
const std::string& host() const { return host_; } |
| 91 |
|
| 92 |
private: |
| 93 |
friend class grpc::Server; |
| 94 |
|
| 95 |
std::string method_; |
| 96 |
std::string host_; |
| 97 |
}; |
| 98 |
|
| 99 |
/// \a CallbackGenericService is the base class for generic services implemented |
| 100 |
/// using the callback API and registered through the ServerBuilder using |
| 101 |
/// RegisterCallbackGenericService. |
| 102 |
class CallbackGenericService { |
| 103 |
public: |
| 104 |
CallbackGenericService() {} |
| 105 |
virtual ~CallbackGenericService() {} |
| 106 |
|
| 107 |
/// The "method handler" for the generic API. This function should be |
| 108 |
/// overridden to provide a ServerGenericBidiReactor that implements the |
| 109 |
/// application-level interface for this RPC. Unimplemented by default. |
| 110 |
virtual ServerGenericBidiReactor* CreateReactor( |
| 111 |
GenericCallbackServerContext* /*ctx*/) { |
| 112 |
class Reactor : public ServerGenericBidiReactor { |
| 113 |
public: |
| 114 |
Reactor() { this->Finish(Status(StatusCode::UNIMPLEMENTED, "")); } |
| 115 |
void OnDone() override { delete this; } |
| 116 |
}; |
| 117 |
return new Reactor; |
| 118 |
} |
| 119 |
|
| 120 |
private: |
| 121 |
friend class grpc::Server; |
| 122 |
|
| 123 |
internal::CallbackBidiHandler<ByteBuffer, ByteBuffer>* Handler() { |
| 124 |
return new internal::CallbackBidiHandler<ByteBuffer, ByteBuffer>( |
| 125 |
[this](grpc::CallbackServerContext* ctx) { |
| 126 |
return CreateReactor(static_cast<GenericCallbackServerContext*>(ctx)); |
| 127 |
}); |
| 128 |
} |
| 129 |
|
| 130 |
grpc::Server* server_{nullptr}; |
| 131 |
}; |
| 132 |
|
| 133 |
} // namespace grpc |
| 134 |
|
| 135 |
#endif // GRPCPP_GENERIC_ASYNC_GENERIC_SERVICE_H |