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_transport.hpp
1 //
2 // Created by Benjamin Schulz on 15/03/15.
3 //
4 
5 #ifndef _THRIFT_ASIO_THRIFT_ASIO_CLIENT_TRANSPORT_HPP_
6 #define _THRIFT_ASIO_THRIFT_ASIO_CLIENT_TRANSPORT_HPP_
7 
8 #pragma once
9 
10 #include "./thrift_asio_transport.hpp"
11 #include <boost/asio/connect.hpp>
12 
13 namespace betabugs {
14 namespace networking {
15 
21 {
22  public:
25  boost::asio::io_service& io_service,
26  const std::string& host_name,
27  const std::string& service_name,
29  ) : thrift_asio_transport(std::make_shared<boost::asio::ip::tcp::socket>(io_service), event_handlers)
30  , host_name_(host_name)
31  , service_name_(service_name)
32  , resolver_(io_service)
33  {
34  }
35 
42  virtual void open() override
43  {
44  using boost::asio::ip::tcp;
45  //this->close();
46  state_ = RESOLVING;
47  resolver_.cancel();
48  resolver_.async_resolve
49  (
50  {host_name_, service_name_},
51  [this]
52  (const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::iterator iterator)
53  {
54  if (ec)
55  {
57  close();
58  }
59  else
60  {
62  boost::asio::async_connect
63  (
64  *socket_,
65  iterator,
66  [this]
67  (boost::system::error_code ec, tcp::resolver::iterator)
68  {
69  if (ec)
70  {
72  this->close();
73  }
74  else
75  {
77  }
78  }
79  );
80  }
81  }
82  );
83  }
84 
85 
87  void connect_to(const std::string& host_name, const std::string& service_name)
88  {
89  close();
90  this->host_name_ = host_name;
91  this->service_name_ = service_name;
92  open();
93  }
94 
95  private:
96  std::string host_name_;
97  std::string service_name_;
98  boost::asio::ip::tcp::resolver resolver_;
99 };
100 
101 }
102 }
103 
104 #endif //_THRIFT_ASIO_THRIFT_ASIO_CLIENT_TRANSPORT_HPP_
virtual void open() override
Definition: thrift_asio_client_transport.hpp:42
Definition: thrift_asio_transport.hpp:32
virtual void on_error(const boost::system::error_code &ec)
Gets invoked when an error occurred while communication over the transport.
Definition: thrift_asio_transport.hpp:35
thrift_asio_client_transport(boost::asio::io_service &io_service, const std::string &host_name, const std::string &service_name, event_handlers *event_handlers)
creates a thrift_asio_client_transport and tries to connect to host_name:service_name ...
Definition: thrift_asio_client_transport.hpp:24
void connect_to(const std::string &host_name, const std::string &service_name)
close the current connection and connect to host_name::service_name
Definition: thrift_asio_client_transport.hpp:87
virtual void close() override
closes the transport
Definition: thrift_asio_transport.hpp:183
virtual void open() override
opens the transport
Definition: thrift_asio_transport.hpp:163
we're currently trying to resolve host_name:service_name
Definition: thrift_asio_transport.hpp:218
event_handlers * event_handlers_
handles events like on_error, etc.
Definition: thrift_asio_transport.hpp:224
socket_ptr socket_
the underlying socket
Definition: thrift_asio_transport.hpp:223
State state_
the state of this transport
Definition: thrift_asio_transport.hpp:222
Definition: thrift_asio_transport.hpp:26
Definition: thrift_asio_client_transport.hpp:20
the transport is currently connecting
Definition: thrift_asio_transport.hpp:217