asio 0.3.8rc3 | Home | Reference | Tutorial | Examples | Design

Examples |

# s11n_example::connection Class Reference

Collaboration diagram for s11n_example::connection:

List of all members.

## Detailed Description

The connection class provides serialization primitives on top of a socket.

Each message sent using this class consists of:

- An 8-byte header containing the length of the serialized data in hexadecimal.
- The serialized data.

Definition at line 34 of file connection.hpp.

## Public Member Functions

| connection (asio::io_service &io_service)
| Constructor.
asio::ip::tcp::socket & | socket ()
| Get the underlying socket. Used for making a connection or for accepting an incoming connection.
template<typename T, typename Handler>
void | async_write (const T &t, Handler handler)
| Asynchronously write a data structure to the socket.
template<typename T, typename Handler>
void | async_read (T &t, Handler handler)
| Asynchronously read a data structure from the socket.
template<typename T, typename Handler>
void | handle_read_header (const asio::error_code &e, T &t, boost::tuple< Handler > handler)
| Handle a completed read of a message header. The handler is passed using a tuple since boost::bind seems to have trouble binding a function object created using boost::bind as a parameter.
template<typename T, typename Handler>
void | handle_read_data (const asio::error_code &e, T &t, boost::tuple< Handler > handler)
| Handle a completed read of message data.

## Private Types

enum | { header_length = 8 }
| The size of a fixed length header. More...

## Private Attributes

asio::ip::tcp::socket | socket_
| The underlying socket.
std::string | outbound_header_
| Holds an outbound header.
std::string | outbound_data_
| Holds the outbound data.
char | inbound_header_ [header_length]
| Holds an inbound header.
std::vector< char > | inbound_data_
| Holds the inbound data.

## Member Enumeration Documentation

anonymous enum`[private]`

The size of a fixed length header.

Enumerator:

  header_length |

Definition at line 166 of file connection.hpp.

    00169 { header_length = 8 };

## Constructor & Destructor Documentation

s11n_example::connection::connection | ( | asio::io_service & | io_service | ) |

Constructor.

Definition at line 38 of file connection.hpp.

    00039     : socket_(io_service)
    00040   {
    00041   }

## Member Function Documentation

asio::ip::tcp::socket& s11n_example::connection::socket | ( |  | ) |

Get the underlying socket. Used for making a connection or for accepting an incoming connection.

Definition at line 44 of file connection.hpp.

