[af3aed] | 1 | /*
|
---|
| 2 | * Connection.hpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Oct 21, 2011
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #ifndef CONNECTION_HPP_
|
---|
| 9 | #define CONNECTION_HPP_
|
---|
| 10 |
|
---|
| 11 | // include config.h
|
---|
| 12 | #ifdef HAVE_CONFIG_H
|
---|
| 13 | #include <config.h>
|
---|
| 14 | #endif
|
---|
[72eaf7f] | 15 |
|
---|
| 16 | #include <boost/archive/text_iarchive.hpp>
|
---|
| 17 | #include <boost/archive/text_oarchive.hpp>
|
---|
[af3aed] | 18 |
|
---|
| 19 | #include <boost/asio.hpp>
|
---|
[72eaf7f] | 20 | #include <boost/bind.hpp>
|
---|
| 21 | #include <boost/shared_ptr.hpp>
|
---|
| 22 | #include <boost/tuple/tuple.hpp>
|
---|
| 23 | #include <iomanip>
|
---|
| 24 | #include <string>
|
---|
| 25 | #include <sstream>
|
---|
| 26 | #include <vector>
|
---|
| 27 |
|
---|
[af3aed] | 28 | /** The Connection class provides serialization primitives on top of a socket.
|
---|
[72eaf7f] | 29 | * Each message sent using this class consists of:
|
---|
| 30 | * @li An 8-byte header containing the length of the serialized data in
|
---|
| 31 | * hexadecimal.
|
---|
| 32 | * @li The serialized data.
|
---|
| 33 | */
|
---|
[af3aed] | 34 | class Connection
|
---|
[72eaf7f] | 35 | {
|
---|
| 36 | public:
|
---|
| 37 | /// Constructor.
|
---|
[af3aed] | 38 | Connection(boost::asio::io_service& io_service)
|
---|
[72eaf7f] | 39 | : socket_(io_service)
|
---|
| 40 | {
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[af3aed] | 43 | /// Get the underlying socket. Used for making a Connection or for accepting
|
---|
| 44 | /// an incoming Connection.
|
---|
[72eaf7f] | 45 | boost::asio::ip::tcp::socket& socket()
|
---|
| 46 | {
|
---|
| 47 | return socket_;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /// Asynchronously write a data structure to the socket.
|
---|
| 51 | template <typename T, typename Handler>
|
---|
| 52 | void async_write(const T& t, Handler handler)
|
---|
| 53 | {
|
---|
| 54 | // Serialize the data first so we know how large it is.
|
---|
| 55 | std::ostringstream archive_stream;
|
---|
| 56 | boost::archive::text_oarchive archive(archive_stream);
|
---|
| 57 | archive << t;
|
---|
| 58 | outbound_data_ = archive_stream.str();
|
---|
| 59 |
|
---|
| 60 | // Format the header.
|
---|
| 61 | std::ostringstream header_stream;
|
---|
| 62 | header_stream << std::setw(header_length)
|
---|
| 63 | << std::hex << outbound_data_.size();
|
---|
| 64 | if (!header_stream || header_stream.str().size() != header_length)
|
---|
| 65 | {
|
---|
| 66 | // Something went wrong, inform the caller.
|
---|
| 67 | boost::system::error_code error(boost::asio::error::invalid_argument);
|
---|
| 68 | socket_.get_io_service().post(boost::bind(handler, error));
|
---|
| 69 | return;
|
---|
| 70 | }
|
---|
| 71 | outbound_header_ = header_stream.str();
|
---|
| 72 |
|
---|
| 73 | // Write the serialized data to the socket. We use "gather-write" to send
|
---|
| 74 | // both the header and the data in a single write operation.
|
---|
| 75 | std::vector<boost::asio::const_buffer> buffers;
|
---|
| 76 | buffers.push_back(boost::asio::buffer(outbound_header_));
|
---|
| 77 | buffers.push_back(boost::asio::buffer(outbound_data_));
|
---|
| 78 | boost::asio::async_write(socket_, buffers, handler);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /// Asynchronously read a data structure from the socket.
|
---|
| 82 | template <typename T, typename Handler>
|
---|
| 83 | void async_read(T& t, Handler handler)
|
---|
| 84 | {
|
---|
| 85 | // Issue a read operation to read exactly the number of bytes in a header.
|
---|
[af3aed] | 86 | void (Connection::*f)(
|
---|
[72eaf7f] | 87 | const boost::system::error_code&,
|
---|
| 88 | T&, boost::tuple<Handler>)
|
---|
[af3aed] | 89 | = &Connection::handle_read_header<T, Handler>;
|
---|
[72eaf7f] | 90 | boost::asio::async_read(socket_, boost::asio::buffer(inbound_header_),
|
---|
| 91 | boost::bind(f,
|
---|
| 92 | this, boost::asio::placeholders::error, boost::ref(t),
|
---|
| 93 | boost::make_tuple(handler)));
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /// Handle a completed read of a message header. The handler is passed using
|
---|
| 97 | /// a tuple since boost::bind seems to have trouble binding a function object
|
---|
| 98 | /// created using boost::bind as a parameter.
|
---|
| 99 | template <typename T, typename Handler>
|
---|
| 100 | void handle_read_header(const boost::system::error_code& e,
|
---|
| 101 | T& t, boost::tuple<Handler> handler)
|
---|
| 102 | {
|
---|
| 103 | if (e)
|
---|
| 104 | {
|
---|
| 105 | boost::get<0>(handler)(e);
|
---|
| 106 | }
|
---|
| 107 | else
|
---|
| 108 | {
|
---|
| 109 | // Determine the length of the serialized data.
|
---|
| 110 | std::istringstream is(std::string(inbound_header_, header_length));
|
---|
| 111 | std::size_t inbound_data_size = 0;
|
---|
| 112 | if (!(is >> std::hex >> inbound_data_size))
|
---|
| 113 | {
|
---|
| 114 | // Header doesn't seem to be valid. Inform the caller.
|
---|
| 115 | boost::system::error_code error(boost::asio::error::invalid_argument);
|
---|
| 116 | boost::get<0>(handler)(error);
|
---|
| 117 | return;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | // Start an asynchronous call to receive the data.
|
---|
| 121 | inbound_data_.resize(inbound_data_size);
|
---|
[af3aed] | 122 | void (Connection::*f)(
|
---|
[72eaf7f] | 123 | const boost::system::error_code&,
|
---|
| 124 | T&, boost::tuple<Handler>)
|
---|
[af3aed] | 125 | = &Connection::handle_read_data<T, Handler>;
|
---|
[72eaf7f] | 126 | boost::asio::async_read(socket_, boost::asio::buffer(inbound_data_),
|
---|
| 127 | boost::bind(f, this,
|
---|
| 128 | boost::asio::placeholders::error, boost::ref(t), handler));
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /// Handle a completed read of message data.
|
---|
| 133 | template <typename T, typename Handler>
|
---|
| 134 | void handle_read_data(const boost::system::error_code& e,
|
---|
| 135 | T& t, boost::tuple<Handler> handler)
|
---|
| 136 | {
|
---|
| 137 | if (e)
|
---|
| 138 | {
|
---|
| 139 | boost::get<0>(handler)(e);
|
---|
| 140 | }
|
---|
| 141 | else
|
---|
| 142 | {
|
---|
| 143 | // Extract the data structure from the data just received.
|
---|
| 144 | try
|
---|
| 145 | {
|
---|
| 146 | std::string archive_data(&inbound_data_[0], inbound_data_.size());
|
---|
| 147 | std::istringstream archive_stream(archive_data);
|
---|
| 148 | boost::archive::text_iarchive archive(archive_stream);
|
---|
| 149 | archive >> t;
|
---|
| 150 | }
|
---|
| 151 | catch (std::exception& e)
|
---|
| 152 | {
|
---|
| 153 | // Unable to decode data.
|
---|
| 154 | boost::system::error_code error(boost::asio::error::invalid_argument);
|
---|
| 155 | boost::get<0>(handler)(error);
|
---|
| 156 | return;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | // Inform caller that data has been received ok.
|
---|
| 160 | boost::get<0>(handler)(e);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | private:
|
---|
| 165 | /// The underlying socket.
|
---|
| 166 | boost::asio::ip::tcp::socket socket_;
|
---|
| 167 |
|
---|
| 168 | /// The size of a fixed length header.
|
---|
| 169 | enum { header_length = 8 };
|
---|
| 170 |
|
---|
| 171 | /// Holds an outbound header.
|
---|
| 172 | std::string outbound_header_;
|
---|
| 173 |
|
---|
| 174 | /// Holds the outbound data.
|
---|
| 175 | std::string outbound_data_;
|
---|
| 176 |
|
---|
| 177 | /// Holds an inbound header.
|
---|
| 178 | char inbound_header_[header_length];
|
---|
| 179 |
|
---|
| 180 | /// Holds the inbound data.
|
---|
| 181 | std::vector<char> inbound_data_;
|
---|
| 182 | };
|
---|
| 183 |
|
---|
[af3aed] | 184 | typedef boost::shared_ptr<Connection> connection_ptr;
|
---|
[72eaf7f] | 185 |
|
---|
[af3aed] | 186 | #endif /* CONNECTION_HPP_ */
|
---|