Manages a TCP/IP connection between two entities where one is a server and the other one is a client.
More...
Inherits enable_shared_from_this< TCPConnection >.
|
| ~TCPConnection () |
| Destructor. Cleanly closes the socket. More...
|
|
boost::asio::ip::tcp::socket & | socket () |
| Gives a reference to the internal socket. More...
|
|
const boost::asio::ip::tcp::socket & | socket () const |
| Gives a constant referemce to the internal socket. More...
|
|
template<typename HANDLER > |
void | send (cf3::common::XML::SignalFrame &args, HANDLER callback_function) |
| Sends a message to the remote entity. The message is sent asynchronously and the function returns directly, before the data is actually send. Once the sending is finished, the callback_function is called with error code. More...
|
|
template<typename HANDLER > |
void | read (cf3::common::XML::SignalFrame &args, HANDLER callback_function) |
| Initiates an asynchronous reading from the remote entity. The function returns directly. More...
|
|
void | disconnect () |
| Disconnects the socket from the remote entity. More...
|
|
void | set_error_handler (boost::weak_ptr< ErrorHandler > handler) |
|
|
template<typename HANDLER > |
void | callback_header_read (cf3::common::XML::SignalFrame &args, const boost::system::error_code &error, boost::tuple< HANDLER > functions) |
| Function called when a frame header has been read, successfully or not. This function allocates the frame data buffer depending on the header received. If reading has failed or header is not valid, the callback function is called with appropriate error code. More...
|
|
template<typename HANDLER > |
void | callback_data_read (common::XML::SignalFrame &args, const boost::system::error_code &error, boost::tuple< HANDLER > functions) |
| Function called when the frame data has been read. More...
|
|
| TCPConnection (boost::asio::io_service &io_service) |
| Constructor. More...
|
|
void | prepare_write_buffers (common::XML::SignalFrame &args, std::vector< boost::asio::const_buffer > &buffers) |
| Builds the data to be sent on the network. More...
|
|
void | process_header (boost::system::error_code &error) |
| Processes a frame header. Tries to cast the header to an unsigned int . On success, allocates the data buffer to this size. More...
|
|
void | parse_frame_data (common::XML::SignalFrame &args, boost::system::error_code &error) |
| Parses frame data from string to XML. More...
|
|
void | notify_error (const std::string &message) const |
| Notifies an error if an error handler has been set. More...
|
|
Manages a TCP/IP connection between two entities where one is a server and the other one is a client.
This class is intented to be used in an asynchronous network architecture. Therefore, a Connection
object cannot live outside of a shared pointer because a such object has to maintain asynchronous operations. One cannot predict when all of those operations will be completed; using a shared pointer garantees the connection stays alive until all operations are done. Use static function create()
to create a Connection
object.
A TCP/IP connection is based on an I/O service that handles the asynchronous operations and calls an appropriate function when one of those is completed.
Frames handled by this class have two main parts:
- A size-fixed header (8 bytes): contains the size in bytes of the frame data.
- Frame data: actual data that is sent, in XML format.
The header is completely tansparent to the calling code and is used as a safeguard to check that all data has arrived and allocate the correct buffer for the reading process.
This class can be used in both client and server applications. However, an additional step is needed on the server-side: open a network connection and start accepting new clients connections.
Most interesting method provided by this class are obviously read()
and send()
. Both take the same parameters:
- a non-const reference to a
SignalFrame
object. read()
uses it as a buffer to which read data will be written. For send()
, it contains the data to send (the reference is non-const because SignalFrame::flush_maps()
is called before converting the XML to string).
- the callback function which will be called when asynchronous operations finishes (on succes or failure).
The callback function can be created using Boost.Bind
. Developers are free on the choice of the paramaters to give to this callback function, but have to handle those parameters by themselves. However, both read
and send
methods require one placeholder of type boost::system::error_code
to store the error code. The placeholder value is given by the network layer. Examples:
- call
read
with a callback function that only takes the error code: connection->read( m_read_buffer,
boost::bind( &MyClass::callback_read,
this,
boost::asio::placeholders::error )
);
void MyClass::callback_read( const boost::system::error_code error )
{
if ( error )
{
{
else
{
{
}
- call
read
with a callback function that takes the error code and a Connection
shared pointer (i.e. a multi-client server application that wants to know which client is speaking): connection->read( m_read_buffer,
boost::bind( &MyClass::callback_read,
this,
connection,
boost::asio::placeholders::error )
);
void MyClass::callback_read( Connection::Ptr conn, const boost::system::error_code error )
{
if ( error )
{
{
else
{
{
}
Similar codes can be applied to send
function.
The internal socket can be retrieve by calling socket()
. Developer can set an error handler by calling set_error_handler()
.
- Warning
- Due to template code, a lot of Boost headers are included by this file, which might increase the compilation time. Please consider only including this file in a limited number of CPP files (typically, one per application is sufficient).
- See also
- ErrorHandler
- Author
- Quentin Gasper
Definition at line 143 of file TCPConnection.hpp.