~ubuntu-branches/debian/sid/boost1.49/sid

« back to all changes in this revision

Viewing changes to libs/asio/example/timeouts/async_tcp_client.cpp

  • Committer: Package Import Robot
  • Author(s): Steve M. Robbins
  • Date: 2012-02-26 00:31:44 UTC
  • Revision ID: package-import@ubuntu.com-20120226003144-eaytp12cbf6ubpms
Tags: upstream-1.49.0
ImportĀ upstreamĀ versionĀ 1.49.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// async_tcp_client.cpp
 
3
// ~~~~~~~~~~~~~~~~~~~~
 
4
//
 
5
// Copyright (c) 2003-2012 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
#include <boost/asio/deadline_timer.hpp>
 
12
#include <boost/asio/io_service.hpp>
 
13
#include <boost/asio/ip/tcp.hpp>
 
14
#include <boost/asio/read_until.hpp>
 
15
#include <boost/asio/streambuf.hpp>
 
16
#include <boost/asio/write.hpp>
 
17
#include <boost/bind.hpp>
 
18
#include <iostream>
 
19
 
 
20
using boost::asio::deadline_timer;
 
21
using boost::asio::ip::tcp;
 
22
 
 
23
//
 
24
// This class manages socket timeouts by applying the concept of a deadline.
 
25
// Some asynchronous operations are given deadlines by which they must complete.
 
26
// Deadlines are enforced by an "actor" that persists for the lifetime of the
 
27
// client object:
 
28
//
 
29
//  +----------------+
 
30
//  |                |
 
31
//  | check_deadline |<---+
 
32
//  |                |    |
 
33
//  +----------------+    | async_wait()
 
34
//              |         |
 
35
//              +---------+
 
36
//
 
37
// If the deadline actor determines that the deadline has expired, the socket
 
38
// is closed and any outstanding operations are consequently cancelled.
 
39
//
 
40
// Connection establishment involves trying each endpoint in turn until a
 
41
// connection is successful, or the available endpoints are exhausted. If the
 
42
// deadline actor closes the socket, the connect actor is woken up and moves to
 
43
// the next endpoint.
 
44
//
 
45
//  +---------------+
 
46
//  |               |
 
47
//  | start_connect |<---+
 
48
//  |               |    |
 
49
//  +---------------+    |
 
50
//           |           |
 
51
//  async_-  |    +----------------+
 
52
// connect() |    |                |
 
53
//           +--->| handle_connect |
 
54
//                |                |
 
55
//                +----------------+
 
56
//                          :
 
57
// Once a connection is     :
 
58
// made, the connect        :
 
59
// actor forks in two -     :
 
60
//                          :
 
61
// an actor for reading     :       and an actor for
 
62
// inbound messages:        :       sending heartbeats:
 
63
//                          :
 
64
//  +------------+          :          +-------------+
 
65
//  |            |<- - - - -+- - - - ->|             |
 
66
//  | start_read |                     | start_write |<---+
 
67
//  |            |<---+                |             |    |
 
68
//  +------------+    |                +-------------+    | async_wait()
 
69
//          |         |                        |          |
 
70
//  async_- |    +-------------+       async_- |    +--------------+
 
71
//   read_- |    |             |       write() |    |              |
 
72
//  until() +--->| handle_read |               +--->| handle_write |
 
73
//               |             |                    |              |
 
74
//               +-------------+                    +--------------+
 
75
//
 
76
// The input actor reads messages from the socket, where messages are delimited
 
77
// by the newline character. The deadline for a complete message is 30 seconds.
 
78
//
 
79
// The heartbeat actor sends a heartbeat (a message that consists of a single
 
80
// newline character) every 10 seconds. In this example, no deadline is applied
 
81
// message sending.
 
82
//
 
83
class client
 
84
{
 
85
public:
 
86
  client(boost::asio::io_service& io_service)
 
87
    : stopped_(false),
 
88
      socket_(io_service),
 
89
      deadline_(io_service),
 
90
      heartbeat_timer_(io_service)
 
91
  {
 
92
  }
 
93
 
 
94
  // Called by the user of the client class to initiate the connection process.
 
95
  // The endpoint iterator will have been obtained using a tcp::resolver.
 
96
  void start(tcp::resolver::iterator endpoint_iter)
 
97
  {
 
98
    // Start the connect actor.
 
99
    start_connect(endpoint_iter);
 
100
 
 
101
    // Start the deadline actor. You will note that we're not setting any
 
102
    // particular deadline here. Instead, the connect and input actors will
 
103
    // update the deadline prior to each asynchronous operation.
 
104
    deadline_.async_wait(boost::bind(&client::check_deadline, this));
 
105
  }
 
106
 
 
107
  // This function terminates all the actors to shut down the connection. It
 
108
  // may be called by the user of the client class, or by the class itself in
 
109
  // response to graceful termination or an unrecoverable error.
 
110
  void stop()
 
111
  {
 
112
    stopped_ = true;
 
113
    boost::system::error_code ignored_ec;
 
114
    socket_.close(ignored_ec);
 
115
    deadline_.cancel();
 
116
    heartbeat_timer_.cancel();
 
117
  }
 
118
 
 
119
private:
 
120
  void start_connect(tcp::resolver::iterator endpoint_iter)
 
121
  {
 
122
    if (endpoint_iter != tcp::resolver::iterator())
 
123
    {
 
124
      std::cout << "Trying " << endpoint_iter->endpoint() << "...\n";
 
125
 
 
126
      // Set a deadline for the connect operation.
 
127
      deadline_.expires_from_now(boost::posix_time::seconds(60));
 
128
 
 
129
      // Start the asynchronous connect operation.
 
130
      socket_.async_connect(endpoint_iter->endpoint(),
 
131
          boost::bind(&client::handle_connect,
 
132
            this, _1, endpoint_iter));
 
133
    }
 
134
    else
 
135
    {
 
136
      // There are no more endpoints to try. Shut down the client.
 
137
      stop();
 
138
    }
 
139
  }
 
140
 
 
141
  void handle_connect(const boost::system::error_code& ec,
 
142
      tcp::resolver::iterator endpoint_iter)
 
143
  {
 
144
    if (stopped_)
 
145
      return;
 
146
 
 
147
    // The async_connect() function automatically opens the socket at the start
 
148
    // of the asynchronous operation. If the socket is closed at this time then
 
149
    // the timeout handler must have run first.
 
150
    if (!socket_.is_open())
 
151
    {
 
152
      std::cout << "Connect timed out\n";
 
153
 
 
154
      // Try the next available endpoint.
 
155
      start_connect(++endpoint_iter);
 
156
    }
 
157
 
 
158
    // Check if the connect operation failed before the deadline expired.
 
159
    else if (ec)
 
160
    {
 
161
      std::cout << "Connect error: " << ec.message() << "\n";
 
162
 
 
163
      // We need to close the socket used in the previous connection attempt
 
164
      // before starting a new one.
 
165
      socket_.close();
 
166
 
 
167
      // Try the next available endpoint.
 
168
      start_connect(++endpoint_iter);
 
169
    }
 
170
 
 
171
    // Otherwise we have successfully established a connection.
 
172
    else
 
173
    {
 
174
      std::cout << "Connected to " << endpoint_iter->endpoint() << "\n";
 
175
 
 
176
      // Start the input actor.
 
177
      start_read();
 
178
 
 
179
      // Start the heartbeat actor.
 
180
      start_write();
 
181
    }
 
182
  }
 
183
 
 
184
  void start_read()
 
185
  {
 
186
    // Set a deadline for the read operation.
 
187
    deadline_.expires_from_now(boost::posix_time::seconds(30));
 
188
 
 
189
    // Start an asynchronous operation to read a newline-delimited message.
 
190
    boost::asio::async_read_until(socket_, input_buffer_, '\n',
 
191
        boost::bind(&client::handle_read, this, _1));
 
192
  }
 
193
 
 
194
  void handle_read(const boost::system::error_code& ec)
 
195
  {
 
196
    if (stopped_)
 
197
      return;
 
198
 
 
199
    if (!ec)
 
200
    {
 
201
      // Extract the newline-delimited message from the buffer.
 
202
      std::string line;
 
203
      std::istream is(&input_buffer_);
 
204
      std::getline(is, line);
 
205
 
 
206
      // Empty messages are heartbeats and so ignored.
 
207
      if (!line.empty())
 
208
      {
 
209
        std::cout << "Received: " << line << "\n";
 
210
      }
 
211
 
 
212
      start_read();
 
213
    }
 
214
    else
 
215
    {
 
216
      std::cout << "Error on receive: " << ec.message() << "\n";
 
217
 
 
218
      stop();
 
219
    }
 
220
  }
 
221
 
 
222
  void start_write()
 
223
  {
 
224
    if (stopped_)
 
225
      return;
 
226
 
 
227
    // Start an asynchronous operation to send a heartbeat message.
 
228
    boost::asio::async_write(socket_, boost::asio::buffer("\n", 1),
 
229
        boost::bind(&client::handle_write, this, _1));
 
230
  }
 
231
 
 
232
  void handle_write(const boost::system::error_code& ec)
 
233
  {
 
234
    if (stopped_)
 
235
      return;
 
236
 
 
237
    if (!ec)
 
238
    {
 
239
      // Wait 10 seconds before sending the next heartbeat.
 
240
      heartbeat_timer_.expires_from_now(boost::posix_time::seconds(10));
 
241
      heartbeat_timer_.async_wait(boost::bind(&client::start_write, this));
 
242
    }
 
243
    else
 
244
    {
 
245
      std::cout << "Error on heartbeat: " << ec.message() << "\n";
 
246
 
 
247
      stop();
 
248
    }
 
249
  }
 
250
 
 
251
  void check_deadline()
 
252
  {
 
253
    if (stopped_)
 
254
      return;
 
255
 
 
256
    // Check whether the deadline has passed. We compare the deadline against
 
257
    // the current time since a new asynchronous operation may have moved the
 
258
    // deadline before this actor had a chance to run.
 
259
    if (deadline_.expires_at() <= deadline_timer::traits_type::now())
 
260
    {
 
261
      // The deadline has passed. The socket is closed so that any outstanding
 
262
      // asynchronous operations are cancelled.
 
263
      socket_.close();
 
264
 
 
265
      // There is no longer an active deadline. The expiry is set to positive
 
266
      // infinity so that the actor takes no action until a new deadline is set.
 
267
      deadline_.expires_at(boost::posix_time::pos_infin);
 
268
    }
 
269
 
 
270
    // Put the actor back to sleep.
 
271
    deadline_.async_wait(boost::bind(&client::check_deadline, this));
 
272
  }
 
273
 
 
274
private:
 
275
  bool stopped_;
 
276
  tcp::socket socket_;
 
277
  boost::asio::streambuf input_buffer_;
 
278
  deadline_timer deadline_;
 
279
  deadline_timer heartbeat_timer_;
 
280
};
 
281
 
 
282
int main(int argc, char* argv[])
 
283
{
 
284
  try
 
285
  {
 
286
    if (argc != 3)
 
287
    {
 
288
      std::cerr << "Usage: client <host> <port>\n";
 
289
      return 1;
 
290
    }
 
291
 
 
292
    boost::asio::io_service io_service;
 
293
    tcp::resolver r(io_service);
 
294
    client c(io_service);
 
295
 
 
296
    c.start(r.resolve(tcp::resolver::query(argv[1], argv[2])));
 
297
 
 
298
    io_service.run();
 
299
  }
 
300
  catch (std::exception& e)
 
301
  {
 
302
    std::cerr << "Exception: " << e.what() << "\n";
 
303
  }
 
304
 
 
305
  return 0;
 
306
}