1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef _THRIFT_TEVHTTP_CLIENT_CHANNEL_H_
21 #define _THRIFT_TEVHTTP_CLIENT_CHANNEL_H_ 1
22 
23 #include <queue>
24 #include <string>
25 #include <utility>
26 #include <memory>
27 #include <thrift/async/TAsyncChannel.h>
28 
29 struct event_base;
30 struct evdns_base;
31 struct evhttp_connection;
32 struct evhttp_request;
33 
34 namespace apache {
35 namespace thrift {
36 namespace transport {
37 class TMemoryBuffer;
38 }
39 }
40 }
41 
42 namespace apache {
43 namespace thrift {
44 namespace async {
45 
46 class TEvhttpClientChannel : public TAsyncChannel {
47 public:
48   using TAsyncChannel::VoidCallback;
49 
50   TEvhttpClientChannel(const std::string& host,
51                        const std::string& path,
52                        const char* address,
53                        int port,
54                        struct event_base* eb,
55                        struct evdns_base *dnsbase = nullptr);
56   ~TEvhttpClientChannel() override;
57 
58   void sendAndRecvMessage(const VoidCallback& cob,
59                                   apache::thrift::transport::TMemoryBuffer* sendBuf,
60                                   apache::thrift::transport::TMemoryBuffer* recvBuf) override;
61 
62   void sendMessage(const VoidCallback& cob,
63                            apache::thrift::transport::TMemoryBuffer* message) override;
64   void recvMessage(const VoidCallback& cob,
65                            apache::thrift::transport::TMemoryBuffer* message) override;
66 
67   void finish(struct evhttp_request* req);
68 
69   // XXX
good()70   bool good() const override { return true; }
error()71   bool error() const override { return false; }
timedOut()72   bool timedOut() const override { return false; }
73 
74 private:
75   static void response(struct evhttp_request* req, void* arg);
76 
77   std::string host_;
78   std::string path_;
79   typedef std::pair<VoidCallback, apache::thrift::transport::TMemoryBuffer*> Completion;
80   typedef std::queue<Completion> CompletionQueue;
81   CompletionQueue completionQueue_;
82   struct evhttp_connection* conn_;
83 };
84 }
85 }
86 } // apache::thrift::async
87 
88 #endif // #ifndef _THRIFT_TEVHTTP_CLIENT_CHANNEL_H_
89