| Home | Libraries | People | FAQ | More

# 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 24 of file connection.hpp.

## Public Member Functions

| connection (boost::asio::io_service &io_service)
| Constructor.
boost::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 boost::asio::error &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 boost::asio::error &e, T &t, boost::tuple< Handler > handler)
| Handle a completed read of message data.

## Private Types

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

## Private Attributes

boost::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 152 of file connection.hpp.

    00155 { header_length = 8 };

## Constructor & Destructor Documentation

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

Constructor.

Definition at line 28 of file connection.hpp.

    00029     : socket_(io_service)
    00030   {
    00031   }

## Member Function Documentation

boost::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 34 of file connection.hpp.

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

    00036   {
    00037     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 41 of file connection.hpp.

    00043   {
    00044     // Serialize the data first so we know how large it is.
    00045     std::ostringstream archive_stream;
    00046     boost::archive::text_oarchive archive(archive_stream);
    00047     archive << t;
    00048     outbound_data_ = archive_stream.str();
    00049
    00050     // Format the header.
    00051     std::ostringstream header_stream;
    00052     header_stream << std::setw(header_length)
    00053       << std::hex << outbound_data_.size();
    00054     if (!header_stream || header_stream.str().size() != header_length)
    00055     {
    00056       // Something went wrong, inform the caller.
    00057       boost::asio::error error(boost::asio::error::invalid_argument);
    00058       socket_.io_service().post(boost::bind(handler, error));
    00059       return;
    00060     }
    00061     outbound_header_ = header_stream.str();
    00062
    00063     // Write the serialized data to the socket. We use "gather-write" to send
    00064     // both the header and the data in a single write operation.
    00065     std::vector<boost::asio::const_buffer> buffers;
    00066     buffers.push_back(boost::asio::buffer(outbound_header_));
    00067     buffers.push_back(boost::asio::buffer(outbound_data_));
    00068     boost::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 72 of file connection.hpp.

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

    00074   {
    00075     // Issue a read operation to read exactly the number of bytes in a header.
    00076     void (connection::*f)(const boost::asio::error&, T&, boost::tuple<Handler>)
    00077       = &connection::handle_read_header<T, Handler>;
    00078     boost::asio::async_read(socket_, boost::asio::buffer(inbound_header_),
    00079         boost::bind(f,
    00080           this, boost::asio::placeholders::error, boost::ref(t),
    00081           boost::make_tuple(handler)));

template<typename T, typename Handler>

void s11n_example::connection::handle_read_header | ( | const boost::asio::error & | 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 85 of file connection.hpp.

    00090   {
    00091     if (e)
    00092     {
    00093       boost::get<0>(handler)(e);
    00094     }
    00095     else
    00096     {
    00097       // Determine the length of the serialized data.
    00098       std::istringstream is(std::string(inbound_header_, header_length));
    00099       std::size_t inbound_data_size = 0;
    00100       if (!(is >> std::hex >> inbound_data_size))
    00101       {
    00102         // Header doesn't seem to be valid. Inform the caller.
    00103         boost::asio::error error(boost::asio::error::invalid_argument);
    00104         boost::get<0>(handler)(error);
    00105         return;
    00106       }
    00107
    00108       // Start an asynchronous call to receive the data.
    00109       inbound_data_.resize(inbound_data_size);
    00110       void (connection::*f)(const boost::asio::error&, T&, boost::tuple<Handler>)
    00111         = &connection::handle_read_data<T, Handler>;
    00112       boost::asio::async_read(socket_, boost::asio::buffer(inbound_data_),
    00113         boost::bind(f, this,

template<typename T, typename Handler>

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

Handle a completed read of message data.

Definition at line 117 of file connection.hpp.

    00122   {
    00123     if (e)
    00124     {
    00125       boost::get<0>(handler)(e);
    00126     }
    00127     else
    00128     {
    00129       // Extract the data structure from the data just received.
    00130       try
    00131       {
    00132         std::string archive_data(&inbound_data_[0], inbound_data_.size());
    00133         std::istringstream archive_stream(archive_data);
    00134         boost::archive::text_iarchive archive(archive_stream);
    00135         archive >> t;
    00136       }
    00137       catch (std::exception& e)
    00138       {
    00139         // Unable to decode data.
    00140         boost::asio::error error(boost::asio::error::invalid_argument);
    00141         boost::get<0>(handler)(error);
    00142         return;
    00143       }
    00144
    00145       // Inform caller that data has been received ok.

## Member Data Documentation

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

The underlying socket.

Definition at line 149 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 155 of file connection.hpp.

Referenced by async_write().

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

Holds the outbound data.

Definition at line 158 of file connection.hpp.

Referenced by async_write().

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

Holds an inbound header.

Definition at line 161 of file connection.hpp.

Referenced by async_read().

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

Holds the inbound data.

Definition at line 164 of file connection.hpp.

Referenced by handle_read_data().

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

- serialization/connection.hpp

| Copyright © 2003 - 2006 Christopher M. Kohlhoff