Referenced by s11n_example::client::client(), and s11n_example::client::handle_connect().

    00046   {
    00047     return socket_;

template<typename T, typename Handler>

void s11n_example::connection::async_write | ( | const T & | t,
|  | Handler | handler |
| ) |  |  |

Asynchronously write a data structure to the socket.

Definition at line 51 of file connection.hpp.

    00053   {
    00054     // Serialize the data first so we know how large it is.
    00055     std::ostringstream archive_stream;
    00056     boost::archive::text_oarchive archive(archive_stream);
    00057     archive << t;
    00058     outbound_data_ = archive_stream.str();
    00059
    00060     // Format the header.
    00061     std::ostringstream header_stream;
    00062     header_stream << std::setw(header_length)
    00063       << std::hex << outbound_data_.size();
    00064     if (!header_stream || header_stream.str().size() != header_length)
    00065     {
    00066       // Something went wrong, inform the caller.
    00067       asio::error_code error(asio::error::invalid_argument);
    00068       socket_.io_service().post(boost::bind(handler, error));
    00069       return;
    00070     }
    00071     outbound_header_ = header_stream.str();
    00072
    00073     // Write the serialized data to the socket. We use "gather-write" to send
    00074     // both the header and the data in a single write operation.
    00075     std::vector<asio::const_buffer> buffers;
    00076     buffers.push_back(asio::buffer(outbound_header_));
    00077     buffers.push_back(asio::buffer(outbound_data_));
    00078     asio::async_write(socket_, buffers, handler);

template<typename T, typename Handler>

void s11n_example::connection::async_read | ( | T & | t,
|  | Handler | handler |
| ) |  |  |

Asynchronously read a data structure from the socket.

Definition at line 82 of file connection.hpp.

Referenced by s11n_example::client::handle_connect().

    00084   {
    00085     // Issue a read operation to read exactly the number of bytes in a header.
    00086     void (connection::*f)(
    00087         const asio::error_code&,
    00088         T&, boost::tuple<Handler>)
    00089       = &connection::handle_read_header<T, Handler>;
    00090     asio::async_read(socket_, asio::buffer(inbound_header_),
    00091         boost::bind(f,
    00092           this, asio::placeholders::error, boost::ref(t),
    00093           boost::make_tuple(handler)));

template<typename T, typename Handler>

void s11n_example::connection::handle_read_header | ( | const asio::error_code & | e,
|  | T & | t,
|  | boost::tuple< Handler > | handler |
| ) |  |  |

Handle a completed read of a message header. The handler is passed using a tuple since boost::bind seems to have trouble binding a function object created using boost::bind as a parameter.

Definition at line 97 of file connection.hpp.

    00102   {
    00103     if (e)
    00104     {
    00105       boost::get<0>(handler)(e);
    00106     }
    00107     else
    00108     {
    00109       // Determine the length of the serialized data.
    00110       std::istringstream is(std::string(inbound_header_, header_length));
    00111       std::size_t inbound_data_size = 0;
    00112       if (!(is >> std::hex >> inbound_data_size))
    00113       {
    00114         // Header doesn't seem to be valid. Inform the caller.
    00115         asio::error_code error(asio::error::invalid_argument);
    00116         boost::get<0>(handler)(error);
    00117         return;
    00118       }
    00119
    00120       // Start an asynchronous call to receive the data.
    00121       inbound_data_.resize(inbound_data_size);
    00122       void (connection::*f)(
    00123           const asio::error_code&,
    00124           T&, boost::tuple<Handler>)
    00125         = &connection::handle_read_data<T, Handler>;
    00126       asio::async_read(socket_, asio::buffer(inbound_data_),
    00127         boost::bind(f, this,

template<typename T, typename Handler>

void s11n_example::connection::handle_read_data | ( | const asio::error_code & | e,
|  | T & | t,
|  | boost::tuple< Handler > | handler |
| ) |  |  |

Handle a completed read of message data.

Definition at line 131 of file connection.hpp.

    00136   {
    00137     if (e)
    00138     {
    00139       boost::get<0>(handler)(e);
    00140     }
    00141     else
    00142     {
    00143       // Extract the data structure from the data just received.
    00144       try
    00145       {
    00146         std::string archive_data(&inbound_data_[0], inbound_data_.size());
    00147         std::istringstream archive_stream(archive_data);
    00148         boost::archive::text_iarchive archive(archive_stream);
    00149         archive >> t;
    00150       }
    00151       catch (std::exception& e)
    00152       {
    00153         // Unable to decode data.
    00154         asio::error_code error(asio::error::invalid_argument);
    00155         boost::get<0>(handler)(error);
    00156         return;
    00157       }
    00158
    00159       // Inform caller that data has been received ok.

## Member Data Documentation

asio::ip::tcp::socket s11n_example::connection::socket_`[private]`

The underlying socket.

Definition at line 163 of file connection.hpp.

Referenced by async_read(), and async_write().

std::string s11n_example::connection::outbound_header_`[private]`

Holds an outbound header.

Definition at line 169 of file connection.hpp.

Referenced by async_write().

std::string s11n_example::connection::outbound_data_`[private]`

Holds the outbound data.

Definition at line 172 of file connection.hpp.

Referenced by async_write().

char s11n_example::connection::inbound_header_[header_length]`[private]`

Holds an inbound header.

Definition at line 175 of file connection.hpp.

Referenced by async_read().

std::vector<char> s11n_example::connection::inbound_data_`[private]`

Holds the inbound data.

Definition at line 178 of file connection.hpp.

Referenced by handle_read_data().

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

- serialization/connection.hpp

asio 0.3.8rc3 | Home | Reference | Tutorial | Examples | Design