View on GitHub

Thrift Asio

Asynchronous client and server for apache thrift implemented via boost::asio

Download this project as a .zip file Download this project as a tar.gz file
thrift_asio_client.hpp
1 //
2 // Created by Benjamin Schulz on 15/03/15.
3 //
4 
5 #ifndef _THRIFT_ASIO_CLIENT_HPP_
6 #define _THRIFT_ASIO_CLIENT_HPP_
7 
8 #pragma once
9 
10 #include <boost/smart_ptr/enable_shared_from_raw.hpp>
11 #include "./thrift_asio_client_transport.hpp"
12 #include <thrift/protocol/TBinaryProtocol.h>
13 #include <thrift/transport/TBufferTransports.h>
14 
15 namespace betabugs {
16 namespace networking {
17 
19 
26 template<
27  typename ClientType,
28  typename ProcessorType,
29  typename HandlerInterfaceType
30 >
32  : public HandlerInterfaceType
33  , public boost::enable_shared_from_raw
34  , public thrift_asio_client_transport::event_handlers
35 {
36  public:
39  boost::asio::io_service& io_service,
40  const std::string& host_name,
41  const std::string& service_name
42  )
43  : io_service_(io_service)
44  , processor_(boost::shared_from_raw(this))
45  , transport_(boost::make_shared<thrift_asio_client_transport>(
46  io_service, host_name, service_name, this
47  ))
48  , protocol_
49  (
50  boost::make_shared<apache::thrift::protocol::TBinaryProtocol>(
51  boost::make_shared<apache::thrift::transport::TFramedTransport>(
52  transport_
53  )
54  )
55  )
56  , client_(protocol_)
57  {
58  protocol_->getTransport()->open();
59  }
60 
62  void update()
63  {
64  while (transport_->isOpen() && transport_->peek())
65  {
66  processor_.process(protocol_, nullptr, nullptr);
67  }
68  }
69 
71  void connect_to(const std::string& host_name, const std::string service_name)
72  {
73  transport_->connect_to(host_name, service_name);
74  }
75 
77  void reconnect_in(const boost::posix_time::time_duration& duration)
78  {
79  auto timer = std::make_shared<boost::asio::deadline_timer>(io_service_);
80  timer->expires_from_now(duration);
81  timer->async_wait(
82  [timer, this](const boost::system::error_code& ec)
83  {
84  if (ec) on_error(ec);
85  else transport_->open();
86  }
87  );
88  }
89 
90  private:
91  boost::asio::io_service& io_service_;
92  ProcessorType processor_;
93 
94  boost::shared_ptr<thrift_asio_client_transport> transport_;
95  boost::shared_ptr<apache::thrift::protocol::TProtocol> protocol_;
96  protected:
97  ClientType client_;
98 };
99 
104 }
105 }
106 
107 #endif //_THRIFT_ASIO_CLIENT_HPP_
void update()
process incoming traffic
Definition: thrift_asio_client.hpp:62
thrift_asio_client(boost::asio::io_service &io_service, const std::string &host_name, const std::string &service_name)
creates a thrift_asio_client and tries to connect to host_name:service_name
Definition: thrift_asio_client.hpp:38
void reconnect_in(const boost::posix_time::time_duration &duration)
reconnect in seconds seconds
Definition: thrift_asio_client.hpp:77
An asynchronous bidirectional thrift client.
Definition: thrift_asio_client.hpp:31
void connect_to(const std::string &host_name, const std::string service_name)
close the connection and connect to host_name:service_name
Definition: thrift_asio_client.hpp:71
ClientType client_
the client used to communicate with the server
Definition: thrift_asio_client.hpp:97
Definition: thrift_asio_client_transport.hpp:20