Asynchronous tpc/ip communication object. More...
#include <AsyncClient.h>
Public Member Functions | |
AsyncClient (boost::asio::io_service &io_service, const std::string &server, const std::string &port, std::string delimiter=std::string("\n")) | |
Class constructor. | |
void | close (void) |
Close the connection. | |
bool | isConnected (void) |
Get the connection status. | |
void | reconnect (void) |
Function to reestablish the connection. | |
void | startConnection () |
Starts a query on the ip and port and starts the asynchronous resolver. | |
void | write (std::string message) |
Send a message to the server. | |
~AsyncClient () | |
Class destructor. | |
Public Attributes | |
boost::signal< void(void)> | connectHandler |
Successful connection establish handler. | |
boost::system::error_code | error_ |
Current error code of the communication. | |
boost::signal< void(std::string)> | readHandler |
New data function handler. | |
boost::signal< void(void)> | writeHandler |
Successful writing handler. | |
Private Member Functions | |
void | handleConnect (const boost::system::error_code &err, tcp::resolver::iterator endpoint_iterator) |
Handle a connection. | |
void | handleRead (const boost::system::error_code &err, std::size_t bytes_transferred) |
New data handler. | |
void | handleResolve (const boost::system::error_code &err, tcp::resolver::iterator endpoint_iterator) |
Query resolve handler. | |
void | startRead (void) |
Start a read asynchronous operation. | |
void | writeRequestHandler (const boost::system::error_code &err, std::size_t bytes_transferred) |
Write handler. | |
Private Attributes | |
bool | attempting_connection_ |
Ongoing reconnection. | |
std::string | delimiter_ |
String used as delimiter in the read until function, default is \n. | |
bool | is_connected_ |
Connection status variable. | |
tcp::resolver::query | query_ |
Start an asynchronous resolve to translate the server and service names into a list of endpoints. | |
boost::asio::streambuf | request_ |
Stream buffer to hold the communications requests. | |
tcp::resolver | resolver_ |
Connection resolver, the resolver obtains the endpoints for the connection. | |
boost::asio::streambuf | response_ |
Stream buffer to hold the communications responses. | |
std::string | server_ip_ |
Ip of the server. | |
std::string | server_port_ |
Port of the server. | |
tcp::socket | socket_ |
Connection socket variable. |
Asynchronous tpc/ip communication object.
This class preforms asynchronous communication with a tcp/ip socket object.
Definition at line 54 of file AsyncClient.h.
AsyncClient::AsyncClient | ( | boost::asio::io_service & | io_service, | |
const std::string & | server, | |||
const std::string & | port, | |||
std::string | delimiter = std::string("\n") | |||
) | [inline] |
Class constructor.
The constructor initializes the server ip and port, the resolver and also the socket. It also calls the startConnection function which proceeds with establishing a connection to the server.
Definition at line 63 of file AsyncClient.h.
AsyncClient::~AsyncClient | ( | ) | [inline] |
Class destructor.
This function only closes the socket communication.
Definition at line 83 of file AsyncClient.h.
void AsyncClient::close | ( | void | ) | [inline] |
Close the connection.
Definition at line 133 of file AsyncClient.h.
void AsyncClient::handleConnect | ( | const boost::system::error_code & | err, | |
tcp::resolver::iterator | endpoint_iterator | |||
) | [inline, private] |
Handle a connection.
err | error status | |
endpoint_iterator | iterator to the endpoints list |
This function is called after an attempt to connect to a endpoint. If the connection was successful (err==false) the function startRead is used to schedule a read operation. If the connection was unsuccessful, if the endpoint is not the last in the list a new connection is attempt on the next endpoint, if there are no more endpoints in the list we start a new connection from the beginning.
Definition at line 250 of file AsyncClient.h.
void AsyncClient::handleRead | ( | const boost::system::error_code & | err, | |
std::size_t | bytes_transferred | |||
) | [inline, private] |
New data handler.
err | error status | |
bytes_transferred | number of bytes received |
This function is called upon a read attempt. If there was an error this function schedules a new connection attempt. On a successful read operation, the data is copied to a string and the user read handler is called. The function finalizes by scheduling a new read operation.
Definition at line 327 of file AsyncClient.h.
void AsyncClient::handleResolve | ( | const boost::system::error_code & | err, | |
tcp::resolver::iterator | endpoint_iterator | |||
) | [inline, private] |
Query resolve handler.
err | error status | |
endpoint_iterator | current iterator to the list of endpoints |
This function is called when resolving the endpoints tying to establish a connection. If there is a error on the resolver a new connection is attempt. If there is no error a connection is attempt on the endpoint.
Definition at line 214 of file AsyncClient.h.
bool AsyncClient::isConnected | ( | void | ) | [inline] |
Get the connection status.
Definition at line 125 of file AsyncClient.h.
void AsyncClient::reconnect | ( | void | ) | [inline] |
Function to reestablish the connection.
Definition at line 143 of file AsyncClient.h.
void AsyncClient::startConnection | ( | ) | [inline] |
Starts a query on the ip and port and starts the asynchronous resolver.
This function stats the query of the ip and port and the asynchronous resolver. The resolver will try to establish a connection to the endpoints returned by the query.
Definition at line 94 of file AsyncClient.h.
void AsyncClient::startRead | ( | void | ) | [inline, private] |
Start a read asynchronous operation.
This function schedules a asynchronous read operation till a new line character appears. The function handleRead is scheduled to be called upon receiving the new data
Definition at line 311 of file AsyncClient.h.
void AsyncClient::write | ( | std::string | message | ) | [inline] |
Send a message to the server.
message | std::string containing the outgoing message |
This function converts the std::string to a boost::asio::streambuf and stats a asynchronous write request with that buffer. If the connection is not established a boost::asio::error::not_connected error is thrown (it can be caught using a std::exception).
Definition at line 156 of file AsyncClient.h.
void AsyncClient::writeRequestHandler | ( | const boost::system::error_code & | err, | |
std::size_t | bytes_transferred | |||
) | [inline, private] |
Write handler.
err | status of the request | |
bytes_transferred | number of bytes transfered |
This function is called upon asynchronous write of data to the socket. The user may preform a specific action after by setting the writeHandler. The user function will only be called on a successful write.
Definition at line 185 of file AsyncClient.h.
bool AsyncClient::attempting_connection_ [private] |
Ongoing reconnection.
Definition at line 367 of file AsyncClient.h.
boost::signal<void (void)> AsyncClient::connectHandler |
Successful connection establish handler.
Definition at line 171 of file AsyncClient.h.
std::string AsyncClient::delimiter_ [private] |
String used as delimiter in the read until function, default is \n.
Definition at line 361 of file AsyncClient.h.
boost::system::error_code AsyncClient::error_ |
Current error code of the communication.
Definition at line 173 of file AsyncClient.h.
bool AsyncClient::is_connected_ [private] |
Connection status variable.
Definition at line 364 of file AsyncClient.h.
tcp::resolver::query AsyncClient::query_ [private] |
Start an asynchronous resolve to translate the server and service names into a list of endpoints.
Definition at line 375 of file AsyncClient.h.
boost::signal<void (std::string)> AsyncClient::readHandler |
New data function handler.
Definition at line 167 of file AsyncClient.h.
boost::asio::streambuf AsyncClient::request_ [private] |
Stream buffer to hold the communications requests.
Definition at line 382 of file AsyncClient.h.
tcp::resolver AsyncClient::resolver_ [private] |
Connection resolver, the resolver obtains the endpoints for the connection.
Definition at line 378 of file AsyncClient.h.
boost::asio::streambuf AsyncClient::response_ [private] |
Stream buffer to hold the communications responses.
Definition at line 384 of file AsyncClient.h.
std::string AsyncClient::server_ip_ [private] |
Ip of the server.
Definition at line 370 of file AsyncClient.h.
std::string AsyncClient::server_port_ [private] |
Port of the server.
Definition at line 372 of file AsyncClient.h.
tcp::socket AsyncClient::socket_ [private] |
Connection socket variable.
Definition at line 380 of file AsyncClient.h.
boost::signal<void (void)> AsyncClient::writeHandler |
Successful writing handler.
Definition at line 169 of file AsyncClient.h.