• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/support/port_platform.h>
23 #include <grpcpp/generic/callback_generic_service.h>
24 #include <grpcpp/support/async_stream.h>
25 #include <grpcpp/support/byte_buffer.h>
26 
27 struct grpc_server;
28 
29 namespace grpc {
30 
31 typedef ServerAsyncReaderWriter<ByteBuffer, ByteBuffer>
32     GenericServerAsyncReaderWriter;
33 typedef ServerAsyncResponseWriter<ByteBuffer> GenericServerAsyncResponseWriter;
34 typedef ServerAsyncReader<ByteBuffer, ByteBuffer> GenericServerAsyncReader;
35 typedef ServerAsyncWriter<ByteBuffer> GenericServerAsyncWriter;
36 
37 class GenericServerContext final : public ServerContext {
38  public:
method()39   const std::string& method() const { return method_; }
host()40   const std::string& host() const { return host_; }
41 
42  private:
43   friend class ServerInterface;
44 
45   std::string method_;
46   std::string host_;
47 };
48 
49 // A generic service at the server side accepts all RPC methods and hosts. It is
50 // typically used in proxies. The generic service can be registered to a server
51 // which also has other services.
52 // Sample usage:
53 //   ServerBuilder builder;
54 //   auto cq = builder.AddCompletionQueue();
55 //   AsyncGenericService generic_service;
56 //   builder.RegisterAsyncGenericService(&generic_service);
57 //   auto server = builder.BuildAndStart();
58 //
59 //   // request a new call
60 //   GenericServerContext context;
61 //   GenericServerAsyncReaderWriter stream;
62 //   generic_service.RequestCall(&context, &stream, cq.get(), cq.get(), tag);
63 //
64 // When tag is retrieved from cq->Next(), context.method() can be used to look
65 // at the method and the RPC can be handled accordingly.
66 class AsyncGenericService final {
67  public:
AsyncGenericService()68   AsyncGenericService() : server_(nullptr) {}
69 
70   void RequestCall(GenericServerContext* ctx,
71                    GenericServerAsyncReaderWriter* reader_writer,
72                    grpc::CompletionQueue* call_cq,
73                    grpc::ServerCompletionQueue* notification_cq, void* tag);
74 
75  private:
76   friend class grpc::Server;
77   grpc::Server* server_;
78 };
79 
80 }  // namespace grpc
81 
82 #endif  // GRPCPP_GENERIC_ASYNC_GENERIC_SERVICE_H
83