1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <thrift/lib/cpp/EventHandlerBase.h>
20 #include <thrift/lib/cpp2/async/HeaderChannel.h>
21 #include <thrift/lib/cpp2/async/Interaction.h>
22 #include <thrift/lib/cpp2/async/RequestChannel.h>
23 
24 namespace apache {
25 namespace thrift {
26 
27 class GeneratedAsyncClient : public TClientBase {
28  public:
29   using channel_ptr =
30       std::unique_ptr<RequestChannel, folly::DelayedDestruction::Destructor>;
31 
32   GeneratedAsyncClient(std::shared_ptr<RequestChannel> channel);
33 
34   virtual char const* getServiceName() const noexcept = 0;
35 
getChannel()36   RequestChannel* getChannel() const noexcept { return channel_.get(); }
37 
getChannelShared()38   std::shared_ptr<RequestChannel> getChannelShared() const noexcept {
39     return channel_;
40   }
41 
getHeaderChannel()42   HeaderChannel* getHeaderChannel() const noexcept {
43     return dynamic_cast<HeaderChannel*>(channel_.get());
44   }
45 
46  protected:
47   std::shared_ptr<RequestChannel> channel_;
48 };
49 
50 class InteractionHandle : public GeneratedAsyncClient {
51  public:
52   InteractionHandle(
53       std::shared_ptr<RequestChannel> channel, folly::StringPiece methodName);
54   InteractionHandle(std::shared_ptr<RequestChannel> channel, InteractionId id);
55   ~InteractionHandle();
56   InteractionHandle(InteractionHandle&&) noexcept = default;
57   InteractionHandle& operator=(InteractionHandle&&);
58 
59   const InteractionId& getInteractionId();
60 
61  protected:
62   void setInteraction(RpcOptions& rpcOptions);
63 
64  private:
65   void terminate();
66 
67  protected:
68   InteractionId interactionId_;
69 };
70 
71 } // namespace thrift
72 } // namespace apache
73