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