Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
AsyncClient Class Reference

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. More...
 
void close (void)
 Close the connection. More...
 
bool isConnected (void)
 Get the connection status. More...
 
void reconnect (void)
 Function to reestablish the connection. More...
 
void startConnection ()
 Starts a query on the ip and port and starts the asynchronous resolver. More...
 
void write (std::string message)
 Send a message to the server. More...
 
 ~AsyncClient ()
 Class destructor. More...
 

Public Attributes

boost::signal< void(void)> connectHandler
 Successful connection establish handler. More...
 
boost::system::error_code error_
 Current error code of the communication. More...
 
boost::signal< void(std::string)> readHandler
 New data function handler. More...
 
boost::signal< void(void)> writeHandler
 Successful writing handler. More...
 

Private Member Functions

void handleConnect (const boost::system::error_code &err, tcp::resolver::iterator endpoint_iterator)
 Handle a connection. More...
 
void handleRead (const boost::system::error_code &err, std::size_t bytes_transferred)
 New data handler. More...
 
void handleResolve (const boost::system::error_code &err, tcp::resolver::iterator endpoint_iterator)
 Query resolve handler. More...
 
void startRead (void)
 Start a read asynchronous operation. More...
 
void writeRequestHandler (const boost::system::error_code &err, std::size_t bytes_transferred)
 Write handler. More...
 

Private Attributes

bool attempting_connection_
 Ongoing reconnection. More...
 
std::string delimiter_
 String used as delimiter in the read until function, default is \n. More...
 
bool is_connected_
 Connection status variable. More...
 
tcp::resolver::query query_
 Start an asynchronous resolve to translate the server and service names into a list of endpoints. More...
 
boost::asio::streambuf request_
 Stream buffer to hold the communications requests. More...
 
tcp::resolver resolver_
 Connection resolver, the resolver obtains the endpoints for the connection. More...
 
boost::asio::streambuf response_
 Stream buffer to hold the communications responses. More...
 
std::string server_ip_
 Ip of the server. More...
 
std::string server_port_
 Port of the server. More...
 
tcp::socket socket_
 Connection socket variable. More...
 

Detailed Description

Asynchronous tpc/ip communication object.

This class preforms asynchronous communication with a tcp/ip socket object.

Definition at line 54 of file AsyncClient.h.

Constructor & Destructor Documentation

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.

Member Function Documentation

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 
)
inlineprivate

Handle a connection.

Parameters
errerror status
endpoint_iteratoriterator 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 
)
inlineprivate

New data handler.

Parameters
errerror status
bytes_transferrednumber 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 
)
inlineprivate

Query resolve handler.

Parameters
errerror status
endpoint_iteratorcurrent 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.

Returns
true if the connection is established, false if not

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  )
inlineprivate

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.

Parameters
messagestd::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 
)
inlineprivate

Write handler.

Parameters
errstatus of the request
bytes_transferrednumber 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.

Member Data Documentation

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.


The documentation for this class was generated from the following file:


tcp_client
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:53