27 #ifndef _ASYNC_CLIENT_H_
28 #define _ASYNC_CLIENT_H_
39 #include <boost/asio.hpp>
40 #include <boost/bind.hpp>
41 #include <boost/date_time/posix_time/posix_time.hpp>
42 #include <boost/signal.hpp>
43 #include <boost/thread/thread.hpp>
45 using boost::asio::ip::tcp;
63 AsyncClient(boost::asio::io_service& io_service,
const std::string& server,
const std::string& port,std::string delimiter=std::string(
"\n")):
64 delimiter_(delimiter),
67 query_(server_ip_,server_port_),
68 resolver_(io_service),
71 attempting_connection_=
false;
73 error_ = boost::asio::error::not_connected;
96 if(attempting_connection_)
102 }
catch(std::exception& e)
107 attempting_connection_=
true;
114 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
117 resolver_.async_resolve(query_,boost::bind(&
AsyncClient::handleResolve,
this,boost::asio::placeholders::error,boost::asio::placeholders::iterator));
127 return is_connected_;
136 error_ = boost::asio::error::not_connected;
137 is_connected_ =
false;
159 throw boost::system::system_error(boost::asio::error::not_connected);
161 std::ostream request_stream(&request_);
162 request_stream << message;
163 boost::asio::async_write(socket_,request_,boost::bind(&
AsyncClient::writeRequestHandler,
this,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
194 attempting_connection_=
false;
214 void handleResolve(
const boost::system::error_code& err,tcp::resolver::iterator endpoint_iterator)
224 attempting_connection_=
false;
235 tcp::endpoint endpoint = *endpoint_iterator;
238 socket_.async_connect(endpoint,boost::bind(&
AsyncClient::handleConnect,
this,boost::asio::placeholders::error, ++endpoint_iterator));
250 void handleConnect(
const boost::system::error_code& err,tcp::resolver::iterator endpoint_iterator)
268 attempting_connection_=
false;
273 else if (endpoint_iterator != tcp::resolver::iterator())
281 tcp::endpoint endpoint = *endpoint_iterator;
283 socket_.async_connect(endpoint,boost::bind(&
AsyncClient::handleConnect,
this,boost::asio::placeholders::error, ++endpoint_iterator));
292 attempting_connection_=
false;
297 if(error_ == boost::asio::error::operation_aborted || error_ == boost::asio::error::already_started)
298 error_=boost::asio::error::host_unreachable;
314 boost::asio::async_read_until(socket_,response_,delimiter_,boost::bind(&
AsyncClient::handleRead,
this,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
327 void handleRead(
const boost::system::error_code& err, std::size_t bytes_transferred)
338 attempting_connection_=
false;
347 std::istream response_stream(&response_);
351 std::getline(response_stream, line);
bool attempting_connection_
Ongoing reconnection.
tcp::resolver resolver_
Connection resolver, the resolver obtains the endpoints for the connection.
void handleRead(const boost::system::error_code &err, std::size_t bytes_transferred)
New data handler.
void startRead(void)
Start a read asynchronous operation.
std::string server_ip_
Ip of the server.
void reconnect(void)
Function to reestablish the connection.
void handleConnect(const boost::system::error_code &err, tcp::resolver::iterator endpoint_iterator)
Handle a connection.
bool isConnected(void)
Get the connection status.
AsyncClient(boost::asio::io_service &io_service, const std::string &server, const std::string &port, std::string delimiter=std::string("\n"))
Class constructor.
boost::system::error_code error_
Current error code of the communication.
bool is_connected_
Connection status variable.
void handleResolve(const boost::system::error_code &err, tcp::resolver::iterator endpoint_iterator)
Query resolve handler.
std::string delimiter_
String used as delimiter in the read until function, default is \n.
void write(std::string message)
Send a message to the server.
boost::asio::streambuf request_
Stream buffer to hold the communications requests.
tcp::resolver::query query_
Start an asynchronous resolve to translate the server and service names into a list of endpoints...
boost::signal< void(void)> writeHandler
Successful writing handler.
boost::signal< void(std::string)> readHandler
New data function handler.
std::string server_port_
Port of the server.
void startConnection()
Starts a query on the ip and port and starts the asynchronous resolver.
boost::signal< void(void)> connectHandler
Successful connection establish handler.
tcp::socket socket_
Connection socket variable.
void writeRequestHandler(const boost::system::error_code &err, std::size_t bytes_transferred)
Write handler.
Asynchronous tpc/ip communication object.
void close(void)
Close the connection.
~AsyncClient()
Class destructor.
boost::asio::streambuf response_
Stream buffer to hold the communications responses